[Python] Detailed explanation of time module

http://blog.csdn.net/kiki113/article/details/4033017

 Python's embedded time template translation and description
  
1. Introduction

  The time module provides function
  descriptions for various operation times : Generally, there are two ways to express time: the
       first is the time stamp (the offset in seconds relative to 1970.1.1 00:00:00), the time stamp It is the only
       second type that is represented in the form of an array, namely (struct_time), which has nine elements, respectively. The struct_time of the same timestamp will be different due to different time zones.
    Year (four digits, eg 1998)
    month (1-12)
    day (1-31)
    hours (0-23)
    minutes (0-59)
    seconds (0-59)
    weekday (0-6, Monday is 0)
    Julian day (day in the year, 1-366)
    DST (Daylight Savings Time) flag (-1, 0 or 1)
    If the DST flag is 0, the time is given in the regular time zone;
    if it is 1, the time is 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 introduction: http://wenda.tianya.cn/wenda/thread?tid=283921a9da7c5aef&clk=wttpcts
    
2. Function introduction
1.asctime()
  asctime ([tuple]) -> string
  to a struct_time (default is the current time), is converted into a string
        . tuple to the convert a string time a, EG 'Sat On Jun 06 16:26:11 1998'
        the when tuple the iS Not time present, current time as returned by localtime()
        is used.
        
2.clock()
  clock() -> floating point number
  This function has two functions.
  When it is called for the first time, it returns the actual time the program is running;
  With the call after the second call, what is returned is
  
  an example of the time interval from the first call to this call :
 

[python]  view plain  copy
  1. import time  
  2. if __name__ == '__main__':  
  3.     time.sleep(1)  
  4.     print "clock1:%s" % time.clock()  
  5.     time.sleep(1)  
  6.     print "clock2:%s" % time.clock()  
  7.     time.sleep(1)  
  8.     print "clock3:%s" % time.clock()  

  Output:
  clock1:3.35238137808e-006
  clock2:1.00004944763
  clock3:2.00012040636
  where the first clock outputs the program running time, the
  second and the third clock outputs the time interval from the first clock
  
3.sleep(... ) The
  sleep(seconds)
  thread postpones running for the specified time. After testing, the unit is seconds. However, there is a sentence like the following in the help document. This is not to understand
  "The argument may be a floating point number for subsecond precision."

4.ctime(...)
  ctime(seconds) -> string
  converts a timestamp (default is the current time) into a time string
  For example:
  time.ctime()
  output is:'Sat Mar 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)
  Convert a timestamp to a UTC time zone (0 time zone) struct_time, if the seconds parameter is not entered, the current time will be used 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)
  convert a timestamp into a struct_time of the current time zone, if the seconds parameter is not entered, the current time will be used as 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 (default is the current time) according to the specified format character String output
  of time and date formatting symbols in python:
  %y two-digit year representation (00-99)
  %Y four-digit year representation (000-9999)
  %m month (01-12)
  %d in the month One day (0-31)
  %H Hours in 24-hour system (0-23)
  %I Hours in 12-hour system (01-12) 
  %M Minutes (00=59)
  %S seconds (00-59)
  
  %a Local 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
  %j Day of the year ( 001-366)
  %p The equivalent of local AM or PM
  %U The number of weeks in the year (00-53) Sunday is the start of the week
  %w Week (0-6), and Sunday is the start of the week
  %W One year The week number in (00-53) Monday is the beginning of the week
  %x The local corresponding date means
  %X The local corresponding time means
  %Z The name of the current time zone %%%
  number itself 
    
9.strptime(...)
  strptime( string, format) -> struct_time
  converts the time string into an array form of time according to the specified formatter.
  For example:
  2009-03-20 11:45:39 The corresponding format string is: %Y-%m-% d %H:%M:%S
  Sat Mar 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 current time timestamp

three doubt
1. daylight saving time
  in the struct_time, if daylight saving time is not used, 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 time, respectively. Converting between them to timestamp should be related to 3600, but conversion The output afterwards are all 646585714.0
  
Four, small application
1.python get the current time
   time.time() get the current timestamp
   time.localtime() the struct_time form of the
   current time time.ctime() the string form of the current time
   
2.python format The string is  
  formatted into 2009-03-20 11:45:39 format
  time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) 
  
  formatted into Sat Mar 28 22 :24:24 2009 form
  time.strftime("%a %b %d %H:%M:%S %Y", time.localtime()) 
  
3. Convert the 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"))

Guess you like

Origin blog.csdn.net/michellechouu/article/details/51065735