在解析不同时区的日期时,我注意到NSDateFormatter在处理过去的日期时会产生奇怪的结果.
我有以下代码:
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
[dateFormatter setLocale:usLocale];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
NSString* dateString1 = @"1890-09-30 12:00";
NSString* dateString2 = @"1890-10-01 12:00";
NSLog(@"original timezone %@", [dateFormatter timeZone]);
NSDate* localDate1 = [dateFormatter dateFromString:dateString1];
NSDate* localDate2 = [dateFormatter dateFromString:dateString2];
NSLog(@"localDate1 %@ » %@", dateString1, localDate1);
NSLog(@"localDate2 %@ » %@", dateString2, localDate2);
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
NSDate* utcDate1 = [dateFormatter dateFromString:dateString1];
NSDate* utcDate2 = [dateFormatter dateFromString:dateString2];
NSLog(@"utcDate1 %@ » %@", dateString1, utcDate1);
NSLog(@"utcDate2 %@ » %@", dateString2, utcDate2);
Run Code Online (Sandbox Code Playgroud)
我期待localDates比UTC时间少一整小时,因为我的时区是CET.相反,我得到了这个:
original timezone Europe/Budapest (CEST) offset 7200 (Daylight)
localDate1 1890-09-30 12:00 » 1890-09-30 10:43:40 +0000
localDate2 1890-10-01 12:00 » 1890-10-01 11:00:00 +0000
utcDate1 1890-09-30 12:00 » 1890-09-30 12:00:00 +0000
utcDate2 1890-10-01 12:00 » 1890-10-01 12:00:00 +0000
Run Code Online (Sandbox Code Playgroud)
经过一些调查后,似乎早于1890-09-30 23:43的日期出现了这个漏洞.
这是什么原因?这是iOS日期处理中的错误吗?
精彩的时区世界:
当地标准时间即将到达1890年10月1日星期一时,00:00:00时钟倒退0:16:20至星期日,1890年9月30日,当地标准时间23:43:40
http://www.timeanddate.com/worldclock/clockchange.html?n=50&year=1890
| 归档时间: |
|
| 查看次数: |
456 次 |
| 最近记录: |