Python common module --time & datetime module
In the usual code, we often need to deal with time. In Python, modules and related processing time will include: time, datetime, calendar (rarely used, does not describe).
When we write a program for processing time can be grouped into the following three:
1, Time display: the screen display, logging and so on.
2, the time of conversion: for example, to convert the date into the date string format type Python.
3, the time calculation: calculating a difference between the two dates.
A, time module
In Python, there are usually three ways to represent time:
1, timestamp (timestamp), indicate that from January 1970 00:00:00 1st offset press in seconds. For example: 1554864776.161901
2, the formatted time string, for example: 2019-10-03 17:54
3, tuples (struct_time) total of nine elements. Due to the time Python module to achieve the main C library calls, so each platform may differ on mac:time.struct_time(tm_year=2019, tm_mon=10, tm_mday=3, tm_hour=18, tm_sec=15, tm_wday=4, tm_yday=100,tm_isdst=0)
索引(Index) 属性(Attribute) 值(Values)
0 tm_year(年) 比如2011
1 tm_mon(月) 1 - 12
2 tm_mday(日) 1 - 31
3 tm_hour(时) 0 - 23
4 tm_min(分) 0 - 59
5 tm_sec(秒) 0 - 61
6 tm_wday(weekday) 0 - 6(0表示周日)
7 tm_yday(一年中的第几天) 1 - 366
8 tm_isdst(是否是夏令时) 默认为-1
UTC time
UTC (Coordinated Universal Time, UTC) that is Greenwich astronomical time, the world standard time. In China is UTC + 8, also known as the East Zone 8. DST (Daylight Saving Time) i.e. daylight saving time.
The method of time module
time.localtime([secs]):将一个时间戳转换为当前时区的struct_time。若secs参数未提供,则以当前时间为准。
time.gmtime([secs]):和localtime()方法类似,gmtime()方法是将一个时间戳转换为UTC时区(0时区)的struct_time。
time.time():返回当前时间的时间戳。
time.mktime(t):将一个struct_time转化为时间戳。
time.sleep(secs):线程推迟指定的时间运行,单位为秒。
time.asctime([t]):把一个表示时间的元组或者struct_time表示为这种形式:’Sun Oct 1 12:04:38 2019’。如果没有参数,将会将time.localtime()作为参数传入。
time.ctime([secs]):把一个时间戳(按秒计算的浮点数)转化为time.asctime()的形式。如果参数未给或者为None的时候,将会默认time.time()为参数。它的作用相当于time.asctime(time.localtime(secs))。
time.strftime(format[, t]):把一个代表时间的元组或者struct_time(如由time.localtime()和time.gmtime()返回)转化为格式化的时间字符串。如果t未指定,将传入time.localtime()。
举例:time.strftime(“%Y-%m-%d %X”, time.localtime()) #输出’2017-10-01 12:14:23’
time.strptime(string[, format]):把一个格式化时间字符串转化为struct_time。实际上它和strftime()是逆操作。
举例:time.strptime(‘2017-10-3 17:54’,”%Y-%m-%d %H:%M”) #输出 time.struct_time(tm_year=2017, tm_mon=10, tm_mday=3, tm_hour=17, tm_min=54, tm_sec=0, tm_wday=1, tm_yday=276, tm_isdst=-1)
String transfer time corresponding to the format table:
Meaning | |
---|---|
%a |
Locale’s abbreviated weekday name. |
%A |
Locale’s full weekday name. |
%b |
Locale’s abbreviated month name. |
%B |
Locale’s full month name. |
%c |
Locale’s appropriate date and time representation. |
%d |
Day of the month as a decimal number [01,31]. |
%H |
Hour (24-hour clock) as a decimal number [00,23]. |
%I |
Hour (12-hour clock) as a decimal number [01,12]. |
%J |
Day of the year as a decimal number [001,366]. |
%m |
Month as a decimal number [01,12]. |
%M |
Minute as a decimal number [00,59]. |
%p |
Locale’s equivalent of either AM or PM. |
%S |
Second as a decimal number [00,61]. |
%U |
Week number of the year (Sunday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Sunday are considered to be in week 0. |
%w |
Weekday as a decimal number [0(Sunday),6]. |
%W |
Week number of the year (Monday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Monday are considered to be in week 0. |
%x |
Locale’s appropriate date representation. |
%X |
Locale’s appropriate time representation. |
%y |
Year without century as a decimal number [00,99]. |
%Y |
Year with century as a decimal number. |
%z |
Time zone offset indicating a positive or negative time difference from UTC/GMT of the form +HHMM or -HHMM, where H represents decimal hour digits and M represents decimal minute digits [-23:59, +23:59]. |
%Z |
Time zone name (no characters if no time zone exists). |
Finally, in order to remember easily convert relationship, see below:
Two, datetime module
Compared to the time module, interface module datetime is more intuitive and easier to call.
datetime module defines the following classes:
- datetime.date: represents the class date. Common attributes are year, month, day;
- datetime.time: represents the class time. Common attributes are hour, minute, second, microsecond;
- datetime.datetime: indicate the date and time.
- datetime.timedelta: indicates the time interval, i.e. the length of time between the two points.
- datetime.tzinfo: Information related to the time zone. (Not discussed here in detail the class, interested in children's shoes can refer python manual)
The only method we need to remember the following:
1, d=datetime.datetime.now()
returns the current date datetime type.
d.timestamp(),d.today(), d.year,d.timetuple()等方法可以调用
2, operation time
>>> datetime.datetime.now()
datetime.datetime(2017, 10, 1, 12, 53, 11, 821218)
>>> datetime.datetime.now() + datetime.timedelta(4) #当前时间 +4天
datetime.datetime(2017, 10, 5, 12, 53, 35, 276589)
>>> datetime.datetime.now() + datetime.timedelta(hours=4) #当前时间+4小时
datetime.datetime(2017, 10, 1, 16, 53, 42, 876275)
3, Time Shift
>>> d.replace(year=2999,month=11,day=30)
datetime.date(2999, 11, 30)