def datetime2timestamp(dt, convert_to_utc=False):
''' Converts a datetime object to UNIX timestamp in milliseconds. '''
if isinstance(dt, datetime.datetime):
if convert_to_utc: # 是否转化为UTC时间
dt = dt + datetime.timedelta(hours=-8) # 中国默认时区
timestamp = total_seconds(dt - EPOCH)
return long(timestamp)
return dt
def timestamp2datetime(timestamp, convert_to_local=False):
''' Converts UNIX timestamp to a datetime object. '''
if isinstance(timestamp, (int, long, float)):
dt = datetime.datetime.utcfromtimestamp(timestamp)
if convert_to_local: # 是否转化为本地时间
dt = dt + datetime.timedelta(hours=8) # 中国默认时区
return dt
return timestamp
def timestamp_utc_now(): return datetime2timestamp(datetime.datetime.utcnow())
def timestamp_now(): return datetime2timestamp(datetime.datetime.now())
# 需要安装python-dateutil
# Ubuntu下:sudo apt-get install python-dateutil
# 或者使用PIP:sudo pip install python-dateutil
from dateutil import tz
from dateutil.tz import tzlocal
from datetime import datetime
# get local time zone name
print datetime.now(tzlocal()).tzname()
# UTC Zone
from_zone = tz.gettz('UTC')
# China Zone
to_zone = tz.gettz('CST')
utc = datetime.utcnow()
# Tell the datetime object that it's in UTC time zone
utc = utc.replace(tzinfo=from_zone)
# Convert time zone
local = utc.astimezone(to_zone)
print datetime.strftime(local, "%Y-%m-%d %H:%M:%S")
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有