1

I'm parsing an XML file that has the dates in GMT time. It's my first time working with timezones so I'm having some difficulty displaying the right time. In the XML file the date is like this.

2015-06-29 23:05

I set up my model with a basic datetime field like this:

date = models.DateTimeField()

...my settings.py has:

 USE_TZ = True
 TIME_ZONE = 'America/Toronto'

However when I display the time via views it shows 3:05. Not exactly sure what I'm suppost to do next.

R.J. Jackson
  • 125
  • 12

1 Answers1

1

Well, there is no way to determine the time zone of the date time you provided. If you know that it is always GMT, then convert from GMT to your local time zone which is "America/Toronto" in your case.

If possible, I'd recommend changing the date format in your XML. Use UTC, as it provides time zone info.

Check this link out: Python - Convert UTC datetime string to local datetime

Readings I recommend for dealing with time. UTC: http://www.w3.org/TR/NOTE-datetime Django Time Zone Docs: https://docs.djangoproject.com/en/1.8/topics/i18n/timezones/

Community
  • 1
  • 1
Peyman
  • 3,059
  • 1
  • 33
  • 68
  • Im parsing the XML file from another webserver. So basically after parsing it I should convert it to UTC and store it into the DB like that right? – R.J. Jackson Jun 29 '15 at 19:45
  • That's right. As long as you know the date in the XML is always in a particular time zone. it's always GMT? then convert it to UTC format and set the timezone to GMT. – Peyman Jun 29 '15 at 19:47
  • Yes the xml feed will always have GMT time, but why would I have to set my time zone to GMT? – R.J. Jackson Jun 29 '15 at 20:02
  • because UTC requires a time zone and if you want to properly create a UTC formatted date, then you need to specify time zone. – Peyman Jun 29 '15 at 20:05
  • You can keep the local time zone in your settings, but when you're converting the date in the XML feed, you need to explicitly convert from GMT because otherwise, it'd be your local time zone by default. – Peyman Jun 29 '15 at 20:06