Time Introduction
Before you begin, you should first explain these points:
1. In Python, there are usually several ways to represent time: 1) timestamp 2) formatted time string 3) tuple (struct_time) a total of nine elements. Because Python's time module implementation primarily calls the C library, each platform may be different.
2.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.
3. Timestamp (timestamp): Typically, a timestamp represents an offset that is calculated in seconds, starting January 1, 1970 00:00:00. We run "type (Time.time ())" and return the float type. The function that returns the Timestamp method mainly has time (), clock () and so on.
4. Tuple (struct_time) mode: Struct_time tuple has 9 elements, the function that returns Struct_time mainly has Gmtime (), localtime (), Strptime (). Several elements in this way tuple are listed below:
indexing (index) |
Properties (Attribute) |
value (values) |
0 |
Tm_year (year) |
Like 2011. |
1 |
Tm_mon (month) |
1-12 |
2 |
Tm_mday (Sun) |
1-31 |
3 |
Tm_hour (Time) |
0-23 |
4 |
Tm_min (min) |
0-59 |
5 |
Tm_sec (SEC) |
0-61 |
6 |
Tm_wday (Weekday) |
0-6 (0 = Sunday) |
7 |
Tm_yday (day ordinal of the year) |
1-366 |
8 |
TM_ISDST (whether it is daylight saving time) |
Default is-1 |
Conversion diagram:
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.
1 Import Time 2 time.time () 3 1468204341.421
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 ().
>>> later = Time.time () +5>>> Time.ctime (later)'Mon Jul 09:46:22 '>>> time.ctime ()'Mon Jul 09:46:35'>>> later = Time.time () +50>>> time.ctime ()'Mon Jul 09:46:47'>> > time.ctime (later)'Mon Jul 09:47:34'
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.
defShow_struct (s):Print 'tm_year:', S.tm_yearPrint 'Tm_mon:', S.tm_monPrint '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_ydayif __name__=='__main__': #a = ' 2016-07-07 00:00:00 ' #B = ' 2016-07-06 00:00:00 ' #print Isostring2time (a) #print isostring2time (b)show_struct (Time.gmtime ())Print '*'*50show_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.
Import Time def FUCN (): time.sleep (5) print"Hello,world"
Execute the above code and wait 5 seconds before outputting the information.
Python-time Module