Skip to content

Instantly share code, notes, and snippets.

@shuxiang
Last active August 29, 2015 14:08
Show Gist options
  • Save shuxiang/67da8a16521d2e26b584 to your computer and use it in GitHub Desktop.
Save shuxiang/67da8a16521d2e26b584 to your computer and use it in GitHub Desktop.
time transform
#coding=utf8
from datetime import datetime, timedelta, date
import time
from time import struct_time
from time import localtime
"""
%y 两位数的年份表示(00-99)
%Y 四位数的年份表示(000-9999)
%m 月份(01-12)
%d 月内中的一天(0-31)
%H 24小时制小时数(0-23)
%I 12小时制小时数(01-12)
%M 分钟数(00=59)
%S 秒(00-59)
%a 本地简化星期名称
%A 本地完整星期名称
%b 本地简化的月份名称
%B 本地完整的月份名称
%c 本地相应的日期表示和时间表示
%j 年内的一天(001-366)
%p 本地A.M.或P.M.的等价符
%U 一年中的星期数(00-53)星期天为星期的开始
%w 星期(0-6),星期天为星期的开始
%W 一年中的星期数(00-53)星期一为星期的开始
%x 本地相应的日期表示
%X 本地相应的时间表示
%Z 当前时区的名称
%% %号本身
"""
##format to datetime string
def tos_datetime(t):
ret = ''
if type(t) == datetime or type(t) == date:
ret = t.strftime('%Y-%m-%d %H:%M:%S')
if type(t) == int or type(t) == float:
ret = datetime.utcfromtimestamp(t).strftime('%Y-%m-%d %H:%M:%S')
if type(t) == struct_time:
ret = time.strftime('%Y-%m-%d %H:%M:%S', t)
return ret
##format to date string
def tos_date(t):
ret = ''
if type(t) == datetime or type(t) == date:
ret = t.strftime('%Y-%m-%d')
if type(t) == int or type(t) == float:
ret = datetime.utcfromtimestamp(t).strftime('%Y-%m-%d')
if type(t) == struct_time:
ret = time.strftime('%Y-%m-%d', t)
return ret
##format to any string
def tos_(t, format):
ret = ''
if type(t) == datetime or type(t) == date:
ret = t.strftime(format)
if type(t) == int or type(t) == float:
ret = datetime.utcfromtimestamp(t).strftime(format)
if type(t) == struct_time:
ret = time.strftime(format, t)
return ret
#any to time
def to_time(t):
ret = None
if type(t) == datetime:
ret = time.mktime(t.utctimetuple())
if type(t) == float or type(t) == int:
ret = t
if type(t) == timedelta:
ret = t.total_seconds()
if type(t) == struct_time:
ret = time.mktime(t)
if type(t) == date:
ret = time.mktime(t.timetuple())
return ret
#any to datetime
def to_datetime(t):
ret = None
if type(t) == datetime:
ret = t
if type(t) == int or type(t) == float:
ret = datetime.utcfromtimestamp(t)
if type(t) == struct_time:
ret = datetime.utcfromtimestamp(time.mktime(t))
if type(t) == date:
ret = datetime.utcfromtimestamp(time.mktime(t.timetuple()))
return ret
#any to tuple
def to_tuple(t):
ret = None
if type(t) == datetime:
ret = t.utctimetuple()
if type(t) == int or type(t) == float:
ret = localtime(t)
if type(t) == struct_time:
ret = t
if type(t) == date:
ret = t.timetuple()
if type(t) == timedelta:
ret = localtime(t.total_seconds())
return ret
#any to date
def to_date(t):
ret = None
if type(t) == datetime:
ret = t.date()
if type(t) == int or type(t) == float:
ret = datetime.utcfromtimestamp(t).date()
if type(t) == struct_time:
ret = datetime.utcfromtimestamp(time.mktime(t)).date()
if type(t) == date:
ret = t
return ret
#any to timedelta
def to_timedelta(t):
ret = None
if type(t) == int or type(t) == float:
ret = timedelta(t)
if type(t) == timedelta:
ret = t
if type(t) == struct_time:
ret = timedelta(time.mktime(t))
#string to datetime
def sto_datetime(s, format):
return datetime.strptime(s, format)
#string to time
def sto_time(s, format):
return time.mktime(time.strptime(s, format))
#string to timedelta
def sto_timedelta(s, format):
return timedelta(time.mktime(time.strptime(s, format)))
#string to struct tuple
def sto_tuple(s, format):
return time.strftime(s, format)
#string to date
def sto_date(s, format):
return datetime.strptime(s, format).date()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment