Python Standard library time

Source: Internet
Author: User
Tags month name

Original: http://www.cnblogs.com/qq78292959/archive/2013/03/22/2975786.html

Official Python documentation

In the program, the inevitable and time to deal with, to learn the Times module, first to understand a few concepts.

1. Timestamp :unix timestamp (Unix timestamp), or Unix time (Unix times), POSIX time, is a time representation, Defined as the total number of seconds from GMT January 01, 1970 00:00 00 seconds (Beijing time January 01, 1970 08:00 00 seconds) to now. Unix timestamps are used not only in UNIX systems, Unix-like systems (such as Linux systems), but also in many other operating systems.

The get command for the Linux timestamp is # date +%s

2. UTC(coordinated Universal time, world co-ordination) is GMT, world standard Time. In China for Utc+8. DST (daylight saving time) is daylight saving time.

3. Struct_time way: Struct_time tuple has 9 elements, the function that returns Struct_time is mainly Gmtime (), localtime (), Strptime (). Several elements in this way tuple are listed below:

indexing (Index) Properties (Attribute) value (values)
0 Tm_year (year) Like 2011.
1 Tm_mon (month) 1-12
2 Tm_mday (Sun) 1-31
3 Tm_hour (Time) 0-23
4 Tm_min (min) 0-59
5 Tm_sec (SEC) 0-61
6 Tm_wday (Weekday) 0-6 (0 = Sunday)
7 Tm_yday (day ordinal of the year) 1-366
8 TM_ISDST (whether it is daylight saving time) Default is-1

>>>Import Time>>> Time.time () # Gets the timestamp of the current time 1428317991.951298>>> Time.localtime () # converts a timestamp to the struct_time of the current time zone Time.struct_time (tm_year=2015, tm_mon=4, tm_mday=6, tm_hour=19, Tm_min=1, Tm_sec=1, tm_wday=0, tm_yday=96, tm_isdst=0)>>> Time.localtime (1428317991.951298) time.struct_time (tm_year=2015, tm_mon=4, tm_mday=6, tm_hour=18, tm_min=59, tm_sec=51, tm_wday=0, tm_yday=96, tm_isdst=0)>>>Time.localtime (Time.time ()) Time.struct_time (Tm_year=2015, tm_mon=4, tm_mday=6, tm_hour=19, Tm_min=1, tm_sec=12, tm_wday=0, tm_yday=96, tm_isdst=0)

time.gmtime () converts a timestamp to the struct_time of the UTC time Zone (0 o'clock Zone), while Time.localtime () is the current time zone Struct_time

>>> Time.gmtime (time.struct_time) (Tm_year=2015, tm_mon=4, tm_mday=6, tm_hour=11, tm_min=7, tm_sec= Wu, Tm_wday=0, tm_yday=96, tm_isdst=0)


time.mktime () converts a struct_time to a timestamp

 >>> time.mktime (Time.localtime ())1428318458.0

time.sleep () thread delays the specified time run

Time.clock () on UNIX, it returns "process time", which is a floating-point number (timestamp) in seconds. In Windows, the first call returns the actual time that the process is running. The second subsequent call is the elapsed time since the first call to the present. (actually based on QueryPerformanceCounter () on WIN32, which is more accurate than milliseconds)

Import Timeif __name__=='__main__': Time.sleep (1)    Print('Clock1%s'%Time.clock ()) Time.sleep (1)    Print('Clock2%s'%Time.clock ()) Time.sleep (1)    Print('Clock3%s'% Time.clock ())

Clock1 0.013094
Clock2 0.013231
Clock3 0.013316

Time.asctime () represents a tuple or struct_time representing time in this form Sun June 20 23:21:05 1993 If there are no arguments, the time.localtime () is passed in as a parameter.

>>> time.asctime ()'Mon Apr  6 19:21:17'

time.ctime () takes a timestamp (floating-point number in seconds, Note that timestamp is not struct_time) into the form of time.asctime (). If the parameter is not given or is none, the default Time.time () is the parameter. Its function is equivalent to Time.asctime (Time.localtime ())

>>> time.ctime ()'Mon Apr  6 19:24:41'>>>  Time.ctime (Time.time ())'Mon Apr  6 19:24:47'>>> time.ctime ( Time.localtime ()) Traceback (most recent  ):"<stdin>"  in <module> is required

time.strftime ()   sets a tuple or struct_ that represents time Time, such as returned by Time.localtime () and Time.gmtime (), is converted to a formatted date string. If T is not specified, the Time.localtime () is passed in. If any of the elements in the tuple are out of bounds, the ValueError error will be thrown.

>>> time.strftime ('%y-%m-%d%x', Time.localtime ())'  2015-04-06 19:36:34'>>> time.strptime ('2015-04-06 19:36:34', ' %y-%m-%d%x ')     #time.strftime () inverse operation  time.struct_time (tm_year=2015, tm_mon=4, Tm_mday=6, TM _hour=19, tm_min=36, tm_sec=34, tm_wday=0, tm_yday=96, Tm_isdst=-1)

Note:

    1. "%p" is only effective when used in conjunction with "%I".
    2. The emphasis in the document is really 0-61, not 59, leap year seconds is two seconds (sweat one).
    3. When using the Strptime () function,%u and%w are only evaluated when the number of weeks and days in the year is determined.
format meaning Notes
%a Local (locale) simplified week name
%A Local Full week name
%b Local Simplified month name
%B Local Full month name
%c Local corresponding date and time representation
%d Day of the one months (01-31)
%H Hours of the day (24-hour, 00-23)
%I Number of hours (12-hour, 01-12)
%j Day of the Year (001-366)
%m Month (01-12)
%M Number of minutes (00-59)
%p Local AM or PM's corresponding character One
%s Seconds (01-61) Two
%u

The number of weeks in a year. (00-53 weeks is the beginning of one weeks.) )

All days before the first Sunday are placed in the No. 0 week.

Three
%w The day of the one week (0-6,0 is Sunday) Three
%W and%u basically the same, the difference is%w with Monday for one weeks start.
%x Local corresponding date
%x Local corresponding time
%y The year of the century was removed (00-99)
%Y The full year
%Z The name of the time zone (if it does not exist as a null character)
%% '% ' character

Python Standard library time

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.