一、arrow.now()的调用问题
如果将arrow.now()赋给current变量进行调用的话,current就是固定的,而不会随着当前时间的变化而变化,每次调用的时间就是初始化current的时间
datetime_format = "YYYY-MM-DD HH:mm:ss"
# 当前的时间
current = arrow.now()
print(f"current1={current.format(datetime_format)}")
print(f"current1的timestamp={current.timestamp()}")
print(f"之前的时间={arrow.now().format(datetime_format)}")
print(f"之前的时间的timestamp={arrow.now().timestamp()}")
print(f"current2={current.format(datetime_format)}")
print(f"current2的timestamp={current.timestamp()}")
print(f"之后的时间={arrow.now().format(datetime_format)}")
print(f"之后的时间的timestamp={arrow.now().timestamp()}")
print(f"current3={current.format(datetime_format)}")
print(f"current3的timestamp={current.timestamp()}")
二、关于arrow.get方法拿到的时间戳
例子1:
在创建arrow对象的时间要特别注意时区的问题:
- 用arrow.now()创建current的
timestamp
拿到的是北京时区时间17点54分
的时间戳 - arrow.get() 方法默认返回值是 UTC时间。
用北京时区的时间戳去用arrow.get()创建了一个current2对象。
北京时间比UTC快8小时,arrow.get() 方法它的实际北京时间确实是17点54分,但是它的UTC时间就是09:54
current = arrow.now()
ti = current.timestamp()
print(f"current时间={current.format(datetime_format)}")
print(f"current本地的timestamp={ti}")
# 用ti来创建arrow对象
current2 = arrow.get(ti)
ti2 = current2.timestamp()
print(f"current时间={current2.format(datetime_format)}")
print(f"current本地的timestamp={ti2}")
例子2:
如果我们将一个时间类型从arrow变成str,然后再通过arrow.get()转化位arrow。这两个得到的时间戳会不一样。因为时区问题
datetime_format = "YYYY-MM-DD HH:mm:ss"
print(f"之前的时间={arrow.now().format(datetime_format)}")
print(f"之前的时间的timestamp={arrow.now().timestamp()}")
print("-----------------------")
test_dt1 = StandardTimeConvert().strandard_date("")
print(f"test_dt1的类型{type(test_dt1)}")
print(f"test_dt1时间={test_dt1.format(datetime_format)}")
print(f"test_dt1时间的timestamp={test_dt1.timestamp()}")
test_dt2 = arrow.get(test_dt1.format(datetime_format), datetime_format)
print(f"test_dt2的类型{type(test_dt2)}")
print(f"test_dt2时间={test_dt2.format(datetime_format)}")
print(f"test_dt2时间的timestamp={test_dt2.timestamp()}")
print("-----------------------")
print(f"之后的时间={arrow.now().format(datetime_format)}")
print(f"之后的时间的timestamp={arrow.now().timestamp()}")
运行结果:
结果分析:
【之前的时间】和【之后的时间】还有【test_dt1】的时间戳都是1628241639.xxxx.。但是对于test_dt2,它的时间戳1628270439.0大于(【之前的时间】和【之后的时间】还有【test_dt1】)
他们的时间戳相差了大概8个小时
原因:
UTC = 本地时间(北京时间))- 0800
-
arrow.now() 本地时区时间, arrow.utcnow() UTC时区时间。
-
arrow.get() 方法默认返回值是 UTC时间,与中国的本地时间不一致,相差8小时
三、关于本地时间和utc时间的转化
可以参考:
Python arrow.get() UTC时间转换本地时间的方法
from dateutil import tz
import arrow
replace 替换成本地时区 >>> arrow.get('2020-03-02').replace(tzinfo=tz.tzlocal()).timestamp 1583078400
例子1:
t1是在【+00:00】utc时区的情况下的09:08:13,UTC = 本地时间(北京时间))- 0800,所以此时t1的本地时间应该是17:08:13
t2在经过.replace(tzinfo=tz.tzlocal())后已经将utc时区替换成本地时区,所以这里的09:08:13的本地时间应该也是09:08:13
所以实际来看,t1的时间是要比t2的时间快了8个小时
t1 = arrow.get('2020-03-02 09:08:13','YYYY-MM-DD HH:mm:ss')
t2 = arrow.get('2020-03-02 09:08:13','YYYY-MM-DD HH:mm:ss').replace(tzinfo=tz.tzlocal())
print(f"t1={t1}")
print(f"t1的时间戳={t1.timestamp()}")
print(f"t2={t2}")
print(f"t2的时间戳={t2.timestamp()}")
t1=2020-03-02T09:08:13+00:00
t1的时间戳=1583140093.0
t2=2020-03-02T09:08:13+08:00
t2的时间戳=1583111293.0
例子2:
test_dt的时间是2021年8月9日8点 现在的的时间是2021年8月9日11点
from dateutil import tz
test_dt = CommonClass.stc_class.strandard_date("今天8点")
test_dt = test_dt.replace(tzinfo=tz.tzlocal())
test_dt_stamp = test_dt.timestamp()
print(f"test_dt={test_dt}")
print(f"test_dt的时间戳={test_dt_stamp}")
# 转换
test_dt.replace(tzinfo=tz.tzlocal())
curr = base_data.current_date_time
curr_stamp = curr.timestamp()
print(f"现在的={curr}")
print(f"现在的时间戳={curr_stamp}")
print(f"相差了{(curr_stamp-test_dt_stamp)/60/60}小时")