How to create tzinfo when I have UTC offset?
python, python-2.7
Solution
With Python 3.2 or higher, you can do this using the builtin datetime library:
import datetime
datetime.timezone(-datetime.timedelta(hours=5, minutes=30))
To solve your specific problem, you could use this regex pattern:
sign, hours, minutes = re.match('([+\-]?)(\d{2})(\d{2})', '+0530').groups()
sign = -1 if sign == '-' else 1
hours, minutes = int(hours), int(minutes)
tzinfo = datetime.timezone(sign * datetime.timedelta(hours=hours, minutes=minutes))
datetime.datetime(2013, 2, 3, 9, 45, tzinfo=tzinfo)
Problem
I have one timezone's offset from UTC in seconds (`19800`) and also have it in string format - `+0530`. How do I use them to create a `tzinfo` instance? I looked into `pytz`, but there I could only find APIs that take timezone name as input.