The time module in Python

Source: Internet
Author: User
Tags local time string format

From the help documentation for the time module, it is found that the relevant functions are as follows:

    time ()  -- return current time in seconds since  the epoch as a float    clock ()  -- return CPU  time since process start as a float    sleep ()  --  delay for a number of seconds given as a float     gmtime ()  -- convert seconds since Epoch to UTC tuple     localtime ()  -- convert seconds since Epoch to local  Time tuple    asctime ()  -- convert time tuple to string     ctime ()  -- convert time in seconds to string     mktime ()  -- convert local time tuple to seconds  Since epoch &nbSp;  strftime ()  -- convert time tuple to string according to  format specification    strptime ()  -- parse string to  Time tuple according to format specification    tzset ()  --  change the local timezone

Then look at the functions of the time module from the above functions.


1. Time ()

This function returns a timestamp. For January 1, 1970 0:0 0 seconds is the start of the timing, up to the current length of time (regardless of leap seconds). Identify with floating-point type

In []: Import Timein [1431868698.641031In]: Time.time () out[53]: [54]:

This return value looks particularly awkward. At least one cannot understand exactly what time it represents.


2, LocalTime ()

This function alleviates the awkward return value of the time function at least. It can convert any timestamp into a tuple, which is a time that people can understand. If there are no parameters in the default localtime, then it takes Time.time () as the timestamp.

In [All]: Time.localtime () out[56]: Time.struct_time (tm_year=2015, tm_mon=5, tm_mday=17, tm_hour=21, tm_min=23, tm_sec= Notoginseng, Tm_wday=6, tm_yday=137, tm_isdst=0) in []: Time.localtime (10000) out[57]: Time.struct_time (tm_year=1970, Tm_mon=1 , Tm_mday=1, tm_hour=10, tm_min=46, tm_sec=40, tm_wday=3, Tm_yday=1, tm_isdst=0)


In this tuple, each item has the following meanings:


| Tm_hour
| Hours, range [0, 23]
|
| Tm_isdst
| 1 if summer time is in effect, 0 if not, and-1 if unknown
|
| Tm_mday
| Day of month, range [1, 31]
|
| Tm_min
| minutes, range [0, 59]
|
| Tm_mon
| Month of year, range [1, 12]
|
| Tm_sec
| seconds, range [0, 61])
|
| Tm_wday
| Day of week, range [0, 6], Monday is 0
|
| Tm_yday
| Day of year, range [1, 366]
|
| Tm_year
| Year, for example, 1993


which

Tm_wday, Tm_yday, tm_isdst These three I personally feel that I do not remember, it is deliberately recorded.

Tm_wday: The first day of the week. The range is 0-6, Monday is the No. 0 day, Sunday is the sixth day

Tm_yday: The number of days of the year, the time range is 0-366

TM_ISDST: Represents daylight saving Time


3, Gmtime ()

LocalTime () is local time, and it's best to use GMT if you want to internationalize.

Gmtime behaves the same as localtime, and if it does not provide a time parameter, it acquires Time.time () to do the time parameter.

In [the]: Time.gmtime () out[59]: Time.struct_time (tm_year=2015, tm_mon=5, tm_mday=17, tm_hour=13, tm_min=32, tm_sec=29, Tm_wday=6, tm_yday=137, tm_isdst=0) in []: Time.gmtime (+) out[60]: Time.struct_time (tm_year=1970, Tm_mon=1, tm_ Mday=1, tm_hour=0, tm_min=16, tm_sec=40, tm_wday=3, Tm_yday=1, tm_isdst=0) in [61]:


4, CTime () and Asctime ()

In daily programming, there must be a string for the most commonly used data types. So how do you translate these time representations into string representations?

The two functions of CTime and asctime can help us do this kind of work.

First, compare the similarities and differences between CTime and Asctime.

Same place:

Both of these functions return the time in the form of a string

in [+]: Time.ctime () out[63]: ' Sun May 21:36:53 ' in [+]: Time.asctime () out[64]: ' Sun May 21:36:56 ' in [65]:

The difference:

The parameter that is accepted by the Cimte function is a time in seconds (s). If no parameters are provided by default, get Time.time ()

The Asctime function accepts a tuple representing the time. If no parameters are provided by default, the time Time.localtime () is obtained.

in [+]: Time.ctime () out[66]: ' Sun may 21:40:08 ' in []: Time.ctime (+) out[67]: ' Thu Jan 1 08:16:40 1970 ' in [68  ]: Time.asctime () out[68]: ' Sun may 21:40:34 ' in []: t = time.localtime (+) in []: Time.asctime (t) out[70]: ' Thu Jan 1 08:16:40 1970 '


5, Strftime () and Strptime ()
Time formats that are now returned in times (), localtime (), and so on, can be converted to time in string form. However, the format of this string cannot be adjusted. What if I wanted time to be represented in a different string format?

This is where you can use the Strftime function. Strftime The parameter of this function when the time of a tuple type. If the parameter is not given, then it gets time.localtime () as the argument.

In [Time.strftime]: ('%y-%m-%d ') out[86]: ' 2015-05-17 ' in []: T = time.localtime (+) in []: Time.strftime ('%y-%m-% d ', T) out[88]: ' 1970-01-01 '

Each person wants to represent a different format of time. In fact, the focus here should be how to get the formatting parameters of each item meaning.

I did not find these instructions for the help of Python itself, but I found the description of each item from the Linux system function strftime. Man strftime can see it. Or you can easily search the Internet.


Strptime This function, do exactly when the strftime of the reverse work:

In [4]: t = time.strftime ('%y-%m-%d ') in [5]: Time.strptime (t, '%y-%m-%d ') out[5]: Time.struct_time (tm_year=2015, tm_mon= 5, Tm_mday=17, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=6, tm_yday=137, Tm_isdst=-1)

This article is from the "Learning Notes" blog, so be sure to keep this source http://unixman.blog.51cto.com/10163040/1652145

The time module in Python

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.