pythonperiodpendulum

Pendulum. Period instance. How to get hours from it


No matter what I put after the period a get 0. Why?

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)

Output :

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

Solution

  • You are getting zero, because a and b are both representing the same moment of your local now time (there will be microsec difference between them based on when the variables are created) but in different time zones.

    By saying a = pendulum.now(home) and b= pendulum.now(away) you are not creating times at different places but rather your local time in representation according to their timezone.

    if you did dif._delta then you would get

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

    170 microseconds is the difference between making a and b by the interpreter.