Oli*_*ver 4 iphone time cocoa datetime ios
我测试了那些电话:
NSLog(@"%@", [NSDate date]);
NSLog(@"%@", [NSDate convertToUTC:[NSDate date]]);
NSLog(@"%@", [[NSDate date] stringValueWithFormat:@"yyyyMMddHHmmss"]);
NSLog(@"%@", [[NSDate convertToUTC:[NSDate date]] stringValueWithFormat:@"yyyyMMddHHmmss"]);
+ (NSDate*) convertToUTC:(NSDate*)sourceDate {
[NSTimeZone resetSystemTimeZone];
NSTimeZone* currentTimeZone = [NSTimeZone localTimeZone];
NSTimeZone* utcTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];
NSInteger currentGMTOffset = [currentTimeZone secondsFromGMTForDate:sourceDate];
NSInteger gmtOffset = [utcTimeZone secondsFromGMTForDate:sourceDate];
NSTimeInterval gmtInterval = gmtOffset - currentGMTOffset;
NSDate* destinationDate = [[[NSDate alloc] initWithTimeInterval:gmtInterval sinceDate:sourceDate] autorelease];
return destinationDate;
}
- (NSString*) stringValueWithFormat:(NSString*)formatRetour {
NSDateFormatter* dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:formatRetour];
return [dateFormatter stringFromDate:self];
}
Run Code Online (Sandbox Code Playgroud)
他们给我:
2011-08-17 13:03:58 +0000
2011-08-17 11:03:58 +0000
20110817150358
20110817130358
Run Code Online (Sandbox Code Playgroud)
在我的手机上,所有配置为法国/法国,现在是:15:03:58.
我不明白这里完成的转换.
1给我GMT
2 的实时时间给我一些我不明白的东西......
3给出了手机上的实时时间
4给出了GMT的实时时间
我迷路了... 2从哪里来?这些电话如何运作?
我的意思是,[NSDate日期]给出了13:03和+0000时区.为什么格式化显示15:03?
为什么1和2显示+0000时区,但时间不同?
你能指导我一下吗?
NSLog 将始终使用+0000时区打印日期对象的描述.
当您使用a NSDateFormatter来获取日期中的字符串时,它将使用手机的时区.随着"yyyyMMddHHmmssZZZ"你会得到20110817150358+0200.
对于转换为UTC的日期,情况相同:GMT + 0(NSLog)获得11:03,与13:03 + 0200相同(使用NSDateFormatter).
然而convertToUTC:,因为价值应该是13:03 + 0000,所以有问题.
NSDate到NSDate的转换方法没有意义.NSDate拥有绝对的时间而不关心时区.从NSDate转换执行NSString时,您只需要考虑时区.
这就是为什么当你"convertToUTC"15:03 + 0200时你会得到13:03 + 0200,这与11:03 + 0000相同.
- (NSString *)UTCRepresentation虽然这样的方法很有意义.
| 归档时间: |
|
| 查看次数: |
1310 次 |
| 最近记录: |