我想我想通了:计算从纪元以来的秒数,然后使用time.localtime转换为本地timzeone,然后将时间结构转换回datetime …
EPOCH_DATETIME = datetime.datetime(1970,1,1)SECONDS_PER_DAY = 24*60*60def utc_to_local_datetime( utc_datetime ): delta = utc_datetime - EPOCH_DATETIME utc_epoch = SECONDS_PER_DAY * delta.days + delta.seconds time_struct = time.localtime( utc_epoch ) dt_args = time_struct[:6] + (delta.microseconds,) return datetime.datetime( *dt_args )
它正确地应用了夏/夏DST:
>>> utc_to_local_datetime( datetime.datetime(2010, 6, 6, 17, 29, 7, 730000) )datetime.datetime(2010, 6, 6, 19, 29, 7, 730000)>>> utc_to_local_datetime( datetime.datetime(2010, 12, 6, 17, 29, 7, 730000) )datetime.datetime(2010, 12, 6, 18, 29, 7, 730000)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)