time module of python module

Posted by pages on Sat, 25 May 2019 23:18:22 +0200

time

  • Three Forms of Time

    • Time stamp: Typically, a time stamp represents an offset calculated in seconds starting at 00:00 on January 1, 1970. We run "type" (time. time ()) and return the float type.
    • Format String:'1999-12-06'
      • '''
        % y-Binary Year Representation (00-99)
        % Y4-digit Annual Representation (000-9999)
        % m month (01-12)
        % One d ay in a month (0-31)
        % H 24-hour system (0-23)
        % I 12-hour hours (01-12)
        % M minutes (00 = 59)
        % S seconds (00-59)
        % a Local simplified week name
        % A Full Local Weekly Name
        % b Locally simplified month names
        % B Local full month name
        % c Local corresponding date and time representations
        % One day in j year (001-366)
        % p Local A.M. or P.M. Equivalents
        % U Weeks of the Year (00-53) Sunday is the beginning of the week
        % w week (0-6), Sunday is the beginning of the week
        % W eeks of the year (00-53) Monday is the beginning of the week.
        % x Local corresponding date representation
        % X Local Corresponding Time Representation
        % Z Name of the current time zone
        %%% Number itself
        
        '''
        Time Formatting Symbol
    • Tuple (struct_time): The struct_time tuple has nine elements: year, month, day, hour, minute, second, week in a year, day in a year, etc.
      • """
        
        Index Attribute Values
        0 tm_year (year), such as 2011
        1 tm_mon (month) 1 - 12
        2 tm_mday (day) 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 means Sunday)
        7 tm_yday (days of the year) 1 - 366
        8 tm_isdst (is it daylight saving time) defaults to -1?
        
        """
        Tuple element meaning

     

  • Several Formats of Time Representation in python

    • #Import time module
      >>>import time
      
      #time stamp
      >>>time.time()
      1500875844.800804
      
      #Time String
      >>>time.strftime("%Y-%m-%d %X")
      '2017-07-24 13:54:37'
      >>>time.strftime("%Y-%m-%d %H-%M-%S")
      '2017-07-24 13-55-04'
      
      #time tuples:localtime Converting a timestamp to the current time zone struct_time
      time.gmtime() time.localtime() time.struct_time(tm_year=2017, tm_mon=7, tm_mday=24,           tm_hour=13, tm_min=59, tm_sec=37, tm_wday=0, tm_yday=205, tm_isdst=0) # Summary: Time stamps are the time that computers can recognize; time strings are the time that people can understand; tuples are used to manipulate time.
  • Conversion between various formatting times

#time stamp-->Structured time
#time.gmtime(time stamp)    #UTC Time, in line with local time in London, UK
#time.localtime(time stamp) #Local time. For example, we are now implementing this method in Beijing: 8 hours difference from UTC time, UTC time + 8 hours = Beijing time. 
>>>time.gmtime(1500000000)
time.struct_time(tm_year=2017, tm_mon=7, tm_mday=14, tm_hour=2, tm_min=40, tm_sec=0, tm_wday=4, tm_yday=195, tm_isdst=0)
>>>time.localtime(1500000000)
time.struct_time(tm_year=2017, tm_mon=7, tm_mday=14, tm_hour=10, tm_min=40, tm_sec=0, tm_wday=4, tm_yday=195, tm_isdst=0)

#Structured time-->time stamp 
#time.mktime(Structured time)
>>>time_tuple = time.localtime(1500000000)
>>>time.mktime(time_tuple)
1500000000.0
#Structured time-->String time
#time.strftime("Format definition","Structured time")  If the structured time parameter is not transmitted, then the real current time
>>>time.strftime("%Y-%m-%d %X")
'2017-07-24 14:55:36'
>>>time.strftime("%Y-%m-%d",time.localtime(1500000000))
'2017-07-14'

#String time-->Structured time
#time.strptime(Time String,String Correspondence Format)
>>>time.strptime("2017-03-16","%Y-%m-%d")
time.struct_time(tm_year=2017, tm_mon=3, tm_mday=16, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=75, tm_isdst=-1)
>>>time.strptime("07/24/2017","%m/%d/%Y")
time.struct_time(tm_year=2017, tm_mon=7, tm_mday=24, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=0, tm_yday=205, tm_isdst=-1)

 

#Structured time --> %a %b %d %H:%M:%S %Y strand
#time.asctime(Structured time) If no parameters are passed, the formatted string of the current time is returned directly
>>>time.asctime(time.localtime(1500000000))
'Fri Jul 14 10:40:00 2017'
>>>time.asctime()
'Mon Jul 24 15:18:33 2017'

#%a %d %d %H:%M:%S %Y strand --> Structured time
#time.ctime(time stamp)  If no parameters are passed, the formatted string of the current time is returned directly
>>>time.ctime()
'Mon Jul 24 15:19:07 2017'
>>>time.ctime(1500000000)
'Fri Jul 14 10:40:00 2017' 
import time
true_time=time.mktime(time.strptime('2017-09-11 08:30:00','%Y-%m-%d %H:%M:%S'))
time_now=time.mktime(time.strptime('2017-09-12 11:00:00','%Y-%m-%d %H:%M:%S'))
dif_time=time_now-true_time
struct_time=time.gmtime(dif_time)
print('In the past%d year%d month%d day%d hour%d Minute%d second'%(struct_time.tm_year-1970,struct_time.tm_mon-1,
                                       struct_time.tm_mday-1,struct_time.tm_hour,
                                       struct_time.tm_min,struct_time.tm_sec))
Calculating time difference

Topics: Python Attribute