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, 12 Jun 2011 1:54 am CDT - 6:33 am - 8:34 pm
Sunrise: 2011-06-11 21:33:00 +0000
Sunset: 2011-06-12 11 :34:00 +0000
当前: 2011-06-12 01:54:00 +0000
上升:6 <正确
设置:20 <正确
时间:10<错误的
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);
I'd expect this to work. However, look at the output below:
Sun, 12 Jun 2011 1:54 am CDT - 6:33 am - 8:34 pm
Sunrise: 2011-06-11 21:33:00 +0000
Sunset: 2011-06-12 11:34:00 +0000
Current: 2011-06-12 01:54:00 +0000
Rise: 6 < correct
Set: 20 < correct
Time: 10 < wrong
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试在格式化程序和日历上将时区设置为 GMT-0。和。您可能会感到惊喜。
Try setting the timezone to GMT-0 on both the formatter and the calendar. You might be pleasantly surprised.