PyCon 2011 - Hidden Treasures of the Python Standard Library - 获得夏时令标识符

 

本作品采用知识共享署名-非商业性使用-相同方式共享 3.0 Unported许可协议进行许可。允许非商业转载,但应注明作者及出处。


作者:liuyuan_jq

2011-03-30

 

 

 

datetime.datetime -> struct_time

 

 

for dt in [ datetime.datetime(2011, 2, 9, 9, 0), datetime.datetime(2011, 9, 30, 9, 0), ]: # Convert datetime -> struct_time time_t = time.struct_time(dt.timetuple()) 

struct_time -> seconds -> struct_time

 

 

# Round-trip: struct_time -> seconds -> struct_time local_time_t = time.localtime(time.mktime(time_t)) 

获得夏时令标识符

 

 

# The answer is in tm_isdst # 夏令时标识符 # 实行夏令时的时候,tm_isdst 为 正。 # 不实行夏令时的进候,tm_isdst 为 0 # 不了解情况时,tm_isdst()为负。 print dt, bool(local_time_t.tm_isdst) 

完整代码

 

 

#!/usr/bin/env python # encoding: utf-8 # # Copyright (c) 2011 Doug Hellmann. All rights reserved. # """Detect whether a datetime value is within daylight savings time or not. """ #end_pymotw_header import time import datetime for dt in [ datetime.datetime(2011, 2, 9, 9, 0), datetime.datetime(2011, 9, 30, 9, 0), ]: # Convert datetime -> struct_time time_t = time.struct_time(dt.timetuple()) # Round-trip: struct_time -> seconds -> struct_time local_time_t = time.localtime(time.mktime(time_t)) # The answer is in tm_isdst # 夏令时标识符 # 实行夏令时的时候,tm_isdst 为 正。 # 不实行夏令时的进候,tm_isdst 为 0 # 不了解情况时,tm_isdst()为负。 print dt, bool(local_time_t.tm_isdst)  

 

你可能感兴趣的:(c,python,struct,library,encoding)