0

The subject just about says it all, but here's some background: I'm about to go live with some software that's been complaining I'm using unaware log timestamps. Everything is happening in California, so all users will be in USA Pacific Time. I've tried reading the documentation but keep getting frustrated by references to other documentation that seems to be an endless self-referential graph of theoretical treatises on stuff I probably but not certainly don't need to know.

I just want the minimum I need to be using aware timestamps where I now write

record = Log(timestamp=datetime.now(), ....

I probably now have the wrong thing is TIME_ZONE in settings, but I don't know what it should be.

4dummies
  • 759
  • 2
  • 9
  • 22

1 Answers1

8

Mine looks like this:

TIME_ZONE = 'America/Los_Angeles'

if that helps.

E.J. Brennan
  • 45,870
  • 7
  • 88
  • 116
  • I'm sure it helps, but my Apache error.log still gets [Thu Mar 01 08:48:06.645640 2018] [wsgi:error] [pid 14504:tid 139722318198528] /usr/local/lib/python3.5/dist-packages/django/db/models/fields/__init__.py:1447: RuntimeWarning: DateTimeField Log.timestamp received a naive datetime (2018-03-01 08:48:06.642549) while time zone support is active. – 4dummies Mar 01 '18 at 16:48
  • it's worth noting, the django docs say "When time zone support is disabled, Django uses naive datetime objects in local time.", so you may want to look at settings for USE_TZ and set to True. Then django will store time in UTC and view in 'America/Los_Angeles' time for your users. Also from the docs: "Even if your website is available in only one time zone, it’s still good practice to store data in UTC in your database." – brandondavid Mar 01 '18 at 16:57
  • https://stackoverflow.com/questions/18622007/runtimewarning-datetimefield-received-a-naive-datetime – E.J. Brennan Mar 01 '18 at 17:03
  • I was reading elsewhere (https://docs.djangoproject.com/en/2.0/topics/i18n/timezones/) and realized I needed to change imports, and use timezone.now() instead of datetime.now(). Thanks to you and that doc, it no longer issues the warning. Thanks. – 4dummies Mar 01 '18 at 17:10