程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python time conversion (timestamp, format time)

編輯:Python

Background story

In our daily work, we often encounter python Time format conversion problem , For example, time stamp is converted to formatted time 、 Convert formatted time to timestamp , Here's a summary .

Before concluding, we need to clarify a key point : The time zone

  • The time zone : The time zones we usually use are generally divided into UTC Time and UTC+8 ( Dongba ) Time , Dongba CT — China standard time Simply understood as Chinese time ,UTC WET — Western European time zone ,GMT - Greenwich mean time , It is simply understood as British time . Dongba time ratio UTC Time is almost eight hours .

  • World time zone map

Format time to timestamp

Access to the local ( Dongba ) Time stamp

import time
import pytz
import datetime
# return Dongba Time stamp 
def get_shanghai_timestamp(date_time):
time_zone = pytz.timezone('Asia/Shanghai')
timeArray = datetime.datetime.strptime(date_time, "%Y-%m-%d %H:%M:%S")
local_dt = timeArray.astimezone(time_zone)
print('>>>', int(time.mktime(local_dt.timetuple())))
get_timestamp('2018-07-13 16:00:00')

obtain UTC Time stamp

import time
import pytz
import datetime
# return utc Time stamp 
def get_utc_timestamp(utc_time_str, utc_format=r'%Y-%m-%d %H:%M:%S'):
local_tz = pytz.timezone('UTC') # Define the local time zone ( Dongba time ratio utc Fast time 8 Hours )
utc_dt = datetime.datetime.strptime(utc_time_str, utc_format) # Convert the format of world time into datetime.datetime Format 
local_dt = utc_dt.astimezone(local_tz) # I want to datetime Add the world time zone to the format , then astimezone Switch time zone : World time zone ==> Local time zone 
return int(time.mktime(local_dt.timetuple())) # Return local timestamp 
print(get_utc_timestamp('2018-07-13 16:00:00', utc_format='%Y-%m-%d %H:%M:%S'))

Time stamp is converted to formatted time

Return to East 8 format time

# Mode one :
import time
import pytz
import datetime
def get_local_format_time(timestamp):
local_time=time.localtime()
format_time=time.strftime("%Y-%m-%d %H:%M:%S", local_time)
return format_time
get_local_format_time(1529112900)
# Mode two :
def local_to_utc(local_ts, time_format=r'%Y-%m-%d %H:%M:%S'):
time_zone = pytz.timezone('Asia/Shanghai')
time_str = time.strftime(time_format, time.localtime(local_ts)) # First, convert the local timestamp into a time tuple , use strftime Format as a string 
dt = datetime.datetime.strptime(time_str, time_format) # Use string strptime Convert to datetime in datetime Format 
utc_dt = dt.astimezone(time_zone) # astimezone Switch to a utc The time zone 
return utc_dt.strftime(time_format) # return utc Format time 
get_local_format_time(1529112900)

return UTC Format time

def get_utc_format_time(local_ts, time_format=r'%Y-%m-%d %H:%M:%S'):
time_str = time.strftime(time_format, time.localtime(local_ts)) # First, convert the local timestamp into a time tuple , use strftime Format as a string 
dt = datetime.datetime.strptime(time_str, time_format) # Use string strptime Convert to datetime in datetime Format 
utc_dt = dt.astimezone(pytz.utc) # astimezone Switch to a utc The time zone 
return utc_dt.strftime(time_format) # return utc Format time 
get_utc_format_time(1529112900)

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved