How to convert a Python datetime.datetime.utctime() to an Obj-C NSDate, and back again?
编辑:基本上,我希望在UTC时间内完成这项工作,最好是通过ISO-8601:
这看起来应该很简单,但我似乎想不出来。
python代码,转换为字符串:
1 2 3 | >>> import datetime >>> datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S %z") '2012-03-08 00:07:31 ' |
请注意,时区信息
1 | '2012-03-08 00:07:31 +0000' |
号
在OBJ-C方面:
1 2 3 4 5 6 7 8 9 | // This fails and prints (null) since the timezone is missing. NSString *pythonDate1 = @"2012-03-07 23:51:58"; NSDate *objCDate1 = [NSDate dateWithString:pythonDate1]; NSLog(@"%@", objCDate1); // This works, manually adding in the"+0000". NSString *pythonDate2 = @"2012-03-07 23:51:58 +0000"; NSDate *objCDate2 = [NSDate dateWithString:pythonDate2]; NSLog(@"%@", objCDate2); |
打印输出:
1 2 | 2012-03-07 19:14:47.848 Untitled 3[3912:707] (null) 2012-03-07 19:14:47.849 Untitled 3[3912:707] 2012-03-07 23:51:58 +0000 |
。
我也不太确定如何从nsdate返回到datetime.datetime对象。非常感谢您的帮助!:)
python datetime到iso-8601字符串:
1 2 3 4 5 6 | >>> from datetime import datetime >>> now = datetime.utcnow() >>> string = now.strftime("%Y-%m-%dT%H:%M:%S +0000") # Manually specifying ' +0000' since we know we have UTC time. >>> >>> string '2012-03-08T21:19:26 +0000' |
ISO-8601字符串到python日期时间:
1 2 3 4 5 6 | >>> from datetime import datetime >>> string ="2012-03-08T21:19:26 +0000" >>> time = datetime.strptime(string,"%Y-%m-%dT%H:%M:%S +0000") # Only works for UTC time. >>> >>> time datetime.datetime(2012, 3, 8, 21, 18, 31) |
。
NSDATE到ISO-8601 NSstring:
1 2 3 4 5 6 7 8 9 10 11 12 | NSDate *now = [NSDate date]; NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; NSLocale *locale = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease]; NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"UTC"]; [dateFormatter setLocale:locale]; // Using"en_US" for locale ?eliminates/reduces? issues with systems running different locales. [dateFormatter setTimeZone:timeZone]; [dateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4]; // Explicitly re-stating default behavior for 10.4+. [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss Z"]; NSString *iso8601String = [dateFormatter stringFromDate:now]; NSLog(@"%@", iso8601String); 2012-03-09T02:44:37 +0000 |
。
ISO-8601 nsstring到nsdate:
1 2 3 4 5 6 7 8 9 10 | NSString *iso8601String = @"2012-03-09T20:37:49 +0000"; NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; NSLocale *locale = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease]; [dateFormatter setLocale:locale]; // Using"en_US" for locale ?eliminates/reduces? issues with systems running different locales. [dateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4]; // Explicitly re-stating default behavior for 10.4+. [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss Z"]; NSDate *date = [dateFormatter dateFromString:iso8601String]; NSLog(@"%@", date); 2012-03-09 20:37:49 +0000 |
如果您真的想要当地时间:
1 2 3 4 5 6 7 | import datetime from pytz import timezone ET = timezone('US/Eastern') now = datetime.datetime.now(ET) now.strftime("%Y-%m-%d %H:%M:%S %z") |
如果您有一个数据库或使用UTC的东西,那么:
1 2 | now = datetime.datetime.utcnow() ET.localize(now).strftime("%Y-%m-%d %H:%M:%S %z") |
号
使用
如果您不想处理时区对象,并且您的时间是在UTC中,为什么不将定义zulu/zero的"z"附加到ISO-8601中定义的末尾呢?
1 2 | >>> datetime.datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%SZ") '2012-03-08T00:07:31Z' |
除非obj-c不支持iso-8601格式…
或者,如果您仍然只使用utcnow(),您可以作弊,只需添加+00:00:
1 2 | >>> datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S +00:00") '2012-03-08 00:07:31 +00:00' |
。