Turn from
The time module provides some C library functions for managing times and dates, because it is bound to the underlying C implementation, so some details are based on the specific platform.
I. Wall clock time 1.time ()
The core function of the time module is time (), which returns the number of seconds from the beginning of the era, the return value is a floating point, and the specific precision depends on the platform.
>>>Import time >>>time.time ()1460599046.85416
2.ctime ()
Floating-point numbers are typically used to store and compare dates, but not friendly to humans, and to record and print time, you can use CTime ().
>>>Import time >>>time.ctime ()'Thu Apr 14 10:03:58 2016 '>>> later = Time.time () +5>>> Time.ctime (later)'Thu Apr 14 10:05:57'
Two. Processor clock time
Clock () returns the processor clock time, and its return value is typically used for performance testing and benchmarking. Therefore, they reflect the actual running time of the program.
>>>Import time >>>time.clock ()0.07
Three. Time composition
The time module defines struct_time to maintain times and dates, where each component is stored separately for access.
Import TimedefShow_struct (s):Print 'tm_year: ", S.tm_year Print 'Tm_mon: ", S.tm_mon Print "Tm_mday:", S.tm_mdayPrint "Tm_hour:", S.tm_hourPrint "tm_min:", S.tm_minPrint "tm_sec:", S.tm_secPrint "Tm_wday:", S.tm_wdayPrint "Tm_yday:", S.tm_ydayshow_struct (Time.gmtime ()) show_struct (Time.localtime ())
Gmtime () is used to get the UTC time, LocalTime () is used to get the current time zone, UTC time is actually Greenwich mean time, and it has a eight hour lag with China.
Locatime () = Gmtime () + 8hour
Four. Processing time zone 1. Get the Difference
>>>Import time >>>time.timezone/3600-8
2. Set the time zone
ZONES = ["GMT""Europe/amsterdam '] for in ZONES: os.environ["TZ"] = zone time.tzset ()
Five. Parsing and formatting time
The time module provides two functions Strptime () and Strftime (), which can be converted between the struct_time and the value strings.
1.strptime ()
Used to convert the string time into Struct_time format:
>>> now=time.ctime ()>>> time.strptime (now) time.struct_time (tm_year=2016, tm_ Mon=4, tm_mday=14, tm_hour=10, tm_min=48, tm_sec=40, tm_wday=3, tm_yday=105, Tm_isdst=-1)
2.strftime ()
Formatted output for TIME
from Import gmtime, strftime>>> strftime ("%a,%d%b%Y%h:%m:%s +0000", Gmtime ()) 'Thu, June 2001 14:17:15 +0000'
3.mktime ()
A floating-point number that is used to convert struct_time into time
>>>fromimport mktime, gmtime>>>mktime (Gmtime ()) 1460573789.0
Six. Sleep ()
The sleep function is used to hand over the current thread, asking it to wait for the system to wake it up again, and if the write program has only one thread, it will actually block the process and do nothing.
Python Time Module Learning