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

上一篇:python 基础(十五)--time和datatime模块


下一篇:表格布局TableLayout