NSDate搞砸了我的约会!错误的时区?

时间:2011-06-12 07:30:45

标签: iphone nsdate

NSLog(@"%@ - %@ - %@",currTime,sunrise,sunset);
NSString *curtime = [[currTime substringToIndex:(currTime.length-4)] stringByAppendingString:@"-0000"];
NSDateFormatter *formatter1 = [[NSDateFormatter alloc]init];
NSLocale *locale = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]autorelease];
[formatter1 setDateFormat:@"EEE, dd MMM yyyy h:mm aZ"];
[formatter1 setLocale:locale];

NSDate *time = [formatter1 dateFromString:curtime];
[formatter1 setDateFormat:@"EEE dd MMM yyyy"];
NSString *zone = [formatter1 stringFromDate:time];
NSString *r = [sunrise stringByAppendingString:zone];
NSString *s = [sunset stringByAppendingString:zone];

[formatter1 setDateFormat:@"hh:mm aEEE dd MMM yyyy"];
NSDate *rise = [formatter1 dateFromString:r];
NSDate *set = [formatter1 dateFromString:s];

unsigned int flags = NSHourCalendarUnit | NSMinuteCalendarUnit | NSDayCalendarUnit;
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components1 = [calendar components:flags fromDate:rise];
NSDateComponents *components2 = [calendar components:flags fromDate:set];
NSDateComponents *components3 = [calendar components:flags fromDate:time];

NSInteger riseHour = [components1 hour];
NSInteger setHour = [components2 hour];
NSInteger timeHour = [components3 hour];

NSLog(@"\nSunrise: %@ \nSunset: %@ \nCurrent: %@",rise,set,time);
NSLog(@"Rise: %i  Set: %i  Time: %i",riseHour,setHour,timeHour);

我希望这可行。但是,请看下面的输出:

Sun,2011年6月12日上午1:54 CDT - 上午6:33 - 晚上8:34

日出:2011-06-11 21:33:00 +0000

日落:2011-06-12 11:34:00 +0000

Current:2011-06-12 01:54:00 +0000

上升:6<正确

设定:20<正确

时间:10<错了

1 个答案:

答案 0 :(得分:3)

尝试在格式化程序和日历上将时区设置为GMT-0。你可能会感到惊喜。

[NSTimeZone timeZoneForSecondsFromGMT:0]