A detailed description of the use of time modules in Python programming

Source: Internet
Author: User
Tags julian day local time month name
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.

Daylight Saving Time Introduction: http://baike.baidu.com/view/100246.htm

UTC Description: Http://wenda.tianya.cn/wenda/thread?tid=283921a9da7c5aef&clk=wttpcts

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 was not present, the time tuple was returned by LocalTime () was 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

Note:
Using Time.time () on Xinux and using Time.clock () in Windows can give you higher precision.
Xinux and win are different in implementing the system clock. Time.clock () is the system clock implementation of the call, and the two platforms are different.
The main problem is the Xinux clock switching strategy: Jiffy implementation, because the kernel clock switchover is not continuous, but the interval period (generally between the 1ms~10ms) before the change, so if it is in the xinux of two time-consuming calls, through the Time.clock () The result is the same.

3.sleep (...)

Sleep (seconds)
Thread delays the specified time run, tested, in seconds
Example:

Import time if __name__ = = ' __main__ ':   time.sleep (3)   print "Clock1:%s"% Time.clock ()   # print "local time:%s "% time.localtime ()   print str (time.strftime ('%y-%m-%d%h:%m:%s ', Time.localtime ()))   Time.sleep (3)   Print "Clock2:%s"% Time.clock ()   # print "local time:%s"% time.localtime ()   print str (time.strftime ('%y-%m-%d%H :%m:%s ', Time.localtime ()))   Time.sleep (3)   print "Clock3:%s"% Time.clock ()   # print "local time:%s"% time. LocalTime ()   print str (time.strftime ('%y-%m-%d%h:%m:%s ', Time.localtime ()))

Results

clock1:0.0206782015-08-09 00:18:31clock2:0.0208912015-08-09 00:18:34clock3:0.0210682015-08-09 00:18:37

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
Print str (time.strftime ('%y-%m-%d%h:%m:%s ', Time.localtime ()))

2015-08-09 00:18:37

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 (the number of floating-point seconds elapsed after the 1970 era)

Iii. Common Commands

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

Print Time.time () print time.localtime () print time.ctime ()

The result is:

1439051479.08time.struct_time (tm_year=2015, tm_mon=8, tm_mday=9, tm_hour=0, tm_min=31, tm_sec=19, tm_wday=6, tm_yday= 221, tm_isdst=0) Sun 9 00:31:19 2015

2.python formatted string

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

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

Formatted as Sat 28 22:24:24 2009 Form

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

3. Converting a format string to a timestamp

A = "Sat Mar 28 22:24:24 2009″

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

Ps:
The main thing to know about this piece is to use time to calculate the running times of the key in my program, so it's more organized. As for the conversion of time and so on, after the subsequent need to organize.

Iv. precision test for calculating code execution efficiency using time modules

#python中使用time模块计算代码执行效率 #测试用time. Time () and time.clock () Use Precision  Import sys import time import timeit Default_timer = None  if Sys.platform = = "Win32": # on Windows, the best timer was Time.clock ()   Default_timer = Time.clock else: # on MO St Other platforms The best timer is time.time ()   Default_timer = time.time print Default_timer timein= time.clock () fo R i in range:   n=i timeuse = Time.clock ()-timein print timeuse  Timein = Time.time () for I in range:   n =i timeuse = time.time ()-timein print timeuse  Timein = Timeit.default_timer () for I in range:   n=i timeuse = t Imeit.default_timer ()-timein print timeuse     #该段代码在windows下结果如下 >>>  4.07873067161e-005 0.0 3.5758734839e-005  
  • 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.