Time&datetime module of common python modules
In the usual code, we often have to deal with time. In Python, the modules related to time processing include: times and DateTime, as described below:
Before you begin, there are several ways to represent the time:
- 1. Time stamp
- 2. Formatted time string (Time object)
- 3. Tuples (struct_time) a total of nine elements, because the Python time module implementation of the main call C library, so the platform may be different
Several definitions
UTC (coordinated Universal time, world co-ordination) is GMT, world standard Time. In China for Utc+8. DST (daylight saving time) is daylight saving time.
Timestamp (timestamp): Typically, a timestamp represents an offset that is calculated in seconds from 00:00:00 January 1, 1970, and we run type(time.time())
, returning the float type.
Tuple (Tuple_time) way: There are 9 elements in the Struct_time tuple, and the function that returns Struct_time is mainly,,, the gmtime()
localtime()
strptime()
following list of several elements in this way tuple:
| Index | Property Attribute | Value (values) |
| :--------: | :-----: | :----: |
| 0 | Tm_year (year) | Like 2011 |
| 1 | Tm_mon (Month | 1-12 |
| 2 | Tm_day (Sun) | 1-31 |
| 3 | Tm_hour (Time) | 0-23 |
| 4 | Tm_min (min) | 0-59 |
| 5 | Tm_sec (sec) | 0-59 |
| 6 | Tm_wday (week) | 0-6 (0 = Sunday) |
| 7 | Tm_yday (Day of the Year) | 1-366 |
| 8 | TM_ISDST (Daylight saving time) | Default is 1 |
Method of the Time module
time.localtime([sesc])
: Converts a timestamp to the struct_time of the current time zone. The SESC parameter is not provided, whichever is the current time
In [2]: import timeIn [3]: time.localtime()Out[3]: time.struct_time(tm_year=2018, tm_mon=5, tm_mday=9, tm_hour=11, tm_min=28, tm_sec=4, tm_wday=2, tm_yday=129, tm_isdst=0)
time.gmtime([sesc])
: time.localtime()
similar to a method, by gmtime()
converting a timestamp into the struct_time of the UTC time Zone
In [5]: time.gmtime(1012931) # 里面跟的是时间戳,如果不跟的话就和localtime()方法一样了Out[5]: time.struct_time(tm_year=1970, tm_mon=1, tm_mday=12, tm_hour=17, tm_min=22, tm_sec=11, tm_wday=0, tm_yday=12, tm_isdst=0)
time.time()
: Returns the timestamp of the current time
In [6]: time.time()Out[6]: 1525836757.4961157 # 那么当我们拿到这个时间戳,就可以通过time.gmtime()方法转换成为时间对象了In [7]: time.gmtime(1525836757.4961157)Out[7]: time.struct_time(tm_year=2018, tm_mon=5, tm_mday=9, tm_hour=3, tm_min=32, tm_sec=37, tm_wday=2, tm_yday=129, tm_isdst=0)
time.sleep([sesc])
: The thread defers the specified time to run. Unit is seconds
In [8]: time.sleep(2)
time.asctime([t])
: A tuple or Time object (Struct_time) representing time is represented in this form: Wed May 9 11:42:49 2018
In [11]: time.asctime()Out[11]: ‘Wed May 9 11:42:49 2018‘
time.ctime([sesc])
: Converts a timestamp (floating point calculated in seconds) to Time.asctime ()
In [15]: time.ctime(13131313131) # 支持自定义输入时间戳Out[15]: ‘Wed Feb 12 02:58:51 2386‘# 如果没有给参数或者为None的话,默认参数就是time.time()。它的作用相当于是time.asctime(time.localtime())In [14]: time.ctime()Out[14]: ‘Wed May 9 11:45:48 2018‘# 它的作用相当于是time.asctime(time.localtime())In [16]: time.asctime(time.localtime())Out[16]: ‘Wed May 9 11:49:09 2018‘
time.srtftime(format,[,t])
: Converts a tuple or time object representing time (Struct_time) (such as returned by Time.localtime () and Time.gmtime ()) to a formatted time string that, if T is not specified, will be passed into the Time.localtime ()
# t没有指定In [17]: time.strftime("%Y-%m-%d %H:%M:%S")Out[17]: ‘2018-05-09 11:56:02‘# t指定In [3]: time.localtime()Out[3]: time.struct_time(tm_year=2018, tm_mon=5, tm_mday=9, tm_hour=11, tm_min=57, tm_sec=50, tm_wday=2, tm_yday=129, tm_isdst=0)In [4]: a = time.localtime()In [5]: time.strftime("%Y-%m-%d",a)Out[5]: ‘2018-05-09‘
time.strptime()
: Converts a formatted time string into a Time object (Struct_time). Actually and strftime () is the inverse operation
In [8]: a = time.localtime()In [9]: time.strftime("%Y-%m-%d %H:%M:%S",a)Out[9]: ‘2018-05-09 12:01:22‘In [10]: s = time.strftime("%Y-%m-%d %H:%M:%S",a)In [11]: s2 = time.strptime(s,"%Y-%m-%d %H:%M:%S")In [12]: print(s2)time.struct_time(tm_year=2018, tm_mon=5, tm_mday=9, tm_hour=12, tm_min=1, tm_sec=22, tm_wday=2, tm_yday=129, tm_isdst=-1)
Time stamp and string conversion relationship DateTime module
The interface of the DateTime module is more intuitive and easier to invoke than the time module
The DateTime module defines the following classes
- Datetime.date (): A class that represents a date. The commonly used attributes are year,month,day;
- Datetime.time (): The class that represents the time. The commonly used attributes are hour,minute,second,microsecond;
- Datetime.datetime (): Indicates the date time;
- Datetime.timedelta (): Indicates the time interval, that is, the length between two points of time;
Here are a few ways we need to remember
1. d=datetime.datetime.now()
return the current datetime date type
In [1]: import datetime # 导入datetime模块In [2]: datetime.datetime.now() # 返回当前datetime日期类型 Out[2]: datetime.datetime(2018, 5, 9, 12, 7, 53, 162336)# 但是如果我们用变量保存的话In [3]: d = datetime.datetime.now()In [5]: d.yearOut[5]: 2018In [9]: d.monthOut[9]: 5In [10]: d.dayOut[10]: 9
2. datetime.date.fromtimestamp()
: Convert a timestamp to a datetime date type
In [11]: datetime.date.fromtimestamp(1111111) Out[11]: datetime.date(1970, 1, 14) # 但是却没有时分秒了
3. Time arithmetic
In [5]: datetime.datetime.now()Out[5]: datetime.datetime(2018, 5, 9, 12, 16, 50, 61482) # 2018年5月9日12:16In [8]: datetime.datetime.now() + datetime.timedelta(days=1) # 加一天Out[8]: datetime.datetime(2018, 5, 10, 12, 17, 29, 495152) 2018年5月10日12:17
4. Time Replacement
In [9]: d = datetime.datetime.now()In [10]: d.replace(year=1027,month=11,day=30) # 想回到1027年的11月30日Out[10]: datetime.datetime(1027, 11, 30, 12, 18, 48, 62154)
Time&datetime module of common python modules