Human readable timezone in python?
Asked Answered
P

2

4

How can I get human readable timezone from these timeformat in python?

And how can I convert the same time to be in that timezone?

'scheduled_datetime': '2017-07-30T10:00:00+05:30'

session.scheduled_datetime
datetime.datetime(2017, 7, 30, 10, 0, tzinfo=<DstTzInfo 'Asia/Kolkata' IST+5:30:00 STD>)
Phenetidine answered 28/6, 2017 at 9:24 Comment(3)
Please read minimal reproducible example. No idea what you mean by "human readable timezone"?Clo
What does "human readable timezone" even mean in this case? In your input you only have a UTC offset, not time zone info.Pendley
I mean when i will use tzname() it will give me '+0530' as timezone whereas i am expecting it to be as 'IST' (Indian Standard Time)Phenetidine
S
6

You can do it with iso8601 or dateutil.parser:

import iso8601
import dateutil.parser
from pytz import timezone

fmt = '%Y-%m-%d %H:%M:%S %Z'
ist =  timezone('Asia/Kolkata')

str = '2017-07-30T10:00:00+05:30'
d = iso8601.parse_date(str).astimezone(ist)
print(d.strftime(fmt))

d = dateutil.parser.parse(str).astimezone(ist)
print(d.strftime(fmt))

The output for both cases is:

2017-07-30 10:00:00 IST


Note that I used Asia/Kolkata instead of IST. That's because these 3-letter names are ambiguous and not standard: IST can be India Standard Time, Israel Standard Time or Irish Standard Time (this last one is also called Irish Summer Time, as it's the timezone used during Ireland's Daylight Saving Time).

Always prefer to use IANA timezones names (always in the format Continent/City, like America/Sao_Paulo or Europe/Berlin). If Asia/Kolkata is not what you need, check this list to find the one that suits best for your case.


If you don't know which timezone to use and want to guess based on the offset, I'm afraid is not that simple.

Using your input as example: the offset is +05:30 (5 hours and 30 minutes ahead of UTC). There's more than one timezone that uses this offset (or used once in its history). The wikipedia link contains 3, but I could find the following:

Asia/Dhaka, Asia/Calcutta, Asia/Karachi, Asia/Dacca, Asia/Thimbu, Asia/Katmandu, Asia/Thimphu, Asia/Kolkata, Asia/Colombo, Asia/Kathmandu

All of these timezones uses (or had used in the past) the +05:30 offset. As timezones can change during history, you have some problems when trying to guess the timezone based on the offset:

  • you need to know what was the current offset at the specified date and time. In your case, you're using 2017-07-30, but I'm assuming you want to handle any dates in the past.
  • there can be more than one timezone with the valid offset at the specified date and time (then you'll have to choose one, based on any arbitrary criteria).
  • some timezones, depending on the date, can be in Daylight Saving Time, which means that the offset won't be +05:30 (or even worse, some might use +05:30 during DST).

So, you can't get just one single timezone, based on the offset. The best you can do is to get a list of candidates: a list with all timezones in which the offset was valid at the respective date/time. Then you'll have to choose one of them (maybe if you have a list of "prefered ones"):

str = '2017-07-30T10:00:00+05:30'
# parse date and get the offset (in this case, +05:30)
d = iso8601.parse_date(str)
offset = d.tzinfo.utcoffset(d)

# a set with all candidate timezones
zones = set()
# find a zone where the offset is valid for the date
for tz in pytz.all_timezones:
    zone = timezone(tz)
    # get the offset for the zone at the specified date/time
    zoneoffset = zone.utcoffset(d.replace(tzinfo=None))
    if (zoneoffset == offset):
        zones.add(zone)

# just checking all the candidates timezones
for z in zones:
    print(z, d.astimezone(z).strftime(fmt))

The output will be:

Asia/Kolkata 2017-07-30 10:00:00 IST
Asia/Colombo 2017-07-30 10:00:00 +0530
Asia/Calcutta 2017-07-30 10:00:00 IST

Note that 3 timezones were found (Kolkata, Colombo and Calcutta). That's because all these 3 timezones have a valid offset of +05:30 on 2017-07-30. For some reason, Colombo were not formatted as IST: I think it depends on how this timezone is configured in Python, or my version is not updated (I'm using Python 3.5.2) - I've tested this in Java and it's formatted as IST, so probably it's a configuration issue in my Python instalation.

Once you have all the candidates timezones, you'll have to choose one (and I'm not sure what would be the best criteria for that).

PS: actually there are only 2 options, because Asia/Kolkata and Asia/Calcutta are synonyms. But you still have more than one option and the question remains: which one to choose?

Sowder answered 28/6, 2017 at 19:30 Comment(1)
You have mention the str time correctly ..but the problem i am facing is i am not passing or have no idea about ist = timezone('Asia/Kolkata') in my program i have to calculate the same timezone from iso format datetime string .I just have the str = '2017-07-30T10:00:00+05:30' .Phenetidine
S
0
import time,datetime,os
localtime = time.asctime( time.localtime(time.time()) )
print ("Local current time :", localtime)  #This is localtime

os.environ['TZ'] = 'EST+05EDT,M4.1.0,M10.5.0'  #Here u need to specify what might be #the timezone
time.tzset()
print time.strftime('%X %x %Z')
Shrub answered 28/6, 2017 at 10:8 Comment(4)
Thanks for responding priya...but i want the timezone is human readable form from the datetime object or iso format datetime.Phenetidine
For ex: I have the date time value in either format .'scheduled_datetime': '2017-07-30T10:00:00+05:30' session.scheduled_datetime datetime.datetime(2017, 7, 30, 10, 0, tzinfo=).HOw can i get the 'IST' timezone from these values .Phenetidine
Try to install pytz, it will help you a lot when working with different timezones (and UTC). The current UTC time (independent from the timezone of your computer) can be obtained with: import pytz from datetime import datetime now = datetime.now(pytz.utc) now is now datetime.datetime(2011, 11, 30, 14, 26, 30, 628014, tzinfo=<UTC>) and you can use it to calculate the current UTC hour with now.hour (returns 14) Please refer pypi.python.org/pypi/pytzShrub
This is not what i wanted....i am passing datetime in ISO format in serilaizer.Now i want the timezone in readable format in human readable form so that the user can understand in view.Phenetidine

© 2022 - 2024 — McMap. All rights reserved.