Python Time module

Source: Internet
Author: User

Objective:

Environment: The Python interpreter is the Windows platform 3.6.1 version.

The main use in Python is 1. Tuples (Struct_time objects) 2. Formatted time string 3. The time stamp of these three forms represents time. This paper mainly describes the use of the built-in functions of the temporal module to transform each other in three forms.

 fromTimeImport*Tuple1= (2017, 9, 23, 14, 45, 10, 0, 0, 0)defdemo1 ():"""Struct_time constructor Function"""struct_time1= Struct_time (Tuple1)#converts a tuple of 9 elements into an object of the Struct_time class    Print(Type (struct_time1), struct_time1)defDemo2 ():"""strftime function Usage: Converts a tuple (Struct_time class is a subclass of a tuple class) to a string"""strftime1= Strftime ('%y-%m-%d%h:%m:%s%A%p%A%z%b%b%I%c')    Print(Type (strftime1), strftime1) strftime2= Strftime ('%y-%m-%d', Tuple1)Print(Type (strftime2), strftime2) strftime2= Strftime ('%y-%m-%d%h:%m:%s', LocalTime ())Print(Type (strftime2), strftime2)defDemo3 ():"""demonstrates the use of the CTime function"""ctime1= CTime ()#converts a timestamp directly from a local time to a string representation of a time    Print(Type (ctime1), ctime1) ctime2= CTime (1500000000)#converts a string representation of a time directly from a timestamp    Print(Type (ctime2), ctime2)defDemo4 ():"""demonstrates the use of the Strptime function"""strptime1= Strptime ('2017-09-21 15:07:38','%y-%m-%d%h:%m:%s')    Print(Type (strptime1), strptime1)Print(Strptime1.tm_yday)defDemo5 ():"""demonstrates the use of gmtime and localtime functions"""gmtime1= Gmtime (1500000000)#converts a timestamp into an object of the Struct_time class (prime time)    Print(Type (gmtime1), gmtime1) gmtime1= Gmtime ()#Converts the timestamp of the current time to the object of the Struct_time class (prime time),    Print(Type (gmtime1), gmtime1) localtime1= LocalTime (1500000000)#converts a timestamp to an object of the Struct_time class (local time)    Print(Type (localtime1), localtime1)Print(Type (localtime ()), localtime ())#Converts the timestamp of the current time to the object of the Struct_time class (local time)defDemo6 ():"""demonstrates the use of the Mktime function"""mktime1= Mktime (Tuple1)#Convert tuples to timestamps    Print(Type (mktime1), mktime1)
Def Demo7 ():
"""
Demonstrate the use of the time function
"""
Print (Time ())
Print (Mktime (localtime ()))
Demo1 () Demo2 () Demo3 () Demo4 () Demo5 () Demo6 ()
Demo7 ()
‘‘‘
Format characters used by Strptime and strftime:
    • %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

Output: (partial output may change depending on the time the program is running)

<class ' Time.struct_time ' > Time.struct_time (tm_year=2017, tm_mon=9, tm_mday=23, tm_hour=14, tm_min=45, tm_sec= Tm_wday=0, tm_yday=0, tm_isdst=0)
<class ' str ' > 2017-09-21 19:16:19 Thursday PM Thu +0800 Sep September Thu Sep 21 19:16:19 2017
<class ' str ' > 2017-09-23
<class ' str ' > 2017-09-21 19:16:19
<class ' str ' > Thu Sep 21 19:16:19 2017
<class ' str ' > Fri Jul 14 10:40:00 2017
<class ' Time.struct_time ' > Time.struct_time (tm_year=2017, tm_mon=9, tm_mday=21, tm_hour=15, tm_min=7, tm_sec=38 , Tm_wday=3, tm_yday=264, Tm_isdst=-1)
264
<class ' Time.struct_time ' > 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)
<class ' Time.struct_time ' > Time.struct_time (tm_year=2017, tm_mon=9, tm_mday=21, tm_hour=11, tm_min=16, tm_sec= Tm_wday=3, tm_yday=264, tm_isdst=0)
<class ' Time.struct_time ' > 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)
<class ' Time.struct_time ' > Time.struct_time (tm_year=2017, tm_mon=9, tm_mday=21, tm_hour=19, tm_min=16, tm_sec= Tm_wday=3, tm_yday=264, tm_isdst=0)
<class ' float ' > 1506149110.0

1505993665.347068
1505993665.0

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.