ここで何かが足りませんか?Appleが提供する方法は、マシンのタイムゾーンのデフォルトや設定内容に関係なく、UTCでのみ機能するようです。
これが私が得る出力です:
Output:
2013-02-01 10:41:24.152 Scratch[17640:c07] cal=gregorian, cal.timeZone=America/Los_Angeles (PST) offset -28800
2013-02-01 10:41:24.154 Scratch[17640:c07] date_Feb1_1400PST=2013-02-01 14:00 -0800
2013-02-01 10:41:24.156 Scratch[17640:c07] date_Feb2_1200PST=2013-02-02 12:00 -0800
2013-02-01 10:41:24.157 Scratch[17640:c07] midnights between=1
2013-02-01 10:41:24.158 Scratch[17640:c07] and then...
2013-02-01 10:41:24.159 Scratch[17640:c07] date_Feb1_2000PST=2013-02-01 22:00 -0800
2013-02-01 10:41:24.161 Scratch[17640:c07] date_Feb2_1000PST=2013-02-02 10:00 -0800
2013-02-01 10:41:24.161 Scratch[17640:c07] midnights between=0
私が本当に知りたいのは、特定のタイムゾーン(ローカルまたはその他、必ずしもUTCではない)の2日間の「真夜中の数」(つまり、暦日数の差)です。
これは非常に一般的でかなり単純な質問のように思われるので、私はどれほど厄介で理解するのが難しいかを見て驚いています。
私は「mod86400」またはそのような不潔なものを含む答えを探していません。フレームワークは、これを真剣に教えてくれるはずです。
- (void)doDateComparisonStuff {
NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
cal.timeZone = [NSTimeZone timeZoneWithName:@"America/Los_Angeles"];
NSLog(@"cal=%@, cal.timeZone=%@", cal.calendarIdentifier, cal.timeZone);
NSDate *date_Feb1_1400PST = [self dateFromStr:@"20130201 1400"];
NSLog(@"date_Feb1_1400PST=%@", [self stringFromDate:date_Feb1_1400PST]);
NSDate *date_Feb2_1200PST = [self dateFromStr:@"20130202 1200"];
NSLog(@"date_Feb2_1200PST=%@", [self stringFromDate:date_Feb2_1200PST]);
NSLog(@"midnights between=%d", [self daysWithinEraFromDate:date_Feb1_1400PST toDate:date_Feb2_1200PST usingCalendar:cal]);
NSLog(@"and then...");
NSDate *date_Feb1_2000PST = [self dateFromStr:@"20130201 2200"];
NSLog(@"date_Feb1_2000PST=%@", [self stringFromDate:date_Feb1_2000PST]);
NSDate *date_Feb2_1000PST = [self dateFromStr:@"20130202 1000"];
NSLog(@"date_Feb2_1000PST=%@", [self stringFromDate:date_Feb2_1000PST]);
NSLog(@"midnights between=%d", [self daysWithinEraFromDate:date_Feb1_2000PST toDate:date_Feb2_1000PST usingCalendar:cal]);
}
// based on "Listing 13" at
// https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/DatesAndTimes/Articles/dtCalendricalCalculations.html#//apple_ref/doc/uid/TP40007836-SW1
- (NSInteger)daysWithinEraFromDate:(NSDate *)startDate toDate:(NSDate *)endDate usingCalendar:(NSCalendar *)cal
{
NSInteger startDay=[cal ordinalityOfUnit:NSDayCalendarUnit
inUnit: NSEraCalendarUnit forDate:startDate];
NSInteger endDay=[cal ordinalityOfUnit:NSDayCalendarUnit
inUnit: NSEraCalendarUnit forDate:endDate];
return endDay-startDay;
}
- (NSDate *)dateFromStr:(NSString *)dateStr {
NSDateFormatter *df = nil;
df = [[NSDateFormatter alloc] init];
df.timeZone = [NSTimeZone timeZoneWithName:@"America/Los_Angeles"];
df.dateFormat = @"yyyyMMdd HHmm";
return [df dateFromString:dateStr];
}
- (NSString *)stringFromDate:(NSDate *)date {
NSDateFormatter *df = nil;
df = [[NSDateFormatter alloc] init];
df.timeZone = [NSTimeZone timeZoneWithName:@"America/Los_Angeles"]; // native timezone here
df.dateFormat = @"yyyy-MM-dd HH:mm Z";
return [df stringFromDate:date];
}