模糊日期算法在Objective-C [英] Fuzzy Date algorithm in Objective-C

查看:89
本文介绍了模糊日期算法在Objective-C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个模糊的日期计算方法日期在Objective-C的iPhone。有一种流行的解释一下:

<一个href="http://stackoverflow.com/questions/11/how-do-i-calculate-relative-time">http://stackoverflow.com/questions/11/how-do-i-calculate-relative-time

然而,它包含缺少的参数。怎样才能在Objective-C本中使用?谢谢你。

  const int的第二= 1;
const int的分钟= 60 *秒;
const int的小时= 60 *分;
const int的天= 24 *小时,
const int的月= 30 *天;

如果(增量&LT; 1 *分钟)
{
  返回ts.Seconds == 1? 一秒钟前:ts.Seconds +秒前;
}
如果(△2 *分钟)的
{
  返回一分钟前;
}
如果(增量&LT; 45 *分)
{
  返回ts.Minutes +分钟前;
}
如果(增量&LT; 90 *分)
{
  返回一个小时前;
}
如果(增量&LT; 24 * HOUR)
{
  返回ts.Hours +小时前;
}
如果(增量&LT; 48 * HOUR)
{
  回归昨天;
}
如果(增量小于30 *天)
{
  返回ts.Days +日前;
}
如果(增量&LT; 12 *月)
{
  INT个月= Convert.ToInt32(Math.Floor((双)ts.Days / 30));
  返回月&其中; = 1? 一个月前:个月+几个月前;
}
其他
{
  INT年= Convert.ToInt32(Math.Floor((双)ts.Days / 365));
  返回年&其中; = 1? 一年前:年+年前;
}
 

解决方案

日期重新$ P $使用的NSDate 类psented可可。有一个在的NSDate 实施了方便的方法来获取增量在几秒钟内两个日期实例之间,<一个href="http://developer.apple.com/DOCUMENTATION/Cocoa/Reference/Foundation/Classes/NSDate%5FClass/Reference/Reference.html#//apple%5Fref/occ/instm/NSDate/timeIntervalSinceDate:"><$c$c>timeIntervalSinceDate:.这就是所谓的在一个的NSDate 实例,同时另一个的NSDate 对象作为参数。它返回一个<一个href="http://developer.apple.com/DOCUMENTATION/Cocoa/Reference/Foundation/Miscellaneous/Foundation%5FDataTypes/Reference/reference.html#//apple%5Fref/doc/c%5Fref/NSTimeInterval"><$c$c>NSTimeInterval (这是一个typedef用于双),这是秒的两个日期之间的数目的重presentative

鉴于此,这将是相当简单的,以适应code,你在上面给一个Objective-C /可可上下文。由于通过的NSDate 计算的增量以秒为单位,考虑到两个日期,你可以很容易地适应code以上:

  //常量
#定义每秒1
#定义分钟(60 *第二)
#定义小时(60 *分钟)
#定义天(24 * HOUR)
#定义MONTH(30 *天)

 - (的NSString *)timeIntervalWithStartDate:(的NSDate *)D1 withEndDate:(的NSDate *)D2
{
    //计算增量以秒为单位的两个日期之间
    NSTimeInterval增量= [D2 timeIntervalSinceDate:D1];

    如果(增量&LT; 1 *分钟)
    {
        返回三角洲== 1? @一秒钟前:[的NSString stringWithFormat:@%d秒前,(INT)三角洲]。
    }
    如果(△2 *分钟)的
    {
        返回@一分钟前;
    }
    如果(增量&LT; 45 *分)
    {
        INT分钟=地板((双)增量/分钟);
        返回[的NSString stringWithFormat:@%d分钟前,分]
    }
    如果(增量&LT; 90 *分)
    {
        返回@一个小时前;
    }
    如果(增量&LT; 24 * HOUR)
    {
        INT小时=地板((双)增量/小时);
        返回[的NSString stringWithFormat:@%d小时前,时间]。
    }
    如果(增量&LT; 48 * HOUR)
    {
        返回@昨天;
    }
    如果(增量小于30 *天)
    {
        INT天=地板((双)三角洲/天);
        返回[的NSString stringWithFormat:@%d天前天]。
    }
    如果(增量&LT; 12 *月)
    {
        INT个月=地板((双)增量/月);
        返回月&其中; = 1? @一个月前:[的NSString stringWithFormat:@%d个月前,月];
    }
    其他
    {
        INT年=地板((双)三角洲/月/ 12.0);
        返回年&其中; = 1? @一年前:[的NSString stringWithFormat:@%D年前,年]。
    }
}
 

这将被调用,传递的开始和结束的NSDate 对象作为参数,并会返回一个的NSString 同的时间间隔。

I would like to write a fuzzy date method for calculating dates in Objective-C for iPhone. There is a popular explanation here:

http://stackoverflow.com/questions/11/how-do-i-calculate-relative-time

However it contains missing arguments. How could this be used in Objective-C?. Thanks.

const int SECOND = 1;
const int MINUTE = 60 * SECOND;
const int HOUR = 60 * MINUTE;
const int DAY = 24 * HOUR;
const int MONTH = 30 * DAY;

if (delta < 1 * MINUTE)
{
  return ts.Seconds == 1 ? "one second ago" : ts.Seconds + " seconds ago";
}
if (delta < 2 * MINUTE)
{
  return "a minute ago";
}
if (delta < 45 * MINUTE)
{
  return ts.Minutes + " minutes ago";
}
if (delta < 90 * MINUTE)
{
  return "an hour ago";
}
if (delta < 24 * HOUR)
{
  return ts.Hours + " hours ago";
}
if (delta < 48 * HOUR)
{
  return "yesterday";
}
if (delta < 30 * DAY)
{
  return ts.Days + " days ago";
}
if (delta < 12 * MONTH)
{
  int months = Convert.ToInt32(Math.Floor((double)ts.Days / 30));
  return months <= 1 ? "one month ago" : months + " months ago";
}
else
{
  int years = Convert.ToInt32(Math.Floor((double)ts.Days / 365));
  return years <= 1 ? "one year ago" : years + " years ago";
}

解决方案

Dates are represented in Cocoa using the NSDate class. There is a convenient method implemented in NSDate to obtain the delta in seconds between two date instances, timeIntervalSinceDate:. This is called upon an NSDate instance, taking another NSDate object as an argument. It returns an NSTimeInterval (which is a typedef for a double), which is representative of the number of seconds between the two dates.

Given this, it would be fairly simple to adapt the code you have given above to an Objective-C/Cocoa context. Since the delta calculated by NSDate is given in seconds, given two dates, you could easily adapt the code above:

//Constants
#define SECOND 1
#define MINUTE (60 * SECOND)
#define HOUR (60 * MINUTE)
#define DAY (24 * HOUR)
#define MONTH (30 * DAY)

- (NSString*)timeIntervalWithStartDate:(NSDate*)d1 withEndDate:(NSDate*)d2
{
    //Calculate the delta in seconds between the two dates
    NSTimeInterval delta = [d2 timeIntervalSinceDate:d1];

    if (delta < 1 * MINUTE)
    {
        return delta == 1 ? @"one second ago" : [NSString stringWithFormat:@"%d seconds ago", (int)delta];
    }
    if (delta < 2 * MINUTE)
    {
        return @"a minute ago";
    }
    if (delta < 45 * MINUTE)
    {
        int minutes = floor((double)delta/MINUTE);
        return [NSString stringWithFormat:@"%d minutes ago", minutes];
    }
    if (delta < 90 * MINUTE)
    {
        return @"an hour ago";
    }
    if (delta < 24 * HOUR)
    {
        int hours = floor((double)delta/HOUR);
        return [NSString stringWithFormat:@"%d hours ago", hours];
    }
    if (delta < 48 * HOUR)
    {
        return @"yesterday";
    }
    if (delta < 30 * DAY)
    {
        int days = floor((double)delta/DAY);
        return [NSString stringWithFormat:@"%d days ago", days];
    }
    if (delta < 12 * MONTH)
    {
        int months = floor((double)delta/MONTH);
        return months <= 1 ? @"one month ago" : [NSString stringWithFormat:@"%d months ago", months];
    }
    else
    {
        int years = floor((double)delta/MONTH/12.0);
        return years <= 1 ? @"one year ago" : [NSString stringWithFormat:@"%d years ago", years];
    }
}

This would then be called, passing the start and end NSDate objects as arguments, and would return an NSString with the time interval.

这篇关于模糊日期算法在Objective-C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆