Objective-C中的模糊日算法 [英] Fuzzy Date algorithm in Objective-C

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

问题描述

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



如何在C#中计算相对时间?



但是它包含缺少的参数。如何在Objective-C中使用它?谢谢。

  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? 前一秒:ts.Seconds +秒前;
}
if(delta< 2 * MINUTE)
{
一分钟前返回;
}
if(delta< 45 * MINUTE)
{
return ts.Minutes +分钟前;
}
if(delta< 90 * MINUTE)
{
返回一小时前;
}
if(delta< 24 * HOUR)
{
return ts.Hours +hours ago;
}
if(delta< 48 * HOUR)
{
returnyesterday;
}
if(delta< 30 * DAY)
{
return ts.Days +days ago;
}
if(delta< 12 * MONTH)
{
int months = Convert.ToInt32(Math.Floor((double)ts.Days / 30));
返回月份< = 1? 一个月前:几个月+几个月前;
}
else
{
int years = Convert.ToInt32(Math.Floor((double)ts.Days / 365));
return years< = 1? 一年前:几年+几年前;
}


解决方案

日期在Cocoa中使用 NSDate 类。在 NSDate 中实现了一种方便的方法来获取两个日期实例之间的秒数( timeIntervalSinceDate: / a>。这是调用一个 NSDate 实例,另一个 NSDate 对象作为参数。它返回一个 NSTimeInterval (这是double的typedef),代表两个日期之间的秒数。



鉴于此,将您以上给出的代码适配为Objective-C / Cocoa上下文是相当简单的。由于由 NSDate 计算的增量由秒给出,给定两个日期,您可以轻松调整上述代码:

  //常量
#define SECOND 1
#define MINUTE(60 * SECOND)
#define HOUR(60 * MINUTE)
#定义DAY(24 * HOUR)
#define MONTH(30 * DAY)

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

if(delta< 1 * MINUTE)
{
return delta == 1? @前一秒:[NSString stringWithFormat:@%d seconds ago,(int)delta];
}
if(delta< 2 * MINUTE)
{
return @一分钟前;
}
if(delta< 45 * MINUTE)
{
int minutes = floor((double)delta / MINUTE);
return [NSString stringWithFormat:@%d分钟前,分钟];
}
if(delta< 90 * MINUTE)
{
return @一小时前;
}
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);
返回月份< = 1? @一个月前:[NSString stringWithFormat:@%d个月前,months];
}
else
{
int years = floor((double)delta / MONTH / 12.0);
return years< = 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:

How can relative time be calculated in C#?

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天全站免登陆