Reputation: 5787
I am trying to read times with their timezone specified by its UTC-offset and store them as python datetimes.
The pytz
module provides the available timezones and I think the complete list is given in this question. If so, most of the times can be stored by using the corresponding Etc/
timezone and flipping the sign:
Etc/GMT
Etc/GMT+0
Etc/GMT+1
Etc/GMT+10
Etc/GMT+11
Etc/GMT+12
Etc/GMT+2
Etc/GMT+3
Etc/GMT+4
Etc/GMT+5
Etc/GMT+6
Etc/GMT+7
Etc/GMT+8
Etc/GMT+9
Etc/GMT-0
Etc/GMT-1
Etc/GMT-10
Etc/GMT-11
Etc/GMT-12
Etc/GMT-13
Etc/GMT-14
Etc/GMT-2
Etc/GMT-3
Etc/GMT-4
Etc/GMT-5
Etc/GMT-6
Etc/GMT-7
Etc/GMT-8
Etc/GMT-9
However, some timezones, like Newfoundland Standard Time and Afghanistan Time are half-hour offsets from GMT (-3:30 and +4:30, respectively). How can I store these times with their appropriate timezone without manually mapping these specific offsets to America/St_Johns
or Asia/Kabul
?
Upvotes: 0
Views: 1015
Reputation: 414079
Do not use Etc/GMT±h
POSIX timezones. They are present only for historical reasons (and perhaps for ships in the sea).
In general, utc offset is not enough to specify a timezone i.e., the same timezone may have different utc offsets at different times. And in reverse, multiple timezones may have the same utc offset at some point.
If you are given a fixed utc offset with corresponding dates then you could use any FixedOffset
implementation.
If you want to get the correct time for other dates then you have to map your input data to the correct timezone ids such as 'America/St_Johns'
. See pytz: return Olson Timezone name from only a GMT Offset.
Upvotes: 1
Reputation: 4476
There is something oyu can do, although not exactly what you seem to want.
>>> nf_offset = pytz.FixedOffset(-150) # offset is in minutes, so 150=2.5 hours
>>> nf_tz = pytz.datetime("Canada/Newfoundland")
>>> datetime.now(nf_tz).strftime("%H:%M") == datetime.now(nf_offset).strftime("%H:%M")
True
They are different objects, so it's not certain you can do what you're hoping after this point, but this will get you a generic object from which you can compare times at arbitrary offsets from UTC.
Upvotes: 1