0

I want to use the 'c' date format with the Django date filter. That format references 'naive' dates. I don't want to have timezones in my template (that's held elsewhere in my xml).

I'm not sure what that is, the documentation for django doesn't mention it, nor does the PHP site it references.

What is it, and how do I get rid of it?

AncientSwordRage
  • 7,086
  • 19
  • 90
  • 173

1 Answers1

2

The documentation refers to python dates, of these available types.

An object of type time or datetime may be naive or aware. A datetime object d is aware if d.tzinfo is not None and d.tzinfo.utcoffset(d) does not return None. If d.tzinfo is None, or if d.tzinfo is not None but d.tzinfo.utcoffset(d) returns None, d is naive. A time object t is aware if t.tzinfo is not None and t.tzinfo.utcoffset(None) does not return None. Otherwise, t is naive.

So naive just means it does not have any time zone information.

To make something 'aware' follow this method by unutbu:

In general, to make a naive datetime timezone-aware, use the localize method:

import datetime
import pytz

unaware = datetime.datetime(2011, 8, 15, 8, 15, 12, 0)
aware = datetime.datetime(2011, 8, 15, 8, 15, 12, 0, pytz.UTC)

now_aware = pytz.utc.localize(unaware)
assert aware == now_aware

For the UTC timezone, it is not really necessary to use localize since there is no daylight savings time calculation to handle:

now_aware = unaware.replace(tzinfo=pytz.UTC)

works. (.replace returns a new datetime; it does not modify unaware.)

To make it unaware set the timezone to None.

Community
  • 1
  • 1
AncientSwordRage
  • 7,086
  • 19
  • 90
  • 173