views:

233

answers:

2

Hello,

Edit : is there a way to easily convert {{ value|date:"Z" }} to one of the +hh:mm or -hh:mm formats (because date:"Z" returns xxxx or -xxxx seconds).

Show this for more explanations about the needed format.

Thank you and sorry for my ugly english. ;)

+1  A: 

Instead of Z you need O (that's an "oh", not a "zero").

>>> from django.template import *
>>> Template('{% now "Y-m-d\TH:i:sO" %}').render(Context())
u'2009-11-29T14:33:59-0600'
SmileyChris
This is a better idea, however the HTML5 spec requires one of the +xx:xx or -xx:xx formats, and {% now "O" %} returns +xxxx or -xxxx.
+1  A: 

Just clarifying here, it's the timezone offset that needs the colon, ie 2009-11-29T14:33:59-0600 in the above example should be 2009-11-29T14:33:59-06:00 to conform to the W3C guidelines.

Looking at the code at django/utils/dateformat.py:

  def O(self):
    "Difference to Greenwich time in hours; e.g. '+0200'"
    seconds = self.Z()
    return u"%+03d%02d" % (seconds // 3600, (seconds // 60) % 60)

You could edit your local copy of django to add the ':' so; return u"%+03d:%02d" % (seconds // 3600, (seconds // 60) % 60) or create your own template tag to do effectively the same thing. But probably the easiest is to compose the string in your view, and pass that along as a variable to the template.

It doesn't look like the HTML5 version of timezone is available out of the box.

[update]

On reflection you could probably do this;

>>> from django.utils import dateformat
>>> fmt = "Y-m-d\TH:i:sO"
>>> import datetime
>>> now = datetime.datetime.now()
>>> now
datetime.datetime(2009, 12, 1, 12, 39, 48, 655867)
>>> str = dateformat.format(now, fmt)
>>> print str
2009-12-01T12:39:48+0000
>>>

and then add the ':'

>>> s = str[:-2] + ':' + str[-2:]
>>> s
u'2009-12-01T12:39:48+00:00'
>>>
tonemcd
Great, thank you. :)