date
+ (NSString*)srtringForDate:(NSDate*)date{
#define IS_FUTURE_THRESHOLD -300
enum DateDisplayFormat {
DateDisplayFormatRelative,
DateDisplayFormatAbsolute,
DateDisplayFormatTwitterGuideline,
DateDisplayFormatError
};
NSInteger sec = (int)[[NSDate date] timeIntervalSinceDate:date];
int casenum;
if(sec < IS_FUTURE_THRESHOLD) {
casenum = DateDisplayFormatError;
} else {
casenum = DateDisplayFormatRelative;
}
switch (casenum)
{
case DateDisplayFormatRelative: {
//相対日時
if (sec < 60) {
return NSLocalizedString(@"just now",nil);
} else if (sec < 120) {
return NSLocalizedString(@"1 minute ago",nil);
} else if (sec < 3600) {
return [NSString stringWithFormat:@"%d%@",sec / 60, NSLocalizedString(@" minutes ago",nil)];
} else if (sec < 7200) {
return NSLocalizedString(@"1 hour ago",nil);
} else if (sec < 86400) {
return [NSString stringWithFormat:@"%d%@",sec / 3600, NSLocalizedString(@" hours ago",nil)];
} else if (sec < 172800) {
return NSLocalizedString(@"1 day ago",nil);
} else {
return [NSString stringWithFormat:@"%d%@",sec / 86400, NSLocalizedString(@" days ago",nil)];
}
} case DateDisplayFormatAbsolute: {
//絶対日時
if (sec < 60){
return NSLocalizedString(@"just now",nil);
}
NSDateFormatter *formatterHour = [[NSDateFormatter alloc] init];
[formatterHour setDateFormat:@"HH:mm"];
NSDateFormatter *formatterDay = [[NSDateFormatter alloc] init];
[formatterDay setDateFormat:@"MM/dd(E)"];
if ([[formatterDay stringFromDate:date] isEqualToString:[formatterDay stringFromDate:[NSDate date]]]){
return [NSString stringWithFormat:@"%@%@", NSLocalizedString(@"Today ",nil), [formatterHour stringFromDate:date]];
} else if ([[formatterDay stringFromDate:date] isEqualToString:[formatterDay stringFromDate:[NSDate dateWithTimeIntervalSinceNow:-86400]]]){
return [NSString stringWithFormat:@"%@%@", NSLocalizedString(@"Yesterday ",nil), [formatterHour stringFromDate:date]];
} else {
return [NSString stringWithFormat:@"%@ %@", [formatterDay stringFromDate:date], [formatterHour stringFromDate:date]];
}
} case DateDisplayFormatTwitterGuideline: {
if (sec < 60) {
return (@"now");
} else if (sec < 3600) {
return [NSString stringWithFormat:@"%d%@",sec / 60, NSLocalizedString(@"m",nil)];
} else if (sec < 86400) {
return [NSString stringWithFormat:@"%d%@",sec / 3600, NSLocalizedString(@"h",nil)];
}
if([NSLocalizedString(@"Language",nil) isEqualToString:@"ja_JP"]) {
NSDateFormatter *formatterDay = [[NSDateFormatter alloc] init];
[formatterDay setDateFormat:@"MM/dd"];
return [formatterDay stringFromDate:date];
}
NSDateFormatter *formatterMonth = [[NSDateFormatter alloc] init];
[formatterMonth setDateFormat:@"MM"];
NSDateFormatter *formatterDay = [[NSDateFormatter alloc] init];
[formatterDay setDateFormat:@"dd"];
return [NSString stringWithFormat:@"%@ %@",
[self monthDigitStringToMonthString : [formatterMonth stringFromDate:date]],
[formatterDay stringFromDate:date]
];
} default: {
//Error or DateDisplayFormatError
NSDateFormatter *formatterHour = [[NSDateFormatter alloc] init];
[formatterHour setDateFormat:@"HH:mm"];
NSDateFormatter *formatterDay = [[NSDateFormatter alloc] init];
[formatterDay setDateFormat:@"MM/dd(E)"];
return [NSString stringWithFormat:@"%@ %@", [formatterDay stringFromDate:date], [formatterHour stringFromDate:date]];
}
}
NSLog(@"ERROR : DateFormat is missing.");
return @"";
}
+ (NSString *)monthDigitStringToMonthString:(NSString *)monthDigitString
{
NSDictionary *month = @{
@"01" : @"Jan", @"02" : @"Feb", @"03" : @"Mar", @"04" : @"Apr",
@"05" : @"May", @"06" : @"Jun", @"07" : @"Jul", @"08" : @"Aug",
@"09" : @"Sep", @"10" : @"Oct", @"11" : @"Nov", @"12" : @"Dec"
};
NSString *ret = [month objectForKey:monthDigitString];
if (!ret) {
NSLog(@"Month %@",monthDigitString);
NSAssert(NO, @"DateFormatError");
ret = @"Err";
}
return ret;
}