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

发布于 2024-11-15 04:42:45 字数 1713 浏览 4 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雨的味道风的声音 2024-11-22 04:42:45

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

[NSTimeZone timeZoneForSecondsFromGMT:0]

Try setting the timezone to GMT-0 on both the formatter and the calendar. You might be pleasantly surprised.

[NSTimeZone timeZoneForSecondsFromGMT:0]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文