摆。期间实例。如何从中获取小时数

Pendulum. Period instance. How to get hours from it

无论我在句号a之后放什么都得0。为什么?

import pendulum

home = 'Europe/Berlin'
away = 'America/New_york'

a = pendulum.now(home)
b = pendulum.now(away)
print(a)
print(b)
dif = b.diff(a)
print (dif)
di = dif.in_hours()
print (di)
d = dif.hours
print (d)

输出:

2019-02-23T21:20:22.738058+01:00

2019-02-23T15:20:22.738058-05:00

Period [2019-02-23T15:20:22.738058-05:00 -> 2019-02-23T21:20:22.738058+01:00]

0

0

你得到的是零,因为 a 和 b 都代表你当地时间的同一时刻(它们之间会有微秒的差异,具体取决于变量的创建时间)但是在不同的时区。

通过说 a = pendulum.now(home) 和 b= pendulum.now(away) 你不是在不同的地方创建时间,而是根据他们的时区来表示你的本地时间。

如果你做了 dif._delta 那么你会得到

0 years 0 months 0 days 0 hours 0 minutes 0 seconds 170 microseconds

170 微秒是解释器制作 a 和 b 之间的差异。