项目开发经常用到一些时间格式转化的情况,这里将一些常用的例子给记录出来:
代码如下:
# -*- coding:utf-8 -*-
import time
import datetime
import calendar
from oslo_utils import timeutils
def sanitize_timestamp(timestamp):
"""Return a naive utc datetime object."""
if not timestamp:
return timestamp
if not isinstance(timestamp, datetime.datetime):
timestamp = timeutils.parse_isotime(timestamp)
return timeutils.normalize_time(timestamp)
def format_time(int_time):
return timeutils.strtime(datetime.datetime.utcfromtimestamp(int_time))
def dt2ts(orig_dt):
"""Translate a datetime into a timestamp."""
return calendar.timegm(orig_dt.timetuple())
def utcnow():
"""Returns a datetime for the current utc time."""
return timeutils.utcnow()
def get_month_days(dt):
return calendar.monthrange(dt.year, dt.month)[1]
def add_days(base_dt, days, stay_on_month=True):
if stay_on_month:
max_days = get_month_days(base_dt)
if days > max_days:
return get_month_end(base_dt)
return base_dt + datetime.timedelta(days=days)
def add_month(dt, stay_on_month=True):
next_month = get_next_month(dt)
return add_days(next_month, dt.day, stay_on_month)
def sub_month(dt, stay_on_month=True):
prev_month = get_last_month(dt)
return add_days(prev_month, dt.day, stay_on_month)
def get_month_start(dt=None):
if not dt:
dt = utcnow()
month_start = datetime.datetime(dt.year, dt.month, 1)
return month_start
def get_month_start_timestamp(dt=None):
return dt2ts(get_month_start(dt))
def get_month_end(dt=None):
month_start = get_month_start(dt)
days_of_month = get_month_days(month_start)
month_end = month_start.replace(day=days_of_month)
return month_end
def get_last_month(dt=None):
if not dt:
dt = utcnow()
month_end = get_month_start(dt) - datetime.timedelta(days=1)
return get_month_start(month_end)
def get_next_month(dt=None):
month_end = get_month_end(dt)
next_month = month_end + datetime.timedelta(days=1)
return next_month
###########################################################################################
# 使用例子
############################################################################################
timestamp_start = "2017-08-01T06:55:25"
timestamp_end = "2017-08-01T06:55:59"
# 1,将时间戳转为秒
start = sanitize_timestamp(timestamp_start)
print "1:change timestamp to seconds:"
seconds = time.mktime(start.timetuple())
print seconds
# 2,获取当前秒数
print "\n2:get current times:"
print time.time()
# 3,获取两个时间戳的时间间隔
print "\n3:get the time delta:"
print "start:"
print start
end = sanitize_timestamp(timestamp_end)
print "end:"
print end
delta_seconds = timeutils.delta_seconds(start, end)
print "delta_seconds: %s" % str(delta_seconds)
# 4,获取当前utc时间
print "\n4:get the utc time:"
print timeutils.utcnow()
# 5,将int时间转化为utc时间戳
print "\n5:format time from seconds:"
print format_time(int(seconds))
# 6,将int时间转化为iso格式的时间戳
print "\n6:format iso timestamp:"
print timeutils.isotime(utcnow())
print "\n7:format iso timestamp to other timezone:" iso_time = "2017-08-31T22:13:01Z" dt1 = sanitize_timestamp(iso_time) print dt1 print "\nchage to shanghai:" import pytz eastern = pytz.timezone('Asia/Shanghai') dt2 = eastern.localize(dt1, is_dst=True) print dt2 print "\nget uctoffset:" dt3 = eastern.utcoffset(dt1) print dt3 print "\nfromutc:" dt4 = eastern.fromutc(dt1) print dt4 print "\nutcoffset:" dt5 = eastern.utcoffset(dt1) print dt5
输出效果:1:change timestamp to seconds: 1501541725.0 2:get current times: 1503039597.67 3:get the time delta: start: 2017-08-01 06:55:25 end: 2017-08-01 06:55:59 delta_seconds: 34.0 4:get the utc time: 2017-08-18 06:59:57.673182 5:format time from seconds: 2017-07-31T22:55:25.000000 6:format iso timestamp: 2017-08-18T06:59:57Z
7:format iso timestamp to other timezone: 2017-08-31 22:13:01 chage to shanghai: 2017-08-31 22:13:01+08:00 get uctoffset: 8:00:00 fromutc: 2017-09-01 06:13:01+08:00 utcoffset: 8:00:00