Demand : get the time of day before the time specified format, if today is the beginning, the beginning of automatic conversion, for example:
Input time: 2019-06-27 23:59:59
output time is: 2019-06-2623 : 59: 59
Before using a variety of datetime need to determine the beginning, the beginning of other conditions, then think of a time stamp to time direct subtract 86400, and then converted to the specified format to
Man of few words said, directly on the code:
def get_yesterdayTime(provided_time): t_providedTime=time.strptime(provided_time,'%Y-%m-%d%H:%M:%S') timeStamp=int(time.mktime(t_providedTime)) yesterday_stamp=timeStamp-86400 yesterday=time.strftime('%Y-%m-%d%H:%M:%S',time.gmtime(yesterday_stamp)) return yesterday
The result of my tears ran, actually 2019-06-26 15:59:59
I wonder why would not the same as the expected time and will, so I put the current time into a timestamp conversion and then converted back into formatted time
DEF get_yesterdayTime (): # get yesterday's timestamp yesterday_stamp = int (time.mktime (time.localtime ())) - 86400 # Gets yesterday timetuple yesterday_t = time.gmtime (yesterday_stamp) # get formatted time yesterday yesterday = time .strftime ( ' % Y-M-% D%% H:% M:% S ' , yesterday_t) return Yesterday
Get out the time difference than the current time of 8 hours, Ever since I started to check every line of code, eventually found time.gmtime there is a problem:
gmtime returns a UTC time tuple, and we are the East eight districts, still have nothing more than look at the source . . .
Amended as follows:
DEF get_yesterdayTime (): # Get the last timestamp yesterday_stamp = int (time.mktime (time.localtime ())) - 86400 + 28800 # acquired yesterday timetuple yesterday_t = time.gmtime (yesterday_stamp) # Get the last time formatted yesterday The time.strftime = ( ' % Y-M-% D%% H:% M:% S ' , yesterday_t) return Yesterday