Python Time module

Source: Internet
Author: User

In Python, there are usually three ways to represent time: timestamp, tuple (struct_time), formatted time string

(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) Time tuple (struct_time): Struct_time A total of 9 elements total nine elements: (year, month, day, hour, minute, second, week of the year, Day of the year, etc.)

(3) formatted time string (format string): ' 1999-12-06 '%y-%m-%d%x ' and so on

Import Time Module>>>Import Time#time Stamp>>>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 tuple: localtime converts a timestamp to the struct_time of the current time zonetime.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)

Summary: Time stamp is the time that the computer can recognize; The time string is the time that a person can read; Tuples are used to manipulate time.

#Timestamp-- structured time#time.gmtime (timestamp) #UTC时间, consistent with local time in London, UK#time.localtime (timestamp) #当地时间. For example, we are now implementing this method in Beijing: 8 hour difference from UTC time, UTC time + 8 hour = Beijing Time>>>time.gmtime (1500000000) time.struct_time (tm_year=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-time stamping#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 does not pass, the current time is displayed>>>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 corresponding format)>>>time.strptime ("2017-03-16","%y-%m-%d") time.struct_time (tm_year=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)
Calculate the time difference:ImportTimetrue_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 months%d%d days%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))

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.