Time module of python module, time module of python Module

Source: Internet
Author: User
Tags format definition

Time module of python module, time module of python Module
Time

  • Three Forms of time
    • Timestamp: Generally, the timestamp indicates the offset calculated in seconds from 00:00:00, January 1, January 1, 1970. We run "type (time. time ()" and return the float type.
    • Formatted time String (Format String): '2017-12-06'
      • ''' % Y two-digit year indicates (00-99) % Y four-digit year indicates (000-9999) % m month (01-12) one day (0-31) % H 24-hour (0-23) % I 12-hour (01-12) in % d month) % M minutes (00 = 59) % S seconds (00-59) % a local simplified week name % A local full week name % B local simplified month name % B local full month name % c local corresponding date representation and Time Representation % j Years one day (001-366) % p local. m. or P. m. % U the number of weeks in a year (00-53) Sunday is the beginning of the week % w Week (0-6 ), sunday is the beginning of a week % W the number of weeks in a year (00-53) monday is the start of the week % x local corresponding date represents % X local corresponding time represents % Z Current Time Zone name % Number itself '''Time Format symbol
    • Tuples (struct_time): struct_time tuples have nine elements in total: (year, month, day, hour, minute, second, week of the year, day of the year, day of the year, etc.
      • "Index Attribute value (Values) 0 tm_year (year) for example 20111 tm_mon (month) 1-122 tm_mday (day) 1-313 tm_hour (hour) 0-234 tm_min (minute) 0-595 tm_sec (second) 0-616 tm_wday (weekday) 0-6 (0 indicates Sunday) 7 tm_yday (day of the year) 1-3668 tm_isdst (when it is enabled or not) is-1 "" by default """Element meaning of tuples

     

  • Several formats of time in python
    • # Import time module> import time # timestamp> time. time () 1500875844.800804 # time string >>> time. strftime ("% Y-% m-% d % X") '2017-07-24 13:54:37 '> time. strftime ("% Y-% m-% d % H-% M-% S") '2017-07-24 13-55-04 '# Time tuples: localtime converts a timestamp to struct_time of the current time zone.
      Time. gmtime () time. localtime () time. struct_time (tm_year = 2017, tm_mon = 7, tm_mday = 24, tm_hour = 13, tm_min = 59, tm_sec = 37, tm_wday = 0, tm_yday = 205, tm_isdst = 0) # conclusion: The timestamp is the time that can be recognized by the computer; the time string is the time that people can understand; and The tuples are used to operate the time.
  • Conversion between various formatting times

# Timestamp --> structured time # time. gmtime (timestamp) # UTC time, consistent with the local time in London # time. localtime (timestamp) # local time. For example, we now run this method in Beijing: the difference between this method and UTC time is 8 hours, And the UTC time + 8 hours = Beijing time >>> time. gmtime (1500000000) time. struct_time (maid = 2017, tm_mon = 7, tm_mday = 14, tm_hour = 2, tm_min = 40, tm_sec = 0, tm_wday = 4, tm_yday = 195, tm_isdst = 0) >>> time. localtime (1500000000) time. struct_time (tm_year = 2017, tm_mon = 7, tm_mday = 14, tm_hour = 10, tm_min = 40, tm_sec = 0, tm_wday = 4, tm_yday = 195, tm_isdst = 0) # structured time --> timestamp # time. mktime (structured time) >>> time_tuple = time. localtime (1500000000) >>> time. mktime (time_tuple) 1500000000.0
# Structured time --> string time # time. strftime ("format definition", "structured time") if the structured time parameter is not passed, the actual current time >>> time. strftime ("% Y-% m-% d % X") '2017-07-24 14:55:36 '> time. strftime ("% Y-% m-% d", time. localtime (1500000000) '2017-07-14 '# string time --> structured time # time. strptime (time string, string format) >>> time. strptime ("2017-03-16", "% Y-% m-% d") time. struct_time (maid = 2017, tm_mon = 3, tm_mday = 16, tm_hour = 0, tm_min = 0, tm_sec = 0, tm_wday = 3, tm_yday = 75, tm_isdst =-1) >>> time. strptime ("07/24/2017", "% m/% d/% Y") time. struct_time (tm_year = 2017, tm_mon = 7, tm_mday = 24, tm_hour = 0, tm_min = 0, tm_sec = 0, tm_wday = 0, tm_yday = 205, tm_isdst =-1)

 

# Structured time --> % a % B % d % H: % M: % S % Y string # time. asctime (structured time) If no parameter is input, the formatted string of the current time is directly returned> time. asctime (time. localtime (1500000000) 'fri Jul 14 10:40:00 100'> time. asctime () 'mon Jul 24 15:18:33 100' # % a % d % H: % M: % S % Y string --> structured time # time. ctime (timestamp) If no parameter is input, the formatted string of the current time is directly returned >>> time. ctime () 'mon Jul 24 15:19:07 2017 '> time. ctime (1500000000) 'fri Jul 14 10:40:00 123'
Import timetrue_time = time. mktime (time. strptime ('2017-09-11 08:30:00 ',' % Y-% m-% d % H: % M: % s') time_now = time. mktime (time. strptime ('2017-09-12 11:00:00 ',' % Y-% m-% d % H: % M: % s') dif_time = time_now-true_timestruct_time = time. gmtime (dif_time) print ('% d years % d months % d days % d hours % d minutes % d seconds' % (struct_time.tm_year-1970, struct_time.tm_mon-1, struct_time.tm_mday-1, struct_time.tm_hour, struct_time.tm_min, struct_time.tm_sec ))Calculation time difference

 

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.