Simon Mitchell
Simon Mitchell

Reputation: 23

Timezone offsets not printing to a rounded hour

When I run the following code;

tz_Pacific = pytz.timezone('US/Pacific')
tz_Tasmania = pytz.timezone('Australia/Tasmania')

time1 = datetime(2020, 10, 7, 18, 0, 0, tzinfo=tz_Pacific)
time2 = datetime(2020, 10, 7, 14, 20, 21, tzinfo=tz_Tasmania)

print(time1)
print(time2)

I get the following output;

2020-10-07 18:00:00-07:53

2020-10-07 14:20:21+09:49

Why would the tz offsets be -07:53 and +09:49 respectively?

Upvotes: 2

Views: 508

Answers (1)

FObersteiner
FObersteiner

Reputation: 25544

Why you get these "weired" offsets with pytz? Those are the first entries from the database for the respective time zones. With pytz, if you don't localize, these won't be adjusted to the time of your datetime object. Here's a nice blog post by Paul Ganssle giving more insights.

from datetime import datetime
import pytz

tz_Pacific = pytz.timezone('US/Pacific')
tz_Tasmania = pytz.timezone('Australia/Tasmania')

# note the LMT for local mean time:
print(repr(tz_Pacific))
print(repr(tz_Tasmania))
# <DstTzInfo 'US/Pacific' LMT-1 day, 16:07:00 STD> 
# <DstTzInfo 'Australia/Tasmania' LMT+9:49:00 STD>

# correctly localized you get
time1 = tz_Pacific.localize(datetime(2020, 10, 7, 18, 0, 0))
time2 = tz_Tasmania.localize(datetime(2020, 10, 7, 14, 20, 21))
print(time1)
print(time2)
# 2020-10-07 18:00:00-07:00
# 2020-10-07 14:20:21+11:00

Further remarks:

Upvotes: 1

Related Questions