Time of the Python module

Source: Internet
Author: User
Tags month name

The time module in Python.

1. In Python, there are usually several ways to represent time: 1) timestamp 2) formatted time string 3) tuple (struct_time) a total of nine elements.

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

3, timestamp (timestamp) way: Typically, a timestamp represents an offset that is calculated in seconds from 00:00:00 January 1, 1970. We run "type (Time.time ())" and return the float type. The function that returns the Timestamp method mainly has time (), clock () and so on.

4, tuple (struct_time) mode: Struct_time tuple has 9 elements, the function that returns Struct_time mainly has 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

(1) Time. localtime ([secs]): Converts a timestamp to the struct_time of the current time zone. The secs parameter is not provided, whichever is the current time.

>>> Time.localtime ()
Time.struct_time (tm_year=2011, tm_mon=5, tm_mday=5, tm_hour=14, tm_min=14, tm_sec=50, tm_wday=3, tm_yday=125, TM_ISDST =0)
>>> Time.localtime (1304575584.1361799)
Time.struct_time (tm_year=2011, tm_mon=5, tm_mday=5, tm_hour=14, tm_min=6, tm_sec=24, tm_wday=3, tm_yday=125, tm_isdst= 0)

(2)time.gmtime ([secs]): Similar to the LocalTime () method, the Gmtime () method is the struct_time of converting a timestamp to the UTC time zone (0 o'clock Zone).

>>>time.gmtime ()
Time.struct_time (tm_year=2011, tm_mon=5, tm_mday=5, tm_hour=6, tm_min=19, tm_sec=48, tm_wday=3, tm_yday=125, tm_isdst= 0)

(3)time.time (): Returns the timestamp of the current time.

>>> Time.time ()

1304575584.1361799

(4)time.mktime (t): Converts a struct_time to a timestamp.

>>> Time.mktime (Time.localtime ())

1304576839.0

(5)time.sleep (secs): The thread defers the specified time run. Unit is seconds.

(6)Time.clock (): This needs to be noted, different meanings on different systems. On a UNIX system, 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 time

if __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 ())

Operation Result:

clock1:3.35238137808e-006

clock2:1.00004944763

clock3:2.00012040636

Where the first clock () output is the program run time

The second to third clock () output is the time interval from the first clock

1, (7)time.asctime ([t]): A tuple or struct_time representing time is expressed in this form: ' Sun June 20 23:21:05 1993 '. If there are no parameters, Time.localtime () will be passed in as a parameter.

>>> Time.asctime ()

' Thu May 5 14:55:43 2011 '

(8)time.ctime ([secs]): Converts a timestamp (floating point number in seconds) to 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 (secs)).

>>> Time.ctime ()

' Thu May 5 14:58:09 2011 '

>>> Time.ctime (Time.time ())

' Thu May 5 14:58:39 2011 '

>>> Time.ctime (1304579615)

' Thu May 5 15:13:35 2011 '

(9)time.strftime (format[, T]): Converts a tuple or struct_time that represents time, such as returned by Time.localtime () and Time.gmtime (), into a formatted time 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.

Format

Meaning

Note

%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

Note:

"%p" is only effective when used in conjunction with "%I".

The emphasis in the document is really 0-61, not 59, leap year seconds is two seconds (sweat one).

When using the Strptime () function,%u and%w are only evaluated when the number of weeks and days in the year is determined.

As an example:

>>> time.strftime ("%y-%m-%d%x", Time.localtime ())

' 2011-05-05 16:37:06 '

(time.strptime)(string[, format]): Converts a formatted time string to Struct_time. In fact it is inverse operation with strftime ().

>>> time.strptime (' 2011-05-05 16:37:06 ', '%y-%m-%d%x ')

Time.struct_time (tm_year=2011, tm_mon=5, tm_mday=5, tm_hour=16, tm_min=37, tm_sec=6, tm_wday=3, tm_yday=125, tm_isdst= -1)

In this function, format defaults to: "%a%b%d%h:%m:%s%Y".

Finally, let's take a summary of the time module. According to the previous description, there are three types of expressions in Python: 1) timestamp 2) tuple or Struct_time 3) formatted string.

The transformation between them:

Time of the Python module

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.