Python Time module

Source: Internet
Author: User

In Python, there are usually several ways to represent time:

1. 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.

2. Formatted time string (format string)

3. Structured time (Struct_time): Struct_time A total of nine elements in a total of 9 elements: (year, month, day, hour, minute, second,
The first week of the year, the day of the year, daylight saving time)

Time stamp floating point number form
Import>>> time.time (1513683626.272084)

Python is born in 1970 this timestamp is python1970 year of birth to now with decimals show time

>>> t = time.time ()

Structime is Ganso format
Returns the local time struct times format
There are 9 elements in a total of nine elements in the Struct_time tuple: (year, month, day, time, minute, second,
The first week of the year, the day of the year, daylight saving time)
Print (Time.localtime ()) time.struct_time (tm_year=2017, tm_mon=12, tm_mday=19, tm_hour=11, tm_min=49, tm_sec=14, Tm_wday=1, tm_yday=353, tm_isdst=0)

Tm_wday Monday to Friday starting from 0 several

attribute                            value tm_year (year)                  such as Tm_mon (month)                   1-12 tm_mday (day)                  1-31 tm_hour (time) 
    - tm_min (min                   )- tm_sec (sec)                   - tm_wday (weekday)             -6
   
     (0 = Sunday) tm_yday (Day of the year)        1-366
     tm_isdst (Daylight saving time)        defaults to 1
   

Time.sleep () Seconds The program is stationary how many seconds

time.gmtime () Struct_time in UTC time Zone

>>> Time.gmtime (time.struct_time) (Tm_year=2017, tm_mon=12, tm_mday=19, tm_hour=17, tm_min=26, tm_ sec=49, Tm_wday=1, tm_yday=353, tm_isdst=0)

Take the value of the tuple inside

>>> time.localtime () time.struct_time (tm_year=2017, tm_mon=12, tm_mday=20, Tm_hour=1, Tm_min=22, TM _sec=16, tm_wday=2, tm_yday=354, tm_isdst=0)>>> t = time.localtime ()>>> >> > >>> t.tm_year2017>>> t.tm_mon12>>> t.tm_mday20>> > t.tm_hour1>>> t.tm_min33

Which day of the year 1970 is printed

>>> t = time.localtime (1232131)>>> ttime.struct_time (tm_year=1970, Tm_mon=1, tm_ Mday=15, tm_hour=14, tm_min=15, tm_sec=31, tm_wday=3, tm_yday=15, tm_isdst=0)print("  This is 1970 day:%d"  percent is 1970 day:15
time.mktime ()   The struct time tuple format is converted to timestamp format
>>> ttime.struct_time (tm_year=1970, Tm_mon=1, tm_mday=15, tm_hour=14, tm_min=15, tm_sec=31, Tm_wday =3, tm_yday=15, tm_isdst=0)>>> >>> time.mktime (t)1232131.0

Time.strftime (format[, tuple]), string format, plus struct time tuple

timestamps are converted into formatted time string

>>> time.strftime ("%y-%m-%d%h:%m", T)'1970-01-26 00:15  '

Python format symbols in time Date:

%shorthand for a week. As Wednesday for Web%full write of a week. As in Wednesday for Wednesday%shorthand for month B. If April is Apr%Full write of month B. As in April for April%c: string representation of DateTime. (such as: 04/07/10 10:43:39)%D: The number of days in the month (the day of the week)%f: microseconds (range [0,999999])%H: Hours (24-hour, [0, 23])%I: Hours (12-hour, [0, 11])%j: Number of days in the year [001,366] (the day of the year)%m: Month ([01,12])%M: minutes ([00,59])%p:am or PM%s: Seconds (the range is [00,61], why not [00, 59], refer to the Python manual ~_~)%U: Week in the week of the current year , Sunday as the first day of the week%w: Today in this week's days, the range is [0, 6],6 says Sunday%W: Week of the Week in the year (the week of the year), Monday as the first day of the week%x: Date string (for example: 04/07/10)%x: Time string (for example: 10:43:39)%Y:2 the year represented by a number%Y:4 the year represented by a number%z: Interval from UTC time (if local time, returns an empty string)%Z: Time zone name (if local time, returns an empty string)Percent:%

Time.strptime (string, format) formatted time string converted to timestamp

The parameter is a formatted time string, formatted

>>> T =time.localtime ()>>>Ttime.struct_time (Tm_year=2017, tm_mon=12, tm_mday=20, tm_hour=11, tm_min=58, Tm_sec=32, tm_wday=2, tm_yday=354, tm_isdst=0)>>> Time.strftime ("%y-%m-%d%h:%m", T)'2017-12-20 11:58'>>> Time.strptime ("2017-12-20 11:58","%y-%m-%d%h:%m") time.struct_time (tm_year=2017, tm_mon=12, tm_mday=20, tm_hour=11, tm_min=58, Tm_sec=0, tm_wday=2, tm_yday=354, Tm_isdst=-1)>>>

Time.asctime ()

Asctime ([tuple]), string

Default print local time if tuple parameter is not passed

>>> t = time.localtime (2313)>>> time.asctime (t)'Thu Jan  1 08:38:33 1970'

>>> time.asctime ()'Wed Dec 16:55:27'

Time.ctime ()

CTime (timestamp), string

Default print local time if time stamp parameter is not passed

>>> t = time.time ()>>> t1513760447.9049652>>> t = time.time () >>> time.ctime (t)'Wed Dec 17:00:58'

Time format conversion diagram:

Python Time module

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.