Python time module, pythontime

Source: Internet
Author: User

Python time module, pythontime

Preface:

Environment: python interpreter is version 3.6.1 on windows.

Python mainly uses 1. tuples (struct_time object) 2. formatted time string 3. timestamp represents the time. this article describes how to use the built-in functions of the time module to convert each other in three forms.

 

From time import * tuple1 = (2017, 9, 23, 14, 45, 10, 0, 0) def demo1 (): "struct_time constructor" struct_time1 = struct_time (tuple1) # convert the tuples of nine elements to the print (type (struct_time1), struct_time1) object of the struct_time class) def demo2 (): "strftime function usage: sets the tuples (the struct_time class is a subclass of the tuple class) convert to 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) def demo3 (): "demonstrate ctime function usage" ctime1 = ctime () # convert the timestamp of local time directly to the string representation of time. print (type (ctime1 ), ctime1) ctime2 = ctime (1500000000) # convert the timestamp directly to the time string representation print (type (ctime2), ctime2) def demo4 (): "demonstrate strptime function usage" strptime1 = strptime ('2017-09-21 15:07:38 ',' % Y-% m-% d % H: % M: % s') print (type (strptime1), strptime1) print (strptime1.tm _ yday) def demo5 (): "demonstrate the usage of gmtime and localtime functions" gmtime1 = gmtime (1500000000) # convert a time stamp to an object of the struct_time class (primary meridian time) print (type (gmtime1 ), gmtime1) gmtime1 = gmtime () # convert the timestamp of the current time to an object of the struct_time class (local meridian time), print (type (gmtime1), gmtime1) localtime1 = localtime (1500000000) # convert the timestamp to the print _time Class Object (local time) print (type (localtime1), localtime1) print (type (localtime (), localtime ()) # convert the timestamp of the current time to the object of the struct_time class (local time) def demo6 (): "demonstrate mktime function usage" mktime1 = mktime (tuple1) # convert a tuple to a timestamp print (type (mktime1), mktime1)
Def demo7 ():
"""
Demonstrate the usage 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 (00-99)
  • % Y indicates the four-digit year (000-9999)
  • % M month (01-12)
  • One day in % d month (0-31)
  • % H hour in 24-hour format (0-23)
  • % I 12-hour (01-12)
  • % 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 Date and Time
  • % J one day in the year (001-366)
  • % P local equivalent of A. M. or P. M.
  • % U number of weeks in a year (00-53) Sunday is the start of the week
  • % W Week (0-6), Sunday is the beginning of the week
  • % W number of weeks in a year (00-53) Monday is the start of the week
  • % X local date Representation
  • % X Local Time Representation
  • % Z Current Time Zone name
  • % Itself

Output: (some outputs will change due to the time when the program runs)

<Class 'time. struct_time '> time. struct_time (maid = 2017, tm_mon = 9, tm_mday = 23, tm_hour = 14, tm_min = 45, tm_sec = 10, tm_wday = 0, tm_yday = 0, tm_isdst = 0)
<Class 'str'> 19:16:19 Thursday PM Thu + 0800 Sep September 07 Thu Sep 21 19:16:19 2017
<Class 'str'>
<Class 'str'> 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 = 19, 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 = 19, tm_wday = 3, tm_yday = 264, tm_isdst = 0)
<Class 'float'> 1506149110.0

1505993665.347068
1505993665.0

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.