Python Time Module usage examples detailed _python

Source: Internet
Author: User
Tags julian day month name time interval in python

This article details the use of Python's inline time module. Share for everyone to use for 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 timestamp (relative to the offset in seconds of 1970.1.1 00:00:00), which is the only time stamp

The second type, represented as an array (struct_time), has nine elements, each of which means that the struct_time of the same timestamp differs 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 "given in" DST time zone;
If it is-1, mktime () should guess based on the date and time.

Second, Function introduction

1.asctime ()

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

2.clock ()

Clock ()-> floating point number
This function has two functions,
At the first call, it returns the actual time the program is running;
After the second call, returns the time interval from the first call to this call

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) 
  print "clock3:%s"% Time.clock () 

Output:

clock1:3.35238137808e-006
clock2:1.00004944763
clock3:2.00012040636

The first clock output is the program run time
The second to third clock output is the time interval with the first clock

3.sleep (...)

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

4.ctime (...)

CTime (seconds)-> string
Converts a timestamp (the default current time) into a time string
For example:
Time.ctime ()
Output as: ' 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) and, if the seconds parameter is not entered, the current time as the conversion standard

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 the struct_time of a current time zone, and if the seconds parameter is not entered, the current period is the conversion standard

7.mktime (...)

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

8.strftime (...)

Strftime (format[, tuple])-> string
Converts the specified struct_time (the default current time) to the specified formatted string output
Time date format symbol in Python:
%y Two-digit year representation (00-99)
%Y Four-digit year representation (000-9999)
%m Month (01-12)
Day of%d months (0-31)
%H 24-hour hours (0-23)
%I 12 Hours of 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 number of weeks in a year (00-53) Sunday is the beginning of the week
%w Week (0-6), Sunday for the beginning of the week
%w number of weeks in a 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
%%% per se

9.strptime (...)

Strptime (string, format)-> Struct_time

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

10.time (...)

Time ()-> floating point number
Returns the time stamp for the current time

Third, doubt

1. Daylight Saving Time

Daylight saving time does not seem to be used in struct_time, 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 are converted to timestamps that should be related to 3600, but the output is 646585714.0 after conversion.

Four, small application

1.python Get current time

Time.time () Gets the current time stamp
Time.localtime () The Struct_time form of the current time
Time.ctime () The string form of the current time

2.python format string

Format into 2009-03-20 11:45:39 form

Time.strftime ("%y-%m-%d%h:%m:%s", Time.localtime ()) 

Format into Sat 28 22:24:24 2009 Form

Time.strftime ("%a%b%d%h:%m:%s%Y", Time.localtime ()) 


3. Convert a format string to a timestamp

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

It is believed that this article has certain reference value to everyone's Python programming.

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.