djangosnippets.org: Latest snippets tagged with 'utc'https://djangosnippets.org/tags/utc/2012-05-27T10:18:44-05:00Querying datetime aware objects in your local timezone2012-05-27T10:18:44-05:00jayliewhttps://djangosnippets.org/snippets/2759/<p>I have a model with a datetime field that I used as a timestamp. I’m in California’s timezone (“America/Los_Angeles”). The data is saved in UTC in MySQL (as confirmed by the ORM). I just want to do a query that looks like this: “give me all the ...</p>
Freely redistributableUTC-based astimezone filter2011-10-27T15:22:30-05:00miracle2khttps://djangosnippets.org/snippets/2583/<p>A version of http://djangosnippets.org/snippets/2388/ which assumes UTC-based dates.</p>
<p>This is when you store dates in UTC (as you should), and want to display them in your site's local timezone, and you notice that Django's timesince/time template tags still do not support timezones.</p>
Freely redistributableJSON encode ISO UTC datetime2009-04-13T09:04:51-05:00japerkhttps://djangosnippets.org/snippets/1435/<p>If you want to do your own JSON serialization of <a href="http://docs.python.org/library/datetime.html#datetime.datetime">datetime</a> objects instead of using DjangoJSONEncoder, use <code>simplejson.dumps(o, default=encode_datetime)</code>. The <code>encode_datetime</code> method will convert the datetime object to UTC and output an ISO format string just like the <a href="http://www.djangosnippets.org/snippets/1424/">isoutc template filter</a>.</p>
Freely redistributableisoutc template filter2009-04-09T21:32:13-05:00japerkhttps://djangosnippets.org/snippets/1424/<p>Use this template filter to produce an ISO format UTC datetime string from a <a href="http://docs.python.org/library/datetime.html#datetime.tzinfo">timezone aware</a> <a href="http://docs.python.org/library/datetime.html#datetime.datetime">datetime</a> object. Usage example in a template:
<code>&lt;input name="when" type="hidden" value="{{ dt|isoutc }}"</code>.
You must have <a href="http://labix.org/python-dateutil">dateutil</a> installed for <code>tz.tzutc()</code> to work. And of course, you'll need to load it ...</p>
Freely redistributableConverting PDT to UTC using pytz and dateutil2008-08-21T15:54:48-05:00simonhttps://djangosnippets.org/snippets/995/<p>Every time I have to do this it takes me a solid half hour to figure it out, so I'm throwing it up here for future reference. I hate timezone calculations.</p>
Freely redistributableUTC DateTime field2007-08-26T05:03:03-05:00ludohttps://djangosnippets.org/snippets/388/<p>A DateTime field extension that automatically stores the timezone, and the computed UTC equivalent. This field needs the pytz library.</p>
<p>The field adds two new fields to the model, with the same name of the UTCDateTimeField field, and a suffix. For an UTCDateTimeField named 'updated', the model will contain</p>
<ul>
<li>
<p>an ...</p></li></ul>
Freely redistributable