Python3-笔记-E-002-库-Unix纪元时间戳time

import time

def time_demo():
curtime = time.time() # 获取当前时间戳, 1508982743.220433
time_str = time.ctime(curtime) # 转为string格式, 'Thu Oct 26 09:52:23 2017'
print(time_str) # => Fri May 5 18:28:08 2017

time_tup = time.localtime(curtime)
# 转为struct_time(tuple)格式,
# time.struct_time(tm_year=2017, tm_mon=10, tm_mday=26, tm_hour=9, tm_min=52, tm_sec=23, tm_wday=3, tm_yday=299, tm_isdst=0)
print(time_tup.tm_year) # => 2017


'''
time模块处理时间的相关说明:
1. 部分系统无法处理很久之前或之后的日期和时间 (:32系统通常时间到2038年为止)
2. UTC (/GMT) 为格林威治标准时间 (简称:世界时间);
3. DST 为夏令时
4. 格式化指示符: %Y(世纪年) / %m([01, 12]) / %d([01, 31]) / %H([00, 23]) / %M([00, 59]) / %S([00, 61]) / %w(星期[0, 6])
%b(E缩写) %B(E) / %a(星期E缩写) / %A(星期E) / %I(12[01, 12]) / %c(日期+时间) / %x(日期) / %X(时间) / %p(AM/PM) / %z(时区[-2359+2359]) / %%('%')
%j(<-[001, 366]) / %U(<-星期[00, 53]) / %W(<-[00, 53])
5. 星期日为一个星期周期的第一天

'''

# 时间戳
time_s = time.altzone # 夏令时与UTC的差值, -32400
time_s = time.timezone # (时区) 本地时间与UTC的差值, -28800
time_s = time.time() # 当前时间戳 (受系统时间影响) 单位:, 1508982895.9727015
time_s = time.mktime(time_tup) # 元组转成时间, 1508982955.0
time_s = time.monotonic() # 单调始终的值 (不受系统时钟更新的影响) 单位: ,670979.781
time_s = time.perf_counter() # (高分辨率)性能计数器 (包括睡眠时间) 单位: ,3.208020564695028e-07

# 元组(struct_time) [格式:(2008, 1, 1, 0, 0, 0, -1, -1, -1) >> (, , , , , , 星期, <-, DST)]
# gmtime([secs]) // 时间戳转为UTC; 0: 开始0年的时间(1970) / 无参:UTC / time_s:转为世界时间
time_tup = time.gmtime(time_s) # time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=1, tm_isdst=0)
# localtime([secs]) // 时间戳转为本地时间
time_tup = time.localtime(time_s)
# strptime(string[, format]) 解析时间
time_tup = time.strptime('Tue Jan 01 00:00:00 2008', '%a %b %d %H:%M:%S %Y') # 字符串解析成时间

# struct_time
time_year = time_tup.tm_year # struct_time中获取数据, 其他省略

# 字符串
# asctime([t]) // 时间格式化 (系统样式); 不传参为当前时间
time_str = time.asctime(time_tup) # 'Tue Jan 1 00:00:00 2008'
# ctime([secs]) // asctime()
time_str = time.ctime(time_s) # 'Thu Jan 1 08:00:00 1970'
# strftime(format[, t])
time_str = time.strftime("%Y-%m-%d-%H-%M-%S", time_tup) # '2008-01-01-00-00-00'

# 其他
time.sleep(1.1) # 线程睡眠 单位:s
time_dst = time.daylight # 夏令时时区(0未定义)
 

# === time ===
# time可做字典的键, time视为True
# Time表示一天中的本地时间,独立于任何特定的日子
# hour[0, 24) minute[0, 60) second[0, 60) microsecond微秒[0, 1000000)
# datetime.time(hour=0, minute=0, second=0, microsecond=0, tzinfo=None)
time = datetime.time(18, 30, 59) # 18:30:59

time_t = time.min # 最早 => 00:00:00
time_t = time.max # 最晚 => 23:59:59.999999
time_t = time.resolution # 最小差值 => 0:00:00.000001
num = time.hour # 18
num = time.minute # 30
mum = time.second # 59
mum = time.microsecond # 0

# replace([hour[, minute[, second[, microsecond[, tzinfo]]]]])
time_t = time.replace(hour=17) # 替换 17:30:59
time_s = time.isoformat() # 格式化 =>'18:30:59'
time_s = time.strftime("%H-%M-%S") # 格式化 '18-30-59'
time_t = time.min  # 最早 =>  00:00:00time_t = time.max  # 最晚 =>  23:59:59.999999time_t = time.resolution  # 最小差值 => 0:00:00.000001num = time.hour  # 18num = time.minute # 30mum = time.second # 59mum = time.microsecond # 0# replace([hour[, minute[, second[, microsecond[, tzinfo]]]]])time_t = time.replace(hour=17)  # 替换 17:30:59time_s = time.isoformat()  # 格式化 =>'18:30:59'time_s = time.strftime("%H-%M-%S")  # 格式化 '18-30-59'
posted @ 2017-10-26 10:03  魏桐  阅读(494)  评论(0编辑  收藏  举报