-1

I have a Python datetime object and here is the offset and tzname(). As in Django, the timezone is stored as UTC, I want it store the the tzname separately, so that I can use that field to reconvert to the actual datetime.

>>>from dateutil.parser import parse
>>>dt = parse('Tue Apr 26 2016 08:32:00 GMT-0400 (EDT)')

>>> dt
datetime.datetime(2016, 4, 26, 8, 32, tzinfo=tzoffset('EDT', 14400))
>>> dt.tzinfo
tzoffset('EDT', 14400)
>>> dt.tzname()
'EDT'

Question:-

When I store "dt" object in Django it converts it to UTC format.How do I reconvert the UTC format to EDT format?

I'm using this link as reference but I'm not sure how to create the to_zone object for 'EDT'. 'UTC' works fine but tz.gettz('EDT') is always None.

>>> to_zone=tz.gettz('EDT')
>>> to_zone
>>> to_zone=tz.gettz('UTC')
>>> to_zone
tzfile('/usr/share/zoneinfo/UTC')

Python - Convert UTC datetime string to local datetime

Community
  • 1
  • 1
user1050619
  • 19,822
  • 85
  • 237
  • 413
  • What is your input? (`type(input_object)`). Do you want to *display* a given UTC time in `America/New_York` time zone? (do you know about `timezone.activate()`, `localtime`?) What are `USE_TZ`, `TIME_ZONE`, `timezone.get_current_timezone()`? – jfs Apr 26 '16 at 07:24
  • I have added all the details you need...When I store the "dt" object in Django it automatically converts to 'UTC' format. I want to retrieve that 'UTC' object and convert it into 'EDT' format again. – user1050619 Apr 26 '16 at 12:42

1 Answers1

0

The datetime package has a method "astimezone":

datetime.astimezone

So

tzone = tz.gettz('EDT')
newdate = olddate.astimezone(tzone)
wm3ndez
  • 721
  • 6
  • 10