tags:

views:

19

answers:

1

I have a DB "TimeZoneField" type for users, how do I use that with the "datetime.datetime()" object to get a strftime() string like '%Y-%m-%dT%H:%M:%S.000Z'?

+1  A: 

This was for Google Calendar's Python data API feed and it was a lot of work because Python's datetime library doesn't support ISO 8601: http://wiki.python.org/moin/WorkingWithTime

In addition if you transmit dates with .000Z timezone to Google calendar it will ignore DST (Daylight Savings Time) for events that occur in EDT and others (so things will be off by an hour for parts of the year.)

Here is my fix: Assuming start_time and end_time are timezone aware datetime.datetime objects:

    timezone_string = start_datetime.strftime('%z')[0:3] + ":" + start_datetime.strftime('%z')[3:6]    
    start_time = start_datetime.strftime('%Y-%m-%dT%H:%M:%S' + timezone_string)
    end_time = end_datetime.strftime('%Y-%m-%dT%H:%M:%S' + timezone_string)

Note that stftime("%z") doesn't include that ":" character to separate the hours/minutes in the offset that Google's calendar API requires.

MikeN