When writing python, there are a lot of time functions that need to be used, such as recording the current time, naming the file in time, or comparing the times, and recording the methods of the common time module.
Environment:
ubuntu16.04
python3.5.2
Use of two modules: Time and datetime
Timestamp after January 1, 1970 seconds, i.e.: Time.time ()
Formatted string 2014-11-11 11:11, i.e.: Time.strftime ('%y-%m-%d ')
Structured time tuples include: year, day, week, etc. time.struct_time is: time.localtime ()
Timestamps can be used for calculations, and structs can be used for calculations as well.
Time module:
Time.clock ()
This method information indicates that:
returns the current CPU time in seconds calculated as a floating-point number. It is more useful than time.time () to measure the time spent on different programs. It is important to note that the meanings are different on different systems. On a UNIX system, it returns "process time", which is a floating-point number (timestamp) in seconds. In Windows, the first call returns the actual time that the process is running. The second subsequent call is the elapsed time since the first call to the present. (actually based on QueryPerformanceCounter () on WIN32, which is more accurate than milliseconds)
I understand that this is generally used to calculate the CPU time of the program, on UNIX systems, like the return of the CPU work, how long it took to complete the operation, and on Windows can be used in the same way as the difference between work hours.
>> Time.clock ()
3.282732
Time.time ()
This returns the timestamp of the current time (the number of floating-point seconds elapsed after the 1970 era).
>> Time.time ()
1515849662.0594246
You can also calculate the time of the program using the difference between the front and back.
Time.mktime ()
Accepts a tuple of 9 elements, returning a timestamp.
Time.altzone
Returns the number of seconds to offset the daylight saving time region in West Greenwich. If the region returns negative values in eastern Greenwich (such as Western Europe, including the UK). Available for daylight saving time enabled regions.
>> Time.altzone
-28800
>> time.altzone/3600
-8.0
Time.ctime ()
This function takes the arguments of the number of seconds, with the 1970 epoch as the node, and returns a more complete format for the time-describing string, such as null for the parameter, the default parameter is Time.time ().
>> Time.ctime (1000)
' Thu Jan 1 08:16:40 1970 '
>> Time.ctime ()
' Sat Jan 13 21:24:00 2018 '
Time.localtime ()
This function takes a timestamp, returns the formatted local time, followed by the specified method to return the numeric type:
>> Time.localtime ()
Time.struct_time (tm_year=2018, Tm_mon=1, tm_mday=13, tm_hour=21, tm_min=43, tm_sec=45, tm_wday=5, tm_yday=13, TM_ISDST =0)
>> time.localtime (). Tm_yday
13
Time.asctime ()
This function accepts a set of 9 elements of the tuple parameter, the parameter is also the return of Time.localtime (), the returned result is a readable time description string, such as null parameter, the default return the current time.
>> Time.asctime ()
' Sat Jan 13 21:34:19 2018 '
Time.strftime ()
Format the output time string, which requires parameters:
%y Two-digit year representation (00-99)
%Y Four-digit year representation (000-9999)
%m Month (01-12)
One day in%d months (0-31)
%H 24-hour hours (0-23)
%I 12-hour hours (01-12)
%M minutes (00=59)
%s seconds (00-59)
%a Local Simplified Week name
%A Local Full week name
%b a locally simplified month name
%B Local Full month name
%c Local corresponding date representation and time representation
%j Day of the Year (001-366)
%p the equivalent of a local a.m. or p.m.
%u weeks of the year (00-53) Sunday is the beginning of the week
%w Week (0-6), Sunday for the beginning of the week
%W Week of the Year (00-53) Monday is the beginning of the week
%x Local corresponding date representation
%x Local corresponding time representation
%Z the name of the current time zone
Percent% of the number itself
>> time.strftime ("%y/%m/%d_%h:%m:%s")
' 2018/01/13_21:37:01 '
>> time.strftime ("%y%m%d%h%m%s")
' 20180113213718 '
Time.strptime ()
The matching time string is a numeric value that represents time and requires two parameters:
>> time.strptime (' 20180113213718 ', "%y%m%d%h%m%s")
Time.struct_time (tm_year=2018, Tm_mon=1, tm_mday=13, tm_hour=21, tm_min=37, tm_sec=18, tm_wday=5, tm_yday=13, TM_ISDST =-1)
Time.sleep ()
Accept the value of a floating-point or integer to allow the thread to wait for a specified number of seconds.
DateTime function:
Only Datetime.datetime.now () and Datetime.datetime.strptime () are used for the time being:
>> Datetime.datetime.now ()
Datetime.datetime (2018, 1, 13, 22, 4, 27, 857289)
>> datetime.datetime.strptime (' 20180113222027 ', '%y%m%d%h%m%s ')
Datetime.datetime (2018, 1, 13, 22, 20, 27)
>> f = datetime.datetime.strptime (' 20180113222027 ', '%y%m%d%h%m%s ')
>> n = datetime.datetime.now ()
>> F-n
Datetime.timedelta (0, 795, 29355)
>> C = f-n
>> C.seconds
795
>> c.days
0
Obviously, the difference between the subtraction, the first element is the day, the second is the second.
Action on time format in Python3