整个地球分为二十四时区,每个时区都有自己的本地时间。在国际无线电通信中,为统一而普遍使用一个标准时间,称为通用协调时(UTC, Universal Time Coordinated)。UTC与格林尼治平均时(GMT, Greenwich Mean Time)一样,都与英国伦敦的本地时相同。UTC与GMT含义完全相同。北京时区是东八区,领先UTC 8个小时。所以将UTC装换成北京时间时,需要加上8小时。
utc = "2017-07-28T08:28:47.776Z"
UTC_FORMAT = "%Y-%m-%dT%H:%M:%S.%fZ"
utcTime = datetime.datetime.strptime(utc, UTC_FORMAT)
localtime = utcTime + datetime.timedelta(hours=8)
print(localtime)
输出:2017-07-28 16:28:47.776000
以下为补充部分: #!/usr/bin/env python #coding:utf-8
本地时间 转换 为时间戳
import time import pytz import datetime dateC1=datetime.datetime(2015,11,30,15,55,00) timestamp2=time.mktime(dateC1.timetuple()) print timestamp2
#时间戳转换为本地时间 import datetime import time ltime=time.localtime(1470009000) ltime=time.localtime(1479285300) timeStr=time.strftime("%Y-%m-%d %H:%M:%S", ltime) print timeStr
UTCS时间转换为时间戳 2016-07-31T16:00:00Z
def utc_to_local(utc_time_str, utc_format='%Y-%m-%dT%H:%M:%SZ'): local_tz = pytz.timezone('Asia/Chongqing') local_format = "%Y-%m-%d %H:%M" utc_dt = datetime.datetime.strptime(utc_time_str, utc_format) local_dt = utc_dt.replace(tzinfo=pytz.utc).astimezone(local_tz) time_str = local_dt.strftime(local_format) return int(time.mktime(time.strptime(time_str, local_format)))
本地时间转换为UTC
def local_to_utc(local_ts, utc_format='%Y-%m-%dT%H:%MZ'): local_tz = pytz.timezone('Asia/Chongqing') local_format = "%Y-%m-%d %H:%M" time_str = time.strftime(local_format, time.localtime(local_ts)) dt = datetime.datetime.strptime(time_str, local_format) local_dt = local_tz.localize(dt, is_dst=None) utc_dt = local_dt.astimezone(pytz.utc) return utc_dt.strftime(utc_format)
t = u"2016-07-31T16:00:00Z"
t = u"2015-07-31T15:55:00Z"
t = u"2015-11-30T15:55:00Z"
t1 = u"2015-12-31T15:55:00Z" # 1470441600 1470095400 1470613800 ret = utc_to_local(t) ret1 = utc_to_local(t1) print ret,ret1 #1469923200 1470009600