NSDate+FormatterAdditions.m 17.2 KB
Newer Older
Sandy's avatar
Sandy committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
//
//  NSDate+FormatterAdditions.m
//  JobTalk
//
//  Created by Xummer on 14-5-16.
//  Copyright (c) 2014年 BST. All rights reserved.
//

#import "NSDate+FormatterAdditions.h"
#include <time.h>
#include <xlocale.h>

#define JTLocalizedString(key) [[NSBundle mainBundle] localizedStringForKey:(key) value:@"" table:nil]

#define ISO8601_MAX_LEN 25

@implementation NSDate (FormatterAdditions)
- (NSString *)yearString {
    NSString *strDate = [self yearMonthDayString];
    NSArray *arrDate = [strDate componentsSeparatedByString:@"-"];
    return arrDate[0];
}
- (NSString *)monthString {
    NSString *strDate = [self yearMonthDayString];
    NSArray *arrDate = [strDate componentsSeparatedByString:@"-"];
    return arrDate[1];
}
- (NSString *)dayString{
    NSString *strDate = [self yearMonthDayString];
    NSArray *arrDate = [strDate componentsSeparatedByString:@"-"];
    return arrDate[2];
}

- (NSString *)yearWeekString {
    NSInteger week = [self getWeekOfYear];
    NSString  *year = [self yearString];
    return  [NSString stringWithFormat:@"%@-%ld",year, (long)week];
}

+ (NSDate *)dateFromISO8601String:(NSString *)iso8601 {
	if (!iso8601) {
        return nil;
    }
	
    const char *str = [iso8601 cStringUsingEncoding:NSUTF8StringEncoding];
    char newStr[ISO8601_MAX_LEN];
    bzero(newStr, ISO8601_MAX_LEN);
	
    size_t len = strlen(str);
    if (len == 0) {
        return nil;
    }
	
    // UTC dates ending with Z
    if (len == 20 && str[len - 1] == 'Z') {
        memcpy(newStr, str, len - 1);
        strncpy(newStr + len - 1, "+0000\0", 6);
    }
	
    // Timezone includes a semicolon (not supported by strptime)
    else if (len == 25 && str[22] == ':') {
        memcpy(newStr, str, 22);
        memcpy(newStr + 22, str + 23, 2);
    }
	
    // Fallback: date was already well-formatted OR any other case (bad-formatted)
    else {
        memcpy(newStr, str, len > ISO8601_MAX_LEN - 1 ? ISO8601_MAX_LEN - 1 : len);
    }
	
    // Add null terminator
    newStr[sizeof(newStr) - 1] = 0;
    
    struct tm tm = {
        .tm_sec = 0,
        .tm_min = 0,
        .tm_hour = 0,
        .tm_mday = 0,
        .tm_mon = 0,
        .tm_year = 0,
        .tm_wday = 0,
        .tm_yday = 0,
        .tm_isdst = -1,
    };
	
    if (strptime_l(newStr, "%FT%T%z", &tm, NULL) == NULL) {
        return nil;
    }
    
    return [NSDate dateWithTimeIntervalSince1970:mktime(&tm)];
}


- (NSString *)ISO8601String {
	struct tm *timeinfo;
	char buffer[80];
	
	time_t rawtime = (time_t)[self timeIntervalSince1970];
	timeinfo = gmtime(&rawtime);
	
	strftime(buffer, 80, "%Y-%m-%dT%H:%M:%SZ", timeinfo);
	
	return [NSString stringWithCString:buffer encoding:NSUTF8StringEncoding];
}

+ (NSString *)stringWithFormatter:(NSString *)dateFormatter andDate:(NSDate *)date {
    if ([dateFormatter length] == 0) {
        return nil;
    }
    
    struct tm *timeinfo;
	char buffer[80];
	
	time_t rawtime = (time_t)[date timeIntervalSince1970];
	timeinfo = gmtime(&rawtime);
	
	strftime(buffer, 80, [dateFormatter UTF8String], timeinfo);
	
	return [NSString stringWithCString:buffer encoding:NSUTF8StringEncoding];
}

- (NSString *)stringWithFormatter:(NSString *)dateFormatter {
    
    if ([dateFormatter length] == 0) {
        return nil;
    }
    
    // Change to Local time zone
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    NSInteger interval = [zone secondsFromGMTForDate:self];
    NSDate *localDate = [self dateByAddingTimeInterval: interval];
    
    return [[self class] stringWithFormatter:dateFormatter andDate:localDate];
}

- (NSString *)chatAttachmentFormatterString {
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    NSInteger interval = [zone secondsFromGMTForDate:self];
    NSDate *localDate = [self dateByAddingTimeInterval: interval];
    
    NSString *dateFormatter = @"%m/%d/%Y %H:%M:%S";
    
    return [[self class] stringWithFormatter:dateFormatter andDate:localDate];
}

- (NSString *)YMDHMSFormatterString {
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    NSInteger interval = [zone secondsFromGMTForDate:self];
    NSDate *localDate = [self dateByAddingTimeInterval: interval];
    
    NSString *dateFormatter = @"%Y%m%d%H%M%S";
    
    return [[self class] stringWithFormatter:dateFormatter andDate:localDate];
}

- (NSString *)chatMsgFormatterString {
    
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    NSInteger interval = [zone secondsFromGMTForDate:self];
    NSDate *localDate = [self dateByAddingTimeInterval: interval];
    
    NSDate *tmpDate = [NSDate date];
    interval = [zone secondsFromGMTForDate:tmpDate];
    NSDate *localCurDate = [tmpDate dateByAddingTimeInterval: interval];
    
    NSString *dateFormatter = @"%Y-%m-%d-%H-%M-%S";
    
    NSString *dateStr = [[self class] stringWithFormatter:dateFormatter andDate:localDate];
    NSString *currentDateStr = [[self class] stringWithFormatter:dateFormatter andDate:localCurDate];
    
    NSArray *dateComponents = [dateStr componentsSeparatedByString:@"-"];
    NSArray *currentDateComponents = [currentDateStr componentsSeparatedByString:@"-"];
    
    // Year
    if (![dateComponents[ 0 ] isEqualToString:currentDateComponents[ 0 ]]) {
        dateFormatter = @"%Y-%m";
    }
    // Month
    else if (![dateComponents[ 1 ] isEqualToString:currentDateComponents[ 1 ]]) {
        dateFormatter = @"%m-%d";
    }
    // Day
    else if (![dateComponents[ 2 ] isEqualToString:currentDateComponents[ 2 ]]) {
        dateFormatter = @"%m-%d";
    }
    // Min
    else if (![dateComponents[ 3 ] isEqualToString:currentDateComponents[ 3 ]]) {
        dateFormatter = @"%H:%M";
    }
    // Sec
    else {
        dateFormatter = @"%H:%M";
    }
    
    return [[self class] stringWithFormatter:dateFormatter andDate:localDate];
}

- (NSString *)briefTimeInWords {
	NSTimeInterval seconds = fabs([self timeIntervalSinceNow]);
	
	static NSNumberFormatter *numberFormatter = nil;
	static dispatch_once_t onceToken;
	dispatch_once(&onceToken, ^{
		numberFormatter = [[NSNumberFormatter alloc] init];
		numberFormatter.numberStyle = NSNumberFormatterCurrencyStyle;
		numberFormatter.currencySymbol = @"";
		numberFormatter.maximumFractionDigits = 0;
	});
	
	// Seconds
	if (seconds < 60.0) {
		if (seconds < 2.0) {
			return JTLocalizedString(@"1s");
		}
		return [NSString stringWithFormat:JTLocalizedString(@"%ds"), (NSInteger)seconds];
	}
	
	NSTimeInterval minutes = round(seconds / 60.0);
	
	// Minutes
	if (minutes >= 0.0 && minutes < 60.0) {
		if (minutes < 2.0) {
			return JTLocalizedString(@"1m");
		}
		return [NSString stringWithFormat:JTLocalizedString(@"%dm"), (NSInteger)minutes];
	}
	
	// Hours
	else if (minutes >= 60.0 && minutes < 1440.0) {
		NSInteger hours = (NSInteger)round(minutes / 60.0);
		if (hours < 2) {
			return JTLocalizedString(@"1h");
		}
		return [NSString stringWithFormat:JTLocalizedString(@"%dh"), hours];
	}
	
	// Days
	else if (minutes >= 1440.0 && minutes < 525600.0) {
		NSInteger days = (NSInteger)round(minutes / 1440.0);
		if (days < 2) {
			return JTLocalizedString(@"1d");
		}
		return [NSString stringWithFormat:JTLocalizedString(@"%@d"),
				[numberFormatter stringFromNumber:[NSNumber numberWithInteger:days]]];
	}
	
	// Years
	else if (minutes >= 525600.0) {
		NSInteger years = (NSInteger)round(minutes / 525600.0);
		if (years < 2) {
			return JTLocalizedString(@"1y");
		}
		return [NSString stringWithFormat:JTLocalizedString(@"%@y"),
				[numberFormatter stringFromNumber:[NSNumber numberWithInteger:years]]];
	}
	
	return nil;
}

- (NSString *)localYMDString {
    return [self stringWithFormatter:@"%Y-%m-%d"];
}


- (NSString *)briefTimeForTimeLine {
	NSTimeInterval seconds = fabs([self timeIntervalSinceNow]);
	
	static NSNumberFormatter *numberFormatter = nil;
	static dispatch_once_t onceToken;
	dispatch_once(&onceToken, ^{
		numberFormatter = [[NSNumberFormatter alloc] init];
		numberFormatter.numberStyle = NSNumberFormatterCurrencyStyle;
		numberFormatter.currencySymbol = @"";
		numberFormatter.maximumFractionDigits = 0;
	});
    
	NSTimeInterval minutes = round(seconds / 60.0);
	
	// Minutes
	if (minutes >= 0.0 && minutes < 60.0) {
		if (minutes < 2.0) {
			return JTLocalizedString(@"1m");
		}
		return [NSString stringWithFormat:JTLocalizedString(@"%dm"), (NSInteger)minutes];
	}
	
	// Hours
	else if (minutes >= 60.0 && minutes < 1440.0) {
		NSInteger hours = (NSInteger)round(minutes / 60.0);
		if (hours < 2) {
			return JTLocalizedString(@"1h");
		}
		return [NSString stringWithFormat:JTLocalizedString(@"%dh"), hours];
	}
	
	// in 7 Days
	else if (minutes >= 1440.0 && minutes < 10080.0) {
		NSInteger days = (NSInteger)round(minutes / 1440.0);
		if (days < 2) {
			return JTLocalizedString(@"1d");
		}
		return [NSString stringWithFormat:JTLocalizedString(@"%@d"),
				[numberFormatter stringFromNumber:[NSNumber numberWithInteger:days]]];
	}
	
	// > 7 Days
	else if (minutes >= 10080.0) {
        return [self stringWithFormatter:@"%m/%d/%Y"];
	}
	
	return nil;
}

- (NSString *)httpParameterString {
    return [self stringWithFormatter:@"%Y-%m-%d %H:%M:%S"];
}
- (NSString *)yearMonthDayString {
    return [self stringWithFormatter:@"%Y-%m-%d"];
}
- (NSString *)yearMonthString {
    return [self stringWithFormatter:@"%Y-%m"];
}
- (NSString *)mobileIDDateString {
    return [self stringWithFormatter:@"%y%m%d%H%M%S000"];
}

- (NSDate *)endOfTheDay {
    NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
    NSUInteger preservedComponents = (NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit);
    NSDateComponents *comp = [calendar components:preservedComponents fromDate:self];
    [comp setMinute:59];
    [comp setHour:23];
    
    return [calendar dateFromComponents:comp];
}

- (NSNumber *)timeStampNumber
{
    NSString *timeStampString = [self timeStampStr];
    NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
    [f setNumberStyle:NSNumberFormatterDecimalStyle];
    NSNumber * time = [f numberFromString:timeStampString];
    return time;
}

+ (NSString *)timeInWordsFromTimeInterval:(NSTimeInterval)intervalInSeconds includingSeconds:(BOOL)includeSeconds {
	NSTimeInterval intervalInMinutes = round(intervalInSeconds / 60.0f);
	
	if (intervalInMinutes >= 0 && intervalInMinutes <= 1) {
		if (!includeSeconds) {
			return intervalInMinutes <= 0 ? JTLocalizedString(@"less than a minute") : JTLocalizedString(@"1 minute");
		}
		if (intervalInSeconds >= 0 && intervalInSeconds < 5) {
			return [NSString stringWithFormat:JTLocalizedString(@"less than %d seconds"), 5];
		} else if (intervalInSeconds >= 5 && intervalInSeconds < 10) {
			return [NSString stringWithFormat:JTLocalizedString(@"less than %d seconds"), 10];
		} else if (intervalInSeconds >= 10 && intervalInSeconds < 20) {
			return [NSString stringWithFormat:@"%d seconds", 20];
		} else if (intervalInSeconds >= 20 && intervalInSeconds < 40) {
			return JTLocalizedString(@"half a minute");
		} else if (intervalInSeconds >= 40 && intervalInSeconds < 60) {
			return JTLocalizedString(@"less than a minute");
	 	} else {
			return JTLocalizedString(@"1 minute");
		}
	} else if (intervalInMinutes >= 2 && intervalInMinutes <= 44) {
		return [NSString stringWithFormat:JTLocalizedString(@"%d minutes"), (NSInteger)intervalInMinutes];
	} else if (intervalInMinutes >= 45 && intervalInMinutes <= 89) {
		return JTLocalizedString(@"about 1 hour");
	} else if (intervalInMinutes >= 90 && intervalInMinutes <= 1439) {
		return [NSString stringWithFormat:JTLocalizedString(@"about %d hours"), (NSInteger)round(intervalInMinutes / 60.0f)];
	} else if (intervalInMinutes >= 1440 && intervalInMinutes <= 2879) {
		return JTLocalizedString(@"1 day");
	} else if (intervalInMinutes >= 2880 && intervalInMinutes <= 43199) {
		return [NSString stringWithFormat:JTLocalizedString(@"%d days"), (NSInteger)round(intervalInMinutes / 1440.0f)];
	} else if (intervalInMinutes >= 43200 && intervalInMinutes <= 86399) {
		return JTLocalizedString(@"about 1 month");
	} else if (intervalInMinutes >= 86400 && intervalInMinutes <= 525599) {
		return [NSString stringWithFormat:JTLocalizedString(@"%d months"), (NSInteger)round(intervalInMinutes / 43200.0f)];
	} else if (intervalInMinutes >= 525600 && intervalInMinutes <= 1051199) {
		return JTLocalizedString(@"about 1 year");
	} else {
		return [NSString stringWithFormat:JTLocalizedString(@"over %d years"), (NSInteger)round(intervalInMinutes / 525600.0f)];
	}
	return nil;
}

/** 该日期是该年的第几周 */
- (NSInteger)getWeekOfYear
{
    NSInteger i;
    NSInteger year = [self getYear];
    NSDate *date = [self endOfWeek];
    for (i = 1;[[date dateAfterDay:-7 * i] getYear] == year;i++)
    {
    }
    return i;
}
//获取年
- (NSUInteger)getYear
{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *dayComponents = [calendar components:(NSYearCalendarUnit) fromDate:self];
    return [dayComponents year];
}

//返回当前周的周末
- (NSDate *)endOfWeek {
    NSCalendar *calendar = [NSCalendar currentCalendar];
    // Get the weekday component of the current date
    NSDateComponents *weekdayComponents = [calendar components:NSWeekdayCalendarUnit fromDate:self];
    NSDateComponents *componentsToAdd = [[NSDateComponents alloc] init];
    // to get the end of week for a particular date, add (7 - weekday) days
    [componentsToAdd setDay:(7 - [weekdayComponents weekday])];
    NSDate *endOfWeek = [calendar dateByAddingComponents:componentsToAdd toDate:self options:0];
    //    [componentsToAdd release];
    
    return endOfWeek;
}

//返回day天后的日期(若day为负数,则为|day|天前的日期)
- (NSDate *)dateAfterDay:(NSInteger)day
{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    // Get the weekday component of the current date
    // NSDateComponents *weekdayComponents = [calendar components:NSWeekdayCalendarUnit fromDate:self];
    NSDateComponents *componentsToAdd = [[NSDateComponents alloc] init];
    // to get the end of week for a particular date, add (7 - weekday) days
    [componentsToAdd setDay:day];
    NSDate *dateAfterDay = [calendar dateByAddingComponents:componentsToAdd toDate:self options:0];
    
    return dateAfterDay;
}

- (NSString *)timeInWords {
	return [self timeInWordsIncludingSeconds:YES];
}


- (NSString *)timeInWordsIncludingSeconds:(BOOL)includeSeconds {
	return [[self class] timeInWordsFromTimeInterval:fabs([self timeIntervalSinceNow]) includingSeconds:includeSeconds];
}


+ (NSString *)weekFromeDateString:(NSString *)dateString {
    NSDateFormatter *f = [[NSDateFormatter alloc] init];
    [f setDateFormat:@"yyy-MM-dd"];
    NSDate *date = [f dateFromString:dateString];
    return [self getWeekDayByDate:date];
}

//计算星期几
+ (NSString *)getWeekDayByDate:(NSDate *)date{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *weekComp = [calendar components:NSCalendarUnitWeekday fromDate:date];
    NSInteger weekDayEnum = [weekComp weekday];
    NSString *weekDays = nil;
    switch (weekDayEnum) {
        case 1:
            weekDays = @"星期日";
            break;
        case 2:
            weekDays = @"星期一";
            break;
        case 3:
            weekDays = @"星期二";
            break;
        case 4:
            weekDays = @"星期三";
            break;
        case 5:
            weekDays = @"星期四";
            break;
        case 6:
            weekDays = @"星期五";
            break;
        case 7:
            weekDays = @"星期六";
            break;
        default:
            break;
    }
    return weekDays;
}

@end

@implementation NSDate (DateFormatterAdditions)

- (NSString *)timeStampStr {
    NSTimeInterval curTimeInterval = [self timeIntervalSince1970];
    
    NSString *result = [NSString stringWithFormat:@"%li", (unsigned long)curTimeInterval];
    
    while ([result length] < 13) {
        result = [result stringByAppendingString:@"0"];
    }
    
    return result;
}

+ (NSString *)curTimeStamp {
    return [[NSDate date] timeStampStr];
}

@end


@implementation NSDate (DateCalculate)
- (NSDate *)dateAddHourse: (NSUInteger)hourse {
    NSTimeInterval curTimeInterval = [self timeIntervalSince1970];
    NSTimeInterval totalTimeInterval = curTimeInterval + hourse * 3600;
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:totalTimeInterval];
    return date;
}
@end



@implementation NSNumber (DateFormatterAdditions)

- (NSDate *)dateValue {
    if (self) {
        return [NSDate dateWithTimeIntervalSince1970:[self integerValue]];
    }
    else {
        return nil;
    }
}

- (NSDate *)overZeroDateValue {
    if (self) {
        NSInteger iSelf = [self integerValue];
        if (iSelf > 0) {
            return [NSDate dateWithTimeIntervalSince1970:iSelf];
        }
        else {
            return nil;
        }
    }
    else {
        return nil;
    }
}

@end

@implementation NSString (DateFormatterAdditions)

- (NSTimeInterval)timeStamp {
    if (self.length > 10) {
        return [[self substringToIndex:10] integerValue];
    }
    else {
        return [self integerValue];
    }
}

- (NSDate *)dateValue {
    if (self) {
        return [NSDate dateWithTimeIntervalSince1970:[self timeStamp]];
    }
    else {
        return nil;
    }
}

- (NSDate *)overZeroDateValue {
    if (self) {
        NSInteger iSelf = [self timeStamp];
        if (iSelf > 0) {
            return [NSDate dateWithTimeIntervalSince1970:iSelf];
        }
        else {
            return nil;
        }
    }
    else {
        return nil;
    }
}

@end