djangosnippets.org: Latest snippets tagged with 'timezone'https://djangosnippets.org/tags/timezone/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 redistributableTimezone choice field for select box
2012-03-01T22:16:26.608011-06:00jmunrohttps://djangosnippets.org/snippets/2703/<p>A simple 2-tuple list of time zones for use in a choice field or select box for Django, or wherever needed.</p>
Freely redistributableiCal for Google Calendar
2012-01-28T04:29:22.976879-06:00TrevorPhttps://djangosnippets.org/snippets/2666/<p>This code publishes an iCal file that can be subscribed to in Google Calendar. They change the way they interpret iCal data occasionally, so this may break, I'll try to keep it up to date.</p>
<p>There is some crazy string replace stuff going on there, I haven't yet convinced vObject …</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 redistributableTemplate tags for localizing UTC times with pytz
2011-08-21T18:03:06.671616-05:00woleverhttps://djangosnippets.org/snippets/2519/<p>For example:</p>
<pre><code>Last modified: {% localdt item.modified_utc %} ({% localtimesince time.modified_utc %})
</code></pre>
<p>Converts the input datetimes to the timezone specified by the localtz context variable (it can also be explicitly specified, and all those other sensible things).</p>
<p>Input UTC datetimes can be specified using either a datetime or a timestamp. …</p>
Freely redistributableastimezone template tag
2011-03-09T14:57:38.909743-06:00whardierhttps://djangosnippets.org/snippets/2388/<p>should probably be migrated to an inclusion tag to allow a source timezone that isn't the site specific TIME_ZONE.</p>
<p>This code assumes that your database stores dates according to the django.conf.settings.TIME_ZONE variable.</p>
<p>Yes.. this assumes that dates are stored in the database according to system time. On my systems the …</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 redistributablefilter dates to user profile's timezone
2007-04-16T23:38:42.465021-05:00Scannerhttps://djangosnippets.org/snippets/183/<p>I have users in many timezones and I let them set their preferred display timezone in their user profile as a string (validated aginst the pytz valid timezones).</p>
<p>This is a filter that takes a datetime as input and as an argument the user to figure out the timezone from. …</p>
Freely redistributable