Python日期时间与iPhone日期不同

Python datetime different from iPhone date

在Google App Engine中,我有一个带有(auto_now_add=True)的datetime属性。在iPhone上的Objective-C中,获得当前时间使我成为2012-04-17 6:55:01,而在Python上,获得auto_now_add使我成为2012-04-17 11:55:01

6:55是我的本地时间,我不确定Python日期位于哪个时区或格式。如何通过Python或Objective-C两者匹配?

编辑:显然,Python日期是UTC格式。但是我的iPhone日期是哪种格式?我以为[NSDate date]也是UTC吗?为什么我得到不同的结果?

编辑2:知道了。需要在Objective-C

中执行此操作

1
2
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
[formatter setTimeZone:timeZone];

这是因为应用引擎始终在UTC时间运行,因此您的手机可能处于不同的时区。

The runtime's TZ environment variable is set to UTC, and can't be changed. Timestamps returned by e.g. time.time() and datetime.datetime.now() will always be in UTC. Similarly, datetime properties in the datastore will always be stored and returned as UTC.

You can change the time zone of a datetime in memory with the astimezone() method.

来源