time 模块

import time

print(dir(time))
print(time.__doc__)

结果:

['_STRUCT_TM_ITEMS', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'altzone', 'asctime', 'clock', 'ctime', 'daylight', 'get_clock_info', 'gmtime', 'localtime', 'mktime', 'monotonic', 'perf_counter', 'process_time', 'sleep', 'strftime', 'strptime', 'struct_time', 'time', 'timezone', 'tzname']
This module provides various functions to manipulate time values.

There are two standard representations of time.  One is the number
of seconds since the Epoch, in UTC (a.k.a. GMT).  It may be an integer
or a floating point number (to represent fractions of seconds).
The Epoch is system-defined; on Unix, it is generally January 1st, 1970.
The actual value can be retrieved by calling gmtime(0).

The other representation is a tuple of 9 integers giving local time.
The tuple items are:
  year (including century, e.g. 1998)
  month (1-12)
  day (1-31)
  hours (0-23)
  minutes (0-59)
  seconds (0-59)
  weekday (0-6, Monday is 0)
  Julian day (day in the year, 1-366)
  DST (Daylight Savings Time) flag (-1, 0 or 1)
If the DST flag is 0, the time is given in the regular time zone;
if it is 1, the time is given in the DST time zone;
if it is -1, mktime() should guess based on the date and time.

Variables:

timezone -- difference in seconds between UTC and local standard time #UTC与本地标准时间之间的秒差
altzone -- difference in  seconds between UTC and local DST time      #UTC与本地DST时间之间的秒差
daylight -- whether local time should reflect DST                     #当地时间是否应反映DST
tzname -- tuple of (standard time zone name, DST time zone name)      #(标准时区名称,DST时区名称)元组

Functions:

time() -- return current time in seconds since the Epoch as a float #返回当前时间(以秒为计算单位),是一个浮点数
clock() -- return CPU time since process start as a float           #从进程开始返回CPU时间,是一个浮点数
sleep() -- delay for a number of seconds given as a float           #延迟一段给定时间,返回None
gmtime() -- convert seconds since Epoch to UTC tuple          #从历元到UTC元组转换秒数
localtime() -- convert seconds since Epoch to local time tuple
asctime() -- convert time tuple to string                           #将时间元组转换为字符串
ctime() -- convert time in seconds to string                        #将秒数转换为字符串
mktime() -- convert local time tuple to seconds since Epoch         #将局部时间元组转换为秒
strftime() -- convert time tuple to string according to format specification #根据格式规范将时间元组转换为字符串
strptime() -- parse string to time tuple according to format specification #根据格式规范解析字符串到时间元组
tzset() -- change the local timezone                                #更改本地时区

time模块中时间表现的格式主要有三种:

  a、timestamp时间戳,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量

  b、struct_time时间元组,共有九个元素组。

  c、format time 格式化时间,已格式化的结构使时间更具可读性。包括自定义格式和固定格式。

时间格式转换图:

主要time生成方法和time格式转换方法实例:

 1 #!/usr/bin/python3
 2 # -*- coding:utf-8 -*-
 3 #__author:Administrator
 4 #date:2018/6/24
 5 import time
 6 
 7 # 生成时间戳
 8 print('1','>>>',time.time())
 9 #将元组时间转化为时间戳
10 print('1','>>>',time.mktime(time.localtime()))
11 print('1','>>>',time.mktime(time.gmtime()))
12 
13 print('2','>>>',time.clock())
14 #等待时间,无返回
15 print('3','>>>',time.sleep(2))
16 
17 #时间戳转化为时间元组 格林威治时间
18 time1=time.time()
19 print('4','>>>',time.gmtime()) #返回格林威治时间
20 print('4','>>>',time.gmtime(time1))# 默认获取当前时间
21 # 时间戳转化为时间元组 本地时间
22 print('5','>>>',time.localtime(time1))
23 print('5','>>>',time.localtime())# 默认获取当前时间戳
24 #格式化时间转化为时间元组
25 print('6','>>>',time.strptime('2018-06-24 11:50:00','%Y-%m-%d %X'))
26 print('6','>>>',time.strptime('2018-06-24','%Y-%m-%d'))
27 #生成格式化时间
28 print('7','>>>',time.strftime('%Y-%m-%d %X'))
29 #元组时间转化为格式化时间
30 print('7','>>>',time.strftime('%Y-%m-%d %X',time.localtime())) # 本地时间
31 print('7','>>>',time.strftime('%Y-%m-%d',time.localtime())) # 本地时间
32 print('7','>>>',time.strftime('%X',time.localtime())) # 本地时间
33 print('7','>>>',time.strftime('%m-%d %X',time.localtime())) # 本地时间
34 print('7','>>>',time.strftime('%Y-%m-%d %X',time.gmtime()))# 格林威治时间
35 
36 
37 #生成固定格式的时间表示格式
38 #将时间元组转化为格式化时间
39 print('8','>>>',time.asctime())
40 print('8','>>>',time.asctime(time.localtime()))
41 print('8','>>>',time.asctime(time.gmtime()))
42 #将时间戳转化为格式化时间
43 print('9','>>>',time.ctime())#将秒数转换为字符串
44 print('9','>>>',time.ctime(time.time()))#将秒数转换为字符串

结果:

D:\Programs\Python36\python.exe F:/python全栈开发(从入门到放弃)/time_text.py
1 >>> 1529813858.3810995
1 >>> 1529813858.0
1 >>> 1529785058.0
2 >>> 0.0
3 >>> None
4 >>> time.struct_time(tm_year=2018, tm_mon=6, tm_mday=24, tm_hour=4, tm_min=17, tm_sec=40, tm_wday=6, tm_yday=175, tm_isdst=0)
4 >>> time.struct_time(tm_year=2018, tm_mon=6, tm_mday=24, tm_hour=4, tm_min=17, tm_sec=40, tm_wday=6, tm_yday=175, tm_isdst=0)
5 >>> time.struct_time(tm_year=2018, tm_mon=6, tm_mday=24, tm_hour=12, tm_min=17, tm_sec=40, tm_wday=6, tm_yday=175, tm_isdst=0)
5 >>> time.struct_time(tm_year=2018, tm_mon=6, tm_mday=24, tm_hour=12, tm_min=17, tm_sec=40, tm_wday=6, tm_yday=175, tm_isdst=0)
6 >>> time.struct_time(tm_year=2018, tm_mon=6, tm_mday=24, tm_hour=11, tm_min=50, tm_sec=0, tm_wday=6, tm_yday=175, tm_isdst=-1)
6 >>> time.struct_time(tm_year=2018, tm_mon=6, tm_mday=24, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=6, tm_yday=175, tm_isdst=-1)
7 >>> 2018-06-24 12:17:40
7 >>> 2018-06-24 12:17:40
7 >>> 2018-06-24
7 >>> 12:17:40
7 >>> 06-24 12:17:40
7 >>> 2018-06-24 04:17:40
8 >>> Sun Jun 24 12:17:40 2018
8 >>> Sun Jun 24 12:17:40 2018
8 >>> Sun Jun 24 04:17:40 2018
9 >>> Sun Jun 24 12:17:40 2018
9 >>> Sun Jun 24 12:17:40 2018

Process finished with exit code 0

 struct_time元组元素结构

属性                            值
tm_year(年)                  比如2011 
tm_mon(月)                   1 - 12
tm_mday(日)                  1 - 31
tm_hour(时)                  0 - 23
tm_min(分)                   0 - 59
tm_sec(秒)                   0 - 61
tm_wday(weekday)             0 - 6(0表示周日)
tm_yday(一年中的第几天)        1 - 366
tm_isdst(是否是夏令时)        默认为-1

format time结构化表示

格式 含义
%a 本地(locale)简化星期名称
%A 本地完整星期名称
%b 本地简化月份名称
%B 本地完整月份名称
%c 本地相应的日期和时间表示
%d 一个月中的第几天(01 - 31)
%H 一天中的第几个小时(24小时制,00 - 23)
%I 第几个小时(12小时制,01 - 12)
%j 一年中的第几天(001 - 366)
%m 月份(01 - 12)
%M 分钟数(00 - 59)
%p 本地am或者pm的相应符
%S 秒(01 - 61)
%U 一年中的星期数。(00 - 53星期天是一个星期的开始。)第一个星期天之前的所有天数都放在第0周。
%w 一个星期中的第几天(0 - 6,0是星期天)
%W 和%U基本相同,不同的是%W以星期一为一个星期的开始。
%x 本地相应日期
%X 本地相应时间
%y 去掉世纪的年份(00 - 99)
%Y 完整的年份
%Z 时区的名字(如果不存在为空字符)
%% ‘%’字符

常见结构化时间组合:

print (time.strftime("%Y-%m-%d %X"))

猜你喜欢

转载自www.cnblogs.com/Mengchangxin/p/9219881.html