记录-datetime
Topic sourcefrom datetime import datetime, timezone, timedeltaimport redef to_timestamp(dt_str, tz_str): ''' >>> to_timestamp('2015-6-1 08:10:30', 'UTC+7:00') 1433121030.0 >>> to_timestamp('2015-5-31 16:10:30', 'UTC-09:00') 1433121030.0 ''' # 将日期和时间转换成datetime类型为cday cday = datetime.strptime(dt_str, '%Y-%m-%d %H:%M:%S') # print(cday) # 使用正则表达式提出时区 m = re.match(r'UTC([+-]\d+):00', tz_str) # print(m.group(1)) # 将时区转换成int类型 int_tz_str = int(m.group(1)) # print(int_tz_str) # 将cday的时间区分出为int_tz_str的时区 tz_utc_n = timezone(timedelta(hours=int_tz_str)) cday = cday.replace(tzinfo=tz_utc_n) # print(cday) # 返回datetime对于的timestamp return cday.timestamp()if __name__ == '__main__': import doctest doctest.testmod(verbose=True)
- 1
这是QQ昵称