Time module of [Python]

Source: Internet
Author: User
Tags epoch time julian day local time month name parse string

The time module provides a variety of functions and ways to identify the timing value, there are two main formats for identifying time: one is identified as a timestamp, which is the number of seconds from January 1, 1970 0:0, 0 seconds to the current time, a total of bytes experienced, a floating point decimal mark, The epoch time is also dependent on the individual's operating system, while the other identifies the time in the form of a tuple containing 9 elements (identifying local times). The 9 elements of the tuple are:

Year (four digits, e.g. 1998)
Month (1-12)
Day (1-31)
Hours (0-23)
Minutes (0-59)
Seconds (0-59)
Weekday (0-6, Monday is 0)
Julian Day, 1-366
DST (daylight Savings time) flag ( -1, 0 or 1)
If the DST flag is 0, the time was given in the regular time zone;
If it is 1, the time was given in the DST time zone;
If it is-1, mktime () should guess based on the date and time.

A brief introduction to the time functions commonly used is given here:

Asctime ([tuple]), string Convert time tuple to string
CTime (seconds), string Convert time in seconds to string
Strftime (format[, tuple]), string Convert time tuple to string according to format specification
Strptime (string, format), Struct_time Parse string to time tuple according to format specification
Clock (), floating point number Return CPU time since process start as a float

Mktime (tuple), floating point number

Convert local time tuple to seconds since Epoch

Floating point number, time ()

Return current time in seconds since the Epoch as a float

Gmtime ([seconds]) (Tm_year, Tm_mon, Tm_mday, Tm_hour, Tm_min, Tm_sec, Tm_wday, Tm_yday, TM_ISDST)

Convert seconds since Epoch to UTC tuple

LocalTime ([seconds]) (Tm_year,tm_mon,tm_mday,tm_hour,tm_min, TM_SEC,TM_WDAY,TM_YDAY,TM_ISDST)

Convert seconds since Epoch to local time tuple
Sleep (seconds) Delay for a number of seconds given as a float

1. Time.asctime ([tuple]), string

Converts a time (tuple) to a string that defaults to the current time.

1 >>> time.asctime ()2'Sun Oct 10:00:57'3 >>> time.asctime (Time.localtime ())4'Sun Oct 10:02:22 '

2. Time.clock (), floating point number

Returns the CPU time or current real time at which the process starts or calls clock (), identifies the actual time at which the program was run on the first call, identifies the time interval from the first invocation to this call, and the number of floating-point seconds.

1 >>> time.clock () 2 0.01

3. Time.ctime (seconds), string

Returns the time since the Epoch Time (general 1970) seconds time, and if no seconds is provided, the return value is identified as a string in the current time.

1 >>> time.ctime ()2'Sun Oct 10:24:08'3 >>> time.ctime (34124)4'Thu Jan  1 17:28:44 1970'  5 >>> time.ctime (3412456780)6'Sat 19 08:39:40 2078'

4. Time.gmtime ([seconds]) (Tm_year, Tm_mon, Tm_mday, Tm_hour, Tm_min, tm_sec, Tm_wday, Tm_yday , TM_ISDST)

Returns the time since the Epoch Time (general 1970) seconds time; if seconds is not provided, the current time is assumed to return a UTC time format in the form of a 9 element tuple, which can be converted to a friendly time format by Time.asctime ().

 1  >>> Time.gmtime ()  2  time.struct_time (tm_year=2016, tm_mon=10, tm_mday=30, tm_hour=2, tm_min=30, tm_sec=0, Tm_wday=6, tm_yday=304, Tm_isdst=0)  3  > >> time.gmtime (423545 4  Time.struct_time (tm_year=1970, Tm_mon=1, tm_mday=5, tm_hour=21, tm_min=39, tm_sec=5, tm_wday=0, tm_yday=5, tm_isdst=< Span style= "COLOR: #000000" >0)  5  >>>  Time.asctime (Time.gmtime ())  6   " sun Oct 02:34:58   " 

5. Time.localtime () (Tm_year, Tm_mon, Tm_mday, Tm_hour, Tm_min, tm_sec, Tm_wday, Tm_yday , TM_ISDST)

Returns the time since the Epoch Time (general 1970) seconds time; if seconds is not provided, the current time is assumed to return a local time format in the form of a 9 element tuple, which can be converted to a friendly time format by Time.asctime ().

1 >>> time.localtime ()2 time.struct_time (tm_year=2016, tm_mon=10, Tm_mday=30, TM _hour=10, tm_min=41, tm_sec=5, tm_wday=6, tm_yday=304, tm_isdst=0)3 >>> time.localtime ( 76899)4 time.struct_time (tm_year=1970, Tm_mon=1, tm_mday=2, tm_hour=5, tm_min=21, tm_sec=39, tm_ Wday=4, tm_yday=2, tm_isdst=0)5 >>> time.asctime (Time.localtime ())6 ' Sun Oct 10:41:50 '

6. Time.mktime (tuple), floating point number

Converts the given tuple time to floating-point seconds, which is the number of seconds from the Epoch Time (1970) to the given time.

1 >>> time.mktime (Time.localtime ())2 1477795487.03 >>>  Time.mktime (Time.gmtime ())4 1477766742.05 >>> time.mktime (time.gmtime (76384))  6 47584.0

7. Time.sleep (seconds)

Delays the execution of the program process after the seconds time.

8. Time.strftime (format[, tuple]), string

Converts a 9-element time tuple to a given time format and outputs it as a string, or local time if not specified. The specific format see the Library Reference manual for formatting codes.

 1  >>> a = (2016,2,12,12,12,12,4,5,6 2  >>> Time.mktime (a)  Span style= "COLOR: #008080" >3  1455250332.04  >>> time.strftime ( Span style= "COLOR: #800000" > " %y%m%d%h%m%s   " , a)  5   "  20160212121212   " 

Python format symbols in time Date:
%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

9. Time.strptime (string, format), Struct_time

Resolves a string to a time tuple in the given time format. The specific format see the Library Reference Manual for formatting codes (same as strftime ()).

Floating point number, time.time ())

Returns the timestamp from the Epoch Time (1970) to the current time, identified by the number of floating-point seconds.

1 >>> time.time ()  2 1477799217.188667 3 >>> time.time ()  4 1477799244.483566 5 >>> time.time ()  6 1477799245.771593 7 >>> time.time ()  8 1477799246.657197 9 >>> time.time ()  Ten 1477799247.236571

Time module of [Python]

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.