I have the following code in one of my models:
def shortDescription(self):
return self.name + ' ' + self.class_date.strftime("%I:%M")
self.class_date is a timezone aware DateTimeField, self.class_date.is_aware() is True, USE_TZ is True.
The shortDescription returns a string that gives the time in UTC rather than the default timezone, putting {{ aclass.class_date }} in the template displays the time in the correct zone.
Is strftime always working on the base, native time? Or what else is going on here?
When you directly reference pieces of the datetime like
%Ior%M, it uses it straight as it is with no locale conversion. If you included%Zyou’d see that the time is inUTC. If you want locale-aware results, you need use the more limited%X, which will simply spit out the full time converted for the locale.If you need more, you’ll have to convert it:
Or, you can rely on the
datefilter, which automatically does this for you: