Python-- time format GMT

1. Background

Recently doing video upload function to get the size, time, video is the presence of Jinshan Yun, due to the Jinshan sdk interface time to return after the results of the use case execution time is http head, time format 'Tue, 08 May 2018 06: 17:00 GMT ', now want to convert it into a' 2018-05-08 06:17:00 'this

 

2. Methods

2.1  How to convert into a type datetime GMT time HTTP header format string used to look at the next

import datetime
GMT_FORMAT =  '%a, %d %b %Y %H:%M:%S GMT'
datetime.datetime.utcnow().strftime(GMT_FORMAT)

Get local GMT time   ' Thu, 15-Aug 2019 03:02:38 GMT '

 

2.2 look at how to convert the GMT time format into a string type datetime:

TIME = '2018-05-08 06:17:00'
datetime.datetime.strptime(TIME,GMT_FORMAT)

 

2.3 General and China local time GMT time there is a time difference, if you want to convert local time, need to subtract some time

 

datetime.datetime.strptime (the TIME, GMT_FORMAT) + datetime.timedelta (hours = 8 )  
 # This converted our time, Beijing East eight districts, to add 8 hours

 

 

2.4 write a generic method

def formatGMTime(timestamp):
    GMT_FORMAT = '%a, %d %b %Y %H:%M:%S GMT'
    a = datetime.datetime.strptime(timestamp, GMT_FORMAT) + datetime.timedelta(hours=8)
    return a
In fact, use strftime and strptime two interchangeable functions only, if interested in both functions, see the blog strptime python learning methods and strftime
To learn more about datetime.timedelta () method using a class, see the blog timedelta python learning

Guess you like

Origin www.cnblogs.com/yuanfang0903/p/11357100.html