Python Time Module Usage example detailed

Source: Internet
Author: User
Tags julian day month name
This article details the use of Python's inline time module. Share it for everyone's reference. The specific analysis is as follows:

First, Introduction

Time module provides functions for various operating times

Description: There are generally two ways to represent time:

The first is the way the timestamp is (relative to the offset from 1970.1.1 00:00:00 in seconds), the timestamp is unique

The second is represented as an array of (Struct_time), a total of nine elements, respectively, that the same timestamp of the struct_time will vary depending on the time zone

Year (four digits, e.g. 1998)
Month (1-12)
Day (1-31)
Hours (0-23)
Minutes (0-59)
Seconds (0-59)
Weekday (0-6, Monday is 0)
Julian Day, 1-366
DST (daylight Savings time) flag ( -1, 0 or 1) is daylight saving time
If the DST flag is 0, the time was given in the regular time zone;
If it is 1, the time was given in the DST time zone;
If it is-1, mktime () should guess based on the date and time.

Second, the function introduction

1.asctime ()

Asctime ([tuple]), string
Converts a struct_time (the default is the current time) to a string
Convert a time tuple to a string, e.g. ' Sat June 06 16:26:11 1998 '.
When the time of a tuple is not present, current time as returned by LocalTime ()
is used.

2.clock ()

Clock (), floating point number
The function has two functions,
At the time of the first call, the actual time of the program running is returned;
After the second call, the time interval from the first call to the call is returned

Example:

Import time if __name__ = = ' __main__ ':   time.sleep (1)   print "clock1:%s"% Time.clock ()   time.sleep (1)   Print "clock2:%s"% Time.clock ()   time.sleep (1)   

Output:

clock1:3.35238137808e-006clock2:1.00004944763clock3: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.

3.sleep (...)

Sleep (seconds)
The thread defers the specified time to run, tested, in seconds, but in the help document there is the following sentence, which is not understood
"The argument is a floating point number for subsecond precision."

4.ctime (...)

CTime (seconds), string
Converts a timestamp (default to the current time) to a time string
For example:
Time.ctime ()
The output is: ' Sat 28 22:24:24 2009 '

5.gmtime (...)

Gmtime ([seconds]) (Tm_year, Tm_mon, Tm_day, Tm_hour, Tm_min,tm_sec, Tm_wday, Tm_yday, TM_ISDST)

Converts a timestamp to the struct_time of a UTC time zone (0 o'clock Zone), and if the seconds parameter is not entered, the current time is the conversion criterion

6.localtime (...)

LocalTime ([seconds]), (TM_YEAR,TM_MON,TM_DAY,TM_HOUR,TM_MIN,TM_SEC,TM_WDAY,TM_YDAY,TM_ISDST)

Converts a timestamp to a struct_time of the current time zone, and if the seconds parameter is not entered, the conversion criterion is the current

7.mktime (...)

Mktime (tuple), floating point number
Converts a struct_time to a timestamp

8.strftime (...)

Strftime (format[, tuple]), string
Outputs the specified Struct_time (default current time), based on the specified formatted string
Python format symbols in time Date:
%y Two-digit year representation (00-99)
%Y Four-digit year representation (000-9999)
%m Month (01-12)
One day in%d months (0-31)
%H 24-hour hours (0-23)
%I 12-hour hours (01-12)
%M minutes (00=59)
%s seconds (00-59)

%a Local Simplified Week name
%A Local Full week name
%b a locally simplified month name
%B Local Full month name
%c Local corresponding date representation and time representation
%j Day of the Year (001-366)
%p the equivalent of a local a.m. or p.m.
%u weeks of the year (00-53) Sunday is the beginning of the week
%w Week (0-6), Sunday for the beginning of the week
%W Week of the Year (00-53) Monday is the beginning of the week
%x Local corresponding date representation
%x Local corresponding time representation
%Z the name of the current time zone
Percent% of the number itself

9.strptime (...)

Strptime (string, format), Struct_time

Converts a time string to an array of time based on a specified format character
For example:
2009-03-20 11:45:39 The corresponding formatted string is:%y-%m-%d%h:%m:%s
Sat 28 22:24:24 2009 The corresponding formatted string is:%a%b%d%h:%m:%s%Y

10.time (...)

Floating point number, time ()
Returns the timestamp of the current time

Third, doubtful points

1. Daylight Saving Time

In Struct_time, daylight saving time seems to be useless, for example
A = (2009, 6, 28, 23, 8, 34, 5, 87, 1)
B = (2009, 6, 28, 23, 8, 34, 5, 87, 0)
A and B represent daylight saving time and standard times, which convert to timestamps should be related to 3600, but the output will be 646585714.0 after conversion.

Four, small application

1.python Get current time

Time.time () gets the current timestamp
Time.localtime () struct_time form of the current time
Time.ctime () The string form of the current time

2.python formatted string

Formatted as 2009-03-20 11:45:39 in the form of

Formatted as Sat 28 22:24:24 2009 Form


3. Converting a format string to a timestamp

A = "Sat Mar 22:24:24" b = Time.mktime (Time.strptime (A, "%a%b%d%h:%m:%s%Y"))

It is believed that this article has some reference value to the Python program design of everyone.

  • 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.