On Apr 14, 7:26 pm, Jamie <utkja...@gmail.com> wrote:
> I am working on an app that will export a calendar event to Google
> Calendar. Google Calendar requires that event datetimes be in UTC
> format. I can make the conversion from local (America/New_York) to UTC
> using pytz, but the time is off by an hour due to daylight savings
> time (Python thinks the offset is -0500 rather than -0400) so all of
> my exported events are off as a result.

pytz does adjust for daylight savings time, that is one of its primary
jobs.

>
> I'm first taking the naive datetime stored by Django and adding the
> local timezone pulled from settings.py then using astimezone(pytz.utc)
> to make the conversion to UTC. I'm not sure how to adjust for DST,
> especially since all the events will be in the future and DST will
> have to be calculated for each of them.

Try using 'US/Eastern' instead of 'America/New_York'. It might be
different.

This is how I am doing it (I'm also integrating with Google Calendar):

from django.utils.tzinfo import FixedOffset

d = datetime.datetime.now()
tz_name = 'US/Eastern'  # or whatever
tz = pytz.timezone(tz_name)  # create timezone
local = tz.localize(d)  # make naive datetime localized
zulu = local.astimezone(FixedOffset(0))  # convert to UTC
s = zulu.strftime('%Y-%m-%dT%H:%M:%S.000Z')

I think I probably could have used pytz.utc instead of FixedOffset(0).
It's the same idea. But at least now this thread has Django
content. :-)

Hope that helps,
BN





--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to