djangosnippets.org: Latest snippets posted by japerkhttps://djangosnippets.org/users/japerk/2009-04-13T09:14:10.613597-05:00japerkJSON decode datetime
2009-04-13T09:14:10.613597-05:00japerkhttps://djangosnippets.org/snippets/1436/<p>If you have JSON objects with <code>datetime</code> attributes that you want to decode to python <a href="http://docs.python.org/library/datetime.html#datetime.datetime">datetime</a> objects, you can use <code>decode_datetime</code> as a <a href="http://simplejson.googlecode.com/svn/tags/simplejson-2.0.9/docs/index.html">simplejson</a> object hook. <code>simplejson.loads(s, object_hook=decode_datetime)</code>.</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 redistributabletimeto template filter
2009-04-10T11:27:30.404782-05:00japerkhttps://djangosnippets.org/snippets/1426/<p>This is a more compact version of django's <a href="http://docs.djangoproject.com/en/dev/ref/templates/builtins/#timeuntil">timeuntil</a> filter that only shows hours & minutes. If used like <code>{{ dt|timeto }}</code>, will produce output like "1hr 30min". If you know for sure that the server has the same timezone as the <a href="http://docs.python.org/library/datetime.html#datetime-objects">datetime</a> value, then you don't need <a href="http://labix.org/python-dateutil#head-587bd3efc48f897f55c179abc520a34330ee0a62">datetutil.tz …</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 redistributableFuzzyDateTimeField
2009-04-09T16:45:33.659046-05:00japerkhttps://djangosnippets.org/snippets/1422/<p>FuzzyDateTimeField is a drop in replacement for the standard <a href="http://docs.djangoproject.com/en/dev/ref/forms/fields/#datetimefield">DateTimeField</a> that uses <a href="http://labix.org/python-dateutil#head-a23e8ae0a661d77b89dfb3476f85b26f0b30349c">dateutil.parser</a> to clean the value. It has an extra keyword argument <code>fuzzy=True</code>, which allows it to be more liberal with the input formats it accepts. Set <code>fuzzy=False</code> for more strict validation.</p>
Freely redistributable