Time module (time and datetime)

A, time

Time is divided into three formats:

1, timestamp: January 1, 1970 00:00:00 offset press in seconds

  Effect: the time interval used to calculate

2, according to a certain format of time: 2020-03-30 11:11:11

  Role: for show time

3, the structure of the time (struct_time)
  role: to get a part time alone (year, month, day, hour, minute, second, the first few weeks of the year, day of the year, daylight saving time)

. 1  Import Time
 2  
. 3  Print (the time.time ())   # stamp 1,585,551,358.6356573 
. 4  
. 5  Print (The time.strftime ( ' % Y-M-%%% X-D ' ))   # formatting time, 2020-03-3014 : 58: 07 
. 6  Print (The time.strftime ( ' % Y-M-% D%%% M-H-P% S% ' )) # formatting time, 2020-03-30 14-58-07 PM, % p aM PM display 
. 7  
. 8 RES = time.localtime ()   # structured time 
. 9  Print (RES)
 10  Print (res.tm_year)   # acquiring a portion of the time

 二、datetime

. 1  Import datetime
 2  
. 3  Print (datetime.datetime.now ())   # 2020-03-30 15: 14: 25.801331 
. 4  
. 5  add or subtract to the top on the time
 . 6  Print (datetime.datetime.now () + datetime. timedelta (days =. 7))   # 2020-04-06 15: 14: 25.801331,7 time of day, or day = -7 subtraction of the count time is 7 days ago 
. 7  Print (datetime.datetime.now () + the datetime.timedelta (= weeks. 1))   # 2020-04-06 15: 14: 25.801331 
. 8  Print (datetime.datetime.now () + the datetime.timedelta (= 10 hours))   # 2020-03-31 01:14: 25.801331

 Third, the time required to master the operation of the module

1, the time format conversion

struct_time-> timestamp

1 import time
2 
3 res=time.localtime()  # time.struct_time(tm_year=2020, tm_mon=3, tm_mday=30, tm_hour=15, tm_min=20, tm_sec=29, tm_wday=0, tm_yday=90, tm_isdst=0)
4 print(time.mktime(res))  # 1585552829.0

Timestamp -> struct_time

1 import time
2 
3 res=time.time()  # 1585552958.499538
4 print(time.localtime(res))  # time.struct_time(tm_year=2020, tm_mon=3, tm_mday=30, tm_hour=15, tm_min=22, tm_sec=38, tm_wday=0, tm_yday=90, tm_isdst=0)

Added: Coordinated Universal Time and local time

. 1  Import Time
 2  
. 3  # difference 8 hours 
. 4  Print (time.localtime ())
 . 5  Print (time.gmtime ()) # Universal Time to learn 
. 6  Print (time.localtime (333333333 ))
 . 7  Print (time.gmtime ( 333333333))

struct_time-> formatted string time

import time

s_time=time.localtime()
print(time.strftime('%Y-%m-%d %H:%M:%S',s_time))  # 2020-03-30 15:27:44

A string of formatted time -> struct_time

import time

print(time.strptime('1988-03-03 11:11:11','%Y-%m-%d %H:%M:%S'))  # time.struct_time(tm_year=1988, tm_mon=3, tm_mday=3, tm_hour=11, tm_min=11, tm_sec=11, tm_wday=3, tm_yday=63, tm_isdst=-1)

Really need to know: format string <------> timestamp

--- String format> struct_time ---> timestamp (timestamp)

import time

struct_time=time.strptime('1988-03-03 11:11:11','%Y-%m-%d %H:%M:%S')
timestamp=time.mktime(struct_time)

format string<---struct_time<---timestamp

res=time.strftime('%Y-%m-%d %X',time.localtime(timestamp))

Knowledge of

. 1  Import Time
 2  
. 3  Print (that time.asctime ())   # Mon-Mar 30 15:40:08 2020 
. 4  
. 5  Import datetime
 . 6  
. 7  Print (datetime.datetime.utcnow ())   # International time 2020-03-30 07:40 : 08.035936 
. 8  
. 9  Print (datetime.datetime.fromtimestamp (333333))   # timestamp formatting rotation time

 

  

Guess you like

Origin www.cnblogs.com/BoyGc/p/12599029.html