Are time zone definitions actually required/used in MySQL?

403 views
Skip to first unread message

Michael Lenaghan

unread,
Nov 14, 2017, 8:35:28 PM11/14/17
to Django users
For MySQL the Django docs say:

If you plan on using Django’s timezone support, use mysql_tzinfo_to_sql to load time zone tables into the MySQL database. This needs to be done just once for your MySQL server, not per database.

I can't find any evidence that Django actually uses those tables?

Here, for example, is a snippet from adapt_datetimefield_value():

        # MySQL doesn't support tz-aware datetimes
        if timezone.is_aware(value):
            if settings.USE_TZ:
                value = timezone.make_naive(value, self.connection.timezone)
            else:
                raise ValueError("MySQL backend does not support timezone-aware datetimes when USE_TZ is False.")

adapt_timefiled_value() is even simpler:

        # MySQL doesn't support tz-aware times
        if timezone.is_aware(value):
            raise ValueError("MySQL backend does not support timezone-aware times.")

What am I missing? 

bill.torcaso

unread,
Nov 15, 2017, 10:53:27 AM11/15/17
to Django users

I frequently use Django + MySql on a pristine Vagrant virtual machine.  And on a fresh machine, for the first time, I had to manually load time zone information into mysql.  

'runserver' refused to run, although 'python ./manage.py --help' ran fine (that is not a surprise - --help does not touch the database)

'runserver' said that a query failed, because of something being too old.  I'm sorry that I cannot quote the error message now.  But it would not run at all.

The solution was to run mysql_tzinfo_to_sql.

>>> Does anyone understand why this is a problem now?  I haven't changed anything in my requirements.txt.  I suppose that my Ubuntu image for Virtual Box was upgraded recently and that might explain it.

I'd like to understand why this happened.

Mike Dewhirst

unread,
Nov 15, 2017, 4:34:05 PM11/15/17
to django...@googlegroups.com
On 16/11/2017 2:53 AM, bill.torcaso wrote:
>
> I frequently use Django + MySql on a pristine Vagrant virtual
> machine.  And on a fresh machine, for the first time, I had to
> manually load time zone information into mysql.
>
> 'runserver' refused to run, although 'python ./manage.py --help' ran
> fine (that is not a surprise - --help does not touch the database)
>
> 'runserver' said that a query failed, because of something being too
> old.  I'm sorry that I cannot quote the error message now.  But it
> would not run at all.
>
> The solution was to run mysql_tzinfo_to_sql
> <https://www.google.com/url?q=https%3A%2F%2Fdev.mysql.com%2Fdoc%2Frefman%2Fen%2Fmysql-tzinfo-to-sql.html&sa=D&sntz=1&usg=AFQjCNH8YJbKdB-R4TSqRnjFDA9KkagrBw>.
>
> >>> Does anyone understand why this is a problem now?

Time marches on and daylight savings changes occur all over the world.
The actual problem might have been that East Coast Australia delayed
going to daylight saving until after the recent AFL grand final. We
normally switch in concert with our neighbours (eg NZ) but not this
year. I suspect such arbitrary adjustments require new tzinfo data from
time to time. Richmond won by the way.

> I haven't changed anything in my requirements.txt.  I suppose that my
> Ubuntu image for Virtual Box was upgraded recently and that might
> explain it.
>
> I'd like to understand why this happened.
>
>
> On Tuesday, November 14, 2017 at 8:35:28 PM UTC-5, Michael Lenaghan
> wrote:
>
> For MySQL the Django docs say
> <https://docs.djangoproject.com/en/1.11/ref/databases/#time-zone-definitions>:
>
>
> If you plan on using Django’s timezone support
> <https://docs.djangoproject.com/en/1.11/topics/i18n/timezones/>,
> use mysql_tzinfo_to_sql
> <https://dev.mysql.com/doc/refman/en/mysql-tzinfo-to-sql.html> to
> load time zone tables into the MySQL database. This needs to
> be done just once for your MySQL server, not per database.
>
>
> I can't find any evidence that Django actually uses those tables?
>
> Here
> <https://github.com/django/django/blob/master/django/db/backends/mysql/operations.py#L164>,
> for example, is a snippet from adapt_datetimefield_value():
>
>       # MySQL doesn't support tz-aware datetimes
>         if timezone.is_aware(value):
>             if settings.USE_TZ:
>                 value = timezone.make_naive(value,
> self.connection.timezone)
>             else:
>                 raise ValueError("MySQL backend does not
> support timezone-aware datetimes when USE_TZ is False.")
>
>
> adapt_timefiled_value() is even simpler
> <https://github.com/django/django/blob/master/django/db/backends/mysql/operations.py#L188>:
>
>       # MySQL doesn't support tz-aware times
>         if timezone.is_aware(value):
>             raise ValueError("MySQL backend does not support
> timezone-aware times.")
>
>
> What am I missing?
>
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to django-users...@googlegroups.com
> <mailto:django-users...@googlegroups.com>.
> To post to this group, send email to django...@googlegroups.com
> <mailto:django...@googlegroups.com>.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/f502ca14-2342-45b6-810a-9f9bbec11da2%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/f502ca14-2342-45b6-810a-9f9bbec11da2%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.

bill.torcaso

unread,
Nov 16, 2017, 9:35:38 AM11/16/17
to Django users

But how would my Vagrant VM know anything about that?
Reply all
Reply to author
Forward
0 new messages