每天CookBook之Python-056

  • 时间转换
  • 时区
  • 本地化
from datetime import datetime, timedelta
from pytz import timezone

d = datetime(2012, 12, 21, 9, 30, 0)

print(d)

central = timezone('US/Central')
loc_d = central.localize(d)

print(loc_d)

band_d = loc_d.astimezone(timezone('Asia/Kolkata'))

print(band_d)

d = datetime(2013, 3, 10, 1, 45)
loc_d = central.localize(d)

print(loc_d)

later = loc_d + timedelta(minutes=30)

print(later)

later = central.normalize(loc_d + timedelta(minutes=30))

print(later)

print(loc_d)

import pytz

utc_d = loc_d.astimezone(pytz.utc)

print(utc_d)

later_utc = utc_d + timedelta(minutes=30)

print(later_utc.astimezone(central))

print(pytz.country_timezones['IN'])

2012-12-21 09:30:00
2012-12-21 09:30:00-06:00
2012-12-21 21:00:00+05:30
2013-03-10 01:45:00-06:00
2013-03-10 02:15:00-06:00
2013-03-10 03:15:00-05:00
2013-03-10 01:45:00-06:00
2013-03-10 07:45:00+00:00
2013-03-10 03:15:00-05:00
['Asia/Kolkata']

Process finished with exit code 0
posted @ 2016-07-21 23:00  4Thing  阅读(107)  评论(0编辑  收藏  举报