djangosnippets.org: Latest snippets tagged with 'utc'https://djangosnippets.org/tags/utc/2012-05-27T10:18:44.805557-05:00Querying datetime aware objects in your local timezone
2012-05-27T10:18:44.805557-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 information with day …</p>
Freely redistributableUTC-based astimezone filter
2011-10-27T15:22:30.758952-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 datetime
2009-04-13T09:04:51.647479-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 filter
2009-04-09T21:32:13.883189-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><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 as a <a href="http://docs.djangoproject.com/en/dev/howto/custom-template-tags/#howto-custom-template-tags">custom …</a></p>
Freely redistributableConverting PDT to UTC using pytz and dateutil
2008-08-21T15:54:48.653959-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 field
2007-08-26T05:03:03.294816-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