Python time-processing-time module

Source: Internet
Author: User
Tags local time locale month name

Time Module

There are three ways to represent time:

    • Timestamp: Represents the number of seconds since January 1, 1970 0 o'clock to date
    • Formatted string representation: This representation is more accustomed to our usual reading method, such as 2018-04-24 00:00:00
    • Formatted meta-ancestor means: a ganso with nine elements
Conversion of timestamp and format tuple to each other
ImportTime#importing the time moduleD= Time.time ()#Displays the current timestampdout[16]: 1524570062.944023Time.localtime (d)#Converts a timestamp into a tuple with 9 elements, converted to local timeOUT[17]: Time.struct_time (tm_year=2018, tm_mon=4, tm_mday=24, tm_hour=19, tm_min=41, tm_sec=2, Tm_wday=1, tm_yday=114, tm_isdst=0) Time.gmtime (d)#Converts a timestamp to a tuple with 9 elements, converted to UTC timeOUT[18]: Time.struct_time (tm_year=2018, tm_mon=4, tm_mday=24, tm_hour=11, tm_min=41, tm_sec=2, Tm_wday=1, tm_yday=114, tm_isdst=0) LD= Time.localtime (d)#Ld.tm_year#converted tuples, which can be calculated independently of the corresponding time as requiredOUT[20]: 2018time.mktime (LD)#converting tuples back to timestampsOUT[21]: 1524570062.0

Note: UTC time is GMT, 8 hours later than the East eight zone!

Conversion of timestamps to formatted strings:

Timestamp--------> Format tuple----------> format string

Help (Time.strftime)-On built-inchfunction strftimeinchmodule Time:strftime (...) Strftime (format[, tuple])-String#The format of the function is usedConvert a time tuple to a string according to a format specification. See the Library Reference manual forformatting codes. when the time tuple is  notPresent, current time as returned by LocalTime () isused. Commonly used format codes:%Y year with century as a decimal number. %m Month as a decimal number [01,12]. %d day of the month as a decimal number [01,31]. %H Hour (24-hour clock) as a decimal number [00,23]. %M Minute as a decimal number [00,59]. %s Second as a decimal number [00,61]. %z Time zone offset fromUTC. %a Locale's abbreviated weekday name.%A Locale's full weekday name.%b Locale's abbreviated month name.%B Locale's full month name.%c Locale's appropriate date and time representation.%I Hour (12-hour clock) as a decimal number [01,12]. %p Locale's equivalent of either AM or PM.Other codes is available on your platform. See documentation forThe C library strftime function.
Ld#formatting the tuple's dataOUT[22]: Time.struct_time (tm_year=2018, tm_mon=4, tm_mday=24, tm_hour=19, tm_min=41, tm_sec=2, Tm_wday=1, tm_yday=114, tm_isdst=0) Time.strftime ("%y-%m-%d%h:%m:%s", LD) out[25]:'2018-04-24 19:41:02'Time.strftime ("%y-%m-%d%x", LD) out[26]:'2018-04-24 19:41:02'Time.strftime ("%y-%m-%d%x%p", LD)#time stamp conversion to formatted tupleOUT[27]:'2018-04-24 19:41:02 PM'#convert string to formatted tuple, note format correspondsTime.strptime ('2018-04-24 19:41:02 PM',"%y-%m-%d%h:%m:%s%p") out[+]: Time.struct_time (tm_year=2018, tm_mon=4, tm_mday=24, tm_hour=19, tm_min=41, tm_sec=2, Tm_wday=1, tm_yday=114, tm_ ISDST=-1)
Convert to character format based on timestamp
 help (time.ctime) Help on built -in     function CTime in   module Time:ctime (...) CTime (seconds) -> string Convert a time   In  seconds since the Epoch to a string in   local    Time. This  is  equivalent to Asctime (localtime (seconds)). When the time is tuple is  not  present, current Ti Me as returned by LocalTime () is   Used.time.ctime ( Time.time ()) out[:  " tue APR 24 20:02:46 2018   " 
Convert a tuple to a string based on the struct_time format
Help (Time.asctime) to built-in  module time:asctime (...)     - string        'Sat June 16:26:11 1998'.      is  not present, current time as returned by LocalTime ()      is used.time.asctime (LD) out['Tue Apr 19:41:02 2018'
DateTime module

The Dateimte module encapsulates the time module and provides an interface for more functionality:

See a blog on the CSDN, speak pretty detailed, Bo master banned reprint, so attached link. DateTime module

Python time-processing-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.