Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django DateTimeField with UTC offset?

Tags:

I have a model with a DateTimeField:

deadline = models.DateTimeField(verbose_name="Valid unitl", null=True, blank=True)

Users should be allowed to input date, time and timezone info in the field. This is my desired format:

2012-12-31 23:30 +0430

I expect the time will get converted to UTC before storing to db. So I tried using a model form for that, but it throws Enter a valid date/time. validation error on that DateTimeField if I enter the value above.

This is in settings.py:

DATE_INPUT_FORMATS = ('%Y-%m-%d %H:%M %Z', )

What am I missing?

Edit:

As per Видул Петров's suggestion, tried to use a form field:

deadline2 = forms.DateTimeField(input_formats=['%Y-%m-%d %H:%M %Z',],

Got the same effect: Enter a valid date/time.

Edit 2

It appears that datetime can't handle the "%z" parameter. This throws a ValueError:

datetime.datetime.strptime(value, format)

So I tested it in console:

>>> import datetime
>>> datetime.datetime.strptime('2012-12-30 19:00 +0100', "%Y-%m-%d %H:%M %z")
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/_strptime.py", line 317, in _strptime
(bad_directive, format))
ValueError: 'z' is a bad directive in format '%Y-%m-%d %H:%M %z'

Also tried pytz:

>>> import pytz
>>> pytz.datetime.datetime.strptime('2012-12-30 19:00 +0100', "%Y-%m-%d %H:%M %z")
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/_strptime.py", line 317, in _strptime
(bad_directive, format))
ValueError: 'z' is a bad directive in format '%Y-%m-%d %H:%M %z'

I really feel this should work. Did I miss some part of the docs that says otherwise?

like image 367
frnhr Avatar asked Dec 28 '12 19:12

frnhr


People also ask

How do I get UTC time in Django?

The solution to this problem is to use UTC in the code and use local time only when interacting with end users. Time zone support is disabled by default. To enable it, set USE_TZ = True in your settings file. In Django 5.0, time zone support will be enabled by default.

What is Use_tz in Django?

When USE_TZ is False, this is the time zone in which Django will store all datetimes. When USE_TZ is True, this is the default time zone that Django will use to display datetimes in templates and to interpret datetimes entered in forms. ... When django rest framework takes the naive datetime data from request.

How does Django store time zones?

Neither django nor python provide a set of timezones for you to use. For that, you will need an additional module like pytz . You can get a list of all timezones like this: >>> import pytz >>> pytz.


1 Answers

When you set USE_TZ = True in your settings, Django stores date and time information in UTC in the database otherwise it will store naive date time (date time without timezone).

In most cases using Django's time zones support is very convenient because input and output datetime will be automatically translate by Django.

But if you really need timezone input from your user, you will need to set USE_TZ = False then use DateTimeField which is naive datetime along with CharField to store timezone information in your models.py.

ref: https://docs.djangoproject.com/en/2.1/topics/i18n/timezones/

like image 167
Chatri Sae-Tung Avatar answered Sep 19 '22 05:09

Chatri Sae-Tung