简体   繁体   English

NSDate独立于时区?

[英]NSDate independent of timezone?

I have an app that displays a timetable of certain ferry trips. 我有一个应用程序,显示某些渡轮旅行的时间表。

If I travel to a different timezone - say 4 hours behind, a 10am ferry trip now shows up as 6am? 如果我前往不同的时区 - 比如说落后4个小时,那么上午10点的轮渡旅行现在显示为早上6点?

I know this has got to do with how dates are treated based on their timezones, but I can't work out how to change that behaviour. 我知道这与如何根据时区处理日期有关,但我无法弄清楚如何改变这种行为。

At the moment here's how I am getting the date and displaying it on a UILabel: 目前,这是我如何获取日期并在UILabel上显示:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"HH:mm"];
[self.departureTime setText:[dateFormatter stringFromDate:[self.route objectForKey:@"departureTime"]]];
[self.arrivalTime setText:[dateFormatter stringFromDate:[self.route objectForKey:@"arrivalTime"]]];
[dateFormatter release];

Thanks in advance for your help. 在此先感谢您的帮助。

You'll need to store the timezone that the ferry ride is taking place in and format it for that timezone. 您需要存储渡轮的时区,并为该时区格式化。

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"HH:mm"]; 

NSDate *now = [NSDate date];   
NSLog(@"now:%@", [dateFormatter stringFromDate:now]);

NSTimeZone *timeZone = [NSTimeZone timeZoneForSecondsFromGMT:(-8 * 3600)];     
[dateFormatter setTimeZone:timeZone];
NSLog(@"adjusted for timezone: %@", [dateFormatter stringFromDate:now]);

Outputs: 输出:

2011-10-10 20:42:23.781 Craplet[2926:707] now:20:42
2011-10-10 20:42:23.782 Craplet[2926:707] adjusted for timezone: 16:42

You have seen NSDateFormatter's setTimeZone method, yes? 您已经看过NSDateFormatter的setTimeZone方法,是吗?

http://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html#//apple_ref/occ/instm/NSDateFormatter/setTimeZone : http://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html#//apple_ref/occ/instm/NSDateFormatter/setTimeZone

(btw, I'd be amazed if there was a ferry that involved crossing four time zones; sounds like a cruise ship itinerary to me) (顺便说一下,如果有一艘渡轮涉及越过四个时区,我会感到很惊讶;听起来像是一艘游轮给我的行程)

You can also use the NSDateComponents class as described by apple's reference: 您还可以使用Apple引用中描述的NSDateComponents类:

If you need to create a date that is independent of timezone, you can store the date as an NSDateComponents object—as long as you store some reference to the corresponding calendar. 如果需要创建独立于时区的日期,则可以将日期存储为NSDateComponents对象 - 只要存储对相应日历的某些引用即可。

In iOS, NSDateComponents objects can contain a calendar, a timezone, and a date object. 在iOS中,NSDateComponents对象可以包含日历,时区和日期对象。 You can therefore store the calendar along with the components. 因此,您可以将日历与组件一起存储。 If you use the date method of the NSDateComponents class to access the date, make sure that the associated timezone is up-to-date. 如果使用NSDateComponents类的date方法来访问日期,请确保关联的时区是最新的。

https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/DatesAndTimes/Articles/dtTimeZones.html#//apple_ref/doc/uid/20000185-SW1 https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/DatesAndTimes/Articles/dtTimeZones.html#//apple_ref/doc/uid/20000185-SW1

Don't confuse an NSDate value with a formatted output like NSLog. 不要将NSDate值与NSLog等格式化输出混淆。 NSDate is GMT, Apple's docs: NSDate是GMT,Apple的文档:

The sole primitive method of NSDate, timeIntervalSinceReferenceDate, provides the basis for all the other methods in the NSDate interface. NSDate的唯一原始方法timeIntervalSinceReferenceDate为NSDate接口中的所有其他方法提供了基础。 This method returns a time value relative to an absolute reference date—the first instant of 1 January 2001, GMT. 此方法返回相对于绝对参考日期的时间值 - 格林威治标准时间2001年1月1日的第一个瞬间。

NSTimeInterval referenceInterval = [[dateFormatter dateFromString:@"1 January 2001 GMT"] timeIntervalSinceReferenceDate];
NSLog(@"referenceInterval: %f", referenceInterval);

NSTimeInterval estInterval = [[dateFormatter dateFromString:@"1 January 2001 EST"] timeIntervalSinceReferenceDate];
NSLog(@"estInterval:       %f", estInterval);

Output: 输出:

referenceInterval: 0.000000  
estInterval:   18000.000000
        NSDate *currentDateTime =  datePicker.date;
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"EEE,MM-dd-yyyy HH:mm:ss"];
        NSString *dateInStringFormated = [dateFormatter stringFromDate:currentDateTime];
        NSLog(@"%@", dateInStringFormated);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM