编程语言
首页 > 编程语言> > python datatime

python datatime

作者:互联网

import datetime
import pytz
time1 = 'Tue Apr 03 18:00:09 +0000 2012'
time2 = 'Tue Apr 03 23:06:05 +0000 2012'
time3 = 'Sat Feb 16 02:35:36 +0000 2013'

def get_datetime(time_str):
    datetime_obj = datetime.datetime.strptime(time_str,'%a %b %d %H:%M:%S %z %Y')
    return datetime_obj

t1 = get_datetime(time1)
t2 = get_datetime(time2)
t3 = get_datetime(time3)
print(t1.timestamp())
print(t1)
print(t2)
print(t3)

def transform_timezone(datetime_obj, to_utc=False):
    tz1 = pytz.timezone('America/New_York')
    if to_utc:
        tz1 = pytz.timezone('UTC')
    res = datetime_obj.astimezone(tz=tz1)
    return res

t1 = transform_timezone(t1)
t2 = transform_timezone(t2)
t3 = transform_timezone(t3)

print(t1.timestamp())
print(t1)
print(t2)
print(t3)

print(t1-t3)
print(t3-t1)

t = datetime.datetime.fromtimestamp(t1.timestamp(),tz=pytz.timezone('UTC'))
print(t)

[1] Python学习笔记——datatime和pandas.to_datetime
[2] python模块——pytz

标签:datatime,python,t3,t1,pytz,print,timezone,datetime
来源: https://www.cnblogs.com/lixyuan/p/12823162.html