# -*- coding: utf-8 -*-
import time
import datetime
class
timeshift
:def
__init__
(self)
:pass
defget_utctime
(self)
-> datetime.datetime:
utc_time = datetime.datetime.utcfromtimestamp(time.time())
return utc_time
defutctime_format
(self)
->
str:
''' 作用:datetime.datetime格式化成字串,可用於檔案命名
:return: str
'''utc_fmt = self.get_utctime(
).strftime(
"%y%m%d%h%m%s"
)return utc_fmt
defget_localtime
(self)
: local_time = datetime.datetime.now(
)return local_time
deflocaltime_format
(self)
: local_fmt = self.get_localtime(
).strftime(
"%y%m%d%h%m%s"
)return local_fmt
defutc2local
(self, utc_st)
-> datetime.datetime:
''' 作用:將utc時間轉換成本地時間
:return: 返回本地時間
'''now_stamp = time.time(
) local_time = datetime.datetime.fromtimestamp(now_stamp)
utc_time = datetime.datetime.utcfromtimestamp(now_stamp)
offset = local_time - utc_time
local_st = utc_st + offset
return local_st
deflocal2utc
(self, local_st)
-> datetime.datetime:
''' 作用:將本地時間轉成utc時間
:return: 返回utc時間
'''time_struct = time.mktime(local_st.timetuple())
utc_st = datetime.datetime.utcfromtimestamp(time_struct)
return utc_st
defprint_sep
(self, num=50)
: line_sep =
"-"* num
print
(line_sep)
defmain
(self)
: utc2local = self.utc2local(self.get_utctime())
local2utc = self.local2utc(self.get_localtime())
print
("utc time: %s"
% self.get_utctime())
print
("local time: %s"
% self.get_localtime())
self.print_sep(40)
print
("utc2local time: %s"
% utc2local)
print
("local2utc time: %s"
% local2utc)
self.print_sep(40)
print
("utctime format time: %s"
% self.utctime_format())
print
("localtime format time: %s"
% self.localtime_format())
if __name__ ==
'__main__'
: timeshift = timeshift(
) timeshift.main(
)
返回結果:
utc time:
2020-04
-1903:
09:34.915714
local time:
2020-04
-1911:
09:34.915714--
----
----
----
----
----
----
----
----
----
--utc2local time:
2020-04
-1911:
09:34.915714
local2utc time:
2020-04
-1903:
09:34-
----
----
----
----
----
----
----
----
----
---utctime format time:
20200419030934
localtime format time:
20200419110934
utc時間 單位換算 UTC轉換本地時間
title utc轉換本地時間 author norains date friday 26 march 2010 environment windows xp windows ce 一般情況下,我們很少需要用到utc轉換為本地時間 因為如果僅僅是獲取本機的本地時間,我們完全可以不用如此麻煩,直接呼叫...
python 獲取utc時間轉化為本地時間
python 獲取utc時間轉化為本地時間 方法一 import datetime timenow datetime.datetime.utcnow datetime.timedelta hours 8 將utc時間轉化為本地時間 timetext timenow.strftime y m d 方法...
UTC時間 GMT時間 本地時間 Unix時間戳
utc universal time coordinated 協調世界時,又稱世界標準時間。gmt greenwich mean time 格林尼治平均時。utc與gmt utc相當於本初子午線 即經度0度 上的平均太陽時,過去曾用格林威治平均時 gmt 來表示這兩者幾乎是同一概念。它們都是指的格林...