Python Standard library Introduction--12 Time module detailed

Source: Internet
Author: User
Tags assert local time

The ==time module = = ' Time ' module provides some functions for processing dates and times of the day. It is a simple package built on the C run-time library. The given date and time can be expressed as floating point (from the reference time, usually 1970.1.1 through the current number of seconds. That is, Unix format), or a struct (a tuple of tuples) that represents time. = = = Get current time ===[example 1-79 #eg-1-79] shows how to use the ' time ' module to get the current times. ====example 1-79. Use the time module to get the current ====[eg-1-79] "File:time-example-1.pyimport TimeNow = Time.time () Print now, seconds since, Time.gmtime (0) [: 6]printprint "or in other words:" Print "-local time:", Time.localtime (now) print "-UTC:", Time.gmtime (No  W) *b*937758359.77 seconds Since (1970, 1, 1, 0, 0, 0) or in other words:-local time: (1999, 9, 19, 18, 25, 59, 6, 262, 1)- UTC: (1999, 9, 6, 262, 0) *b* ' ' localtime ' and ' Gmtime ' return the class tuple including year, month, day, time, minute, second, week, Day of the year, daylight sign. The year was a four-digit number (otherwise specified on the Millennium bug platform, but four digits), and the week began in Monday (the number 0), January 1 is the first day of the year. = = = Converts a time value to a string = = = You can convert a time object to a string using a standard format string, but the ' Times ' module already provides a number of standard conversion functions, as shown in [Example 1-80 #eg-1-80]. ====example 1-80. Format time output ====[EG-1-80] "File:time-example-2.pyimport timenow = Time.localtime (Time.time ()) Print Time.aSctime (now) print time.strftime ("%y/%m/%d%h:%m", now) print Time.strftime ("%a%b%d", now) print time.strftime ("%c", now)  Print Time.strftime ("%I%p", now) print Time.strftime ("%y-%m-%d%h:%m:%s%Z", now) # does it by Hand...year, month, day, hour, Minute, second, weekday, yearday, daylight = nowprint "%04d-%02d-%02d"% (year, month, day) print "%02d:%02d:%02d"% (hour , minute, second) print ("MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN") [weekday], Yearday*b*sun Oct 10 21:39:24 199999/10 /10 21:39sun Oct 10Sun Oct 21:39:24 199909 pm1999-10-10 21:39:24 cest1999-10-1021:39:24sun 283*b* "' = = = Convert String to time object = = = In On some platforms, the ' time ' module contains the ' Strptime ' function, which has the opposite effect of ' strftime '. Given a string and a pattern, it returns the corresponding time object, as shown in [Example 1-81 #eg-1-81]. ====example 1-81. Use the Time.strptime function to parse the time ====[eg-1-81] "' File:time-example-6.pyimport time# make sure we have a strptime function!# confirm that there is a function s Trptimetry:strptime = time.strptimeexcept attributeerror:from strptime import strptimeprint strptime ("00", "%d%b%y") print Strptime ("1 Jan 1:30pm", "%d%b%y%i:%m%p") ' ' time.strptime ' function can only be used when the system's C library provides the corresponding function. For platforms that do not provide a standard implementation, [Example 1-82 #eg-1-82] provides an incomplete implementation. ====example 1-82. Strptime implementation ====[eg-1-82] ' File:strptime.pyimport reimport stringmonths = ["Jan", "Feb", "Mar", "APR", "may", "June", "Jul" ","]spec "," Sep "," Oct "," Nov "," Dec "= {# map formatting code to a regular expression fragment"%a ": "(? p<weekday>[a-z]+) ","%A ":" (? p<weekday>[a-z]+) ","%b ":" (? p<month>[a-z]+) ","%B ":" (? p<month>[a-z]+) ","%c ":" (? P<century>\d\d?) ","%d ":" (? P<day>\d\d?) ","%d ":" (? P<month>\d\d?) /(? P<day>\d\d?) /(? p<year>\d\d) ","%e ":" (? P<day>\d\d?) ","%h ":" (? p<month>[a-z]+) ","%H ":" (? P

  

Python Standard library Introduction--12 Time module detailed

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.