Small ape circle python learning -time & datetime module

In the usual code, we often need to deal with time. In Python, the processing time associated with the module including: time, datetime, calendar (rarely used, do not speak), respectively described below.

When we write a program for processing time can be grouped into the following three:

Time display, screen display, logging, etc.

Time conversion, such as the date format is converted to the date string type in Python

Time, and calculates the difference between the two date

time module
in Python, which usually represents time in several ways:

Timestamp (timestamp), indicate that from January 1970 00:00:00 1st offset press in seconds. Example: 1554864776.161901

Time formatted string, such as "2020-10-03 17:54"

Tuple (struct_time) total of nine elements. Since Python time module to achieve the main calling the C library, so each platform may vary, the mac: time.struct_time (tm_year = 2020, tm_mon = 4, tm_mday = 10, tm_hour = 2, tm_min = 53, tm_sec = 15, tm_wday = 2, tm_yday = 100, tm_isdst = 0)

Index (Index) property (the Attribute) values (Values)
0 tm_year (years), such as 2011
. 1 tm_mon (January). 1 - 12 is
2 tm_mday (date). 1 - 31 is
. 3 tm_hour (time) 0 - 23 is
. 4 tm_min (min) 0 - 59
. 5 tm_sec (s) 0 - 61 is
. 6 tm_wday (WEEKDAY) 0 -. 6 (Sunday represents 0)
(day of the year). 7 tm_yday. 1 - 366
(whether it is daylight saving time) 8 tm_isdst default -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]): The time stamp is converted to a current time zone struct_time. Secs If the parameter is not provided, the current time places subject.

time.gmtime ([secs]): and the like localtime () method, gmtime () method is to convert a timestamp struct_time the UTC time zone (0:00 region).

time.time (): Returns the timestamp of the current time.

time.mktime (t): The conversion of a struct_time timestamp.

time.sleep (secs): thread running postpone the specified time, in seconds.

time.asctime ([t]): the tuple indicates a time or as expressed in this form struct_time: 'Sun Oct 1 12:04:38 2019'. If there are no parameters, it will be time.localtime () as a parameter.

time.ctime ([secs]): to a timestamp (in seconds calculated in floating point) is converted to that time.asctime () form. If the parameter is not given time or None, it will default time.time () as a parameter. It acts time.asctime (time.localtime (secs)).

time.strftime (format [, t]): representing time to a tuple or struct_time (as returned by time.localtime () and time.gmtime ()) is converted to time format string. If t is not specified, the incoming time.localtime ().

Example: time.strftime ( "% Y-% m-% d% X", time.localtime ()) # outputs "2017-10-01 12:14:23"

time.strptime (string [, format]): the time a formatted string into struct_time. In fact it strftime () is the inverse operation.

举例: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)

Guess you like

Origin blog.csdn.net/weixin_44867000/article/details/92576595