Slide 1

Slide 1 text

࣌ࠩॄኄత࠷౼Ԁྃ

Slide 2

Slide 2 text

Timezone in Websites • Totally broken in all possible ways • Two negatives make a positive! • But if you (by chance) get some of them right…

Slide 3

Slide 3 text

Databases • All databases do not support timezones • PostgreSQL: yes • SQLite: no • MySQL: sometimes • Django stores everything in UTC

Slide 4

Slide 4 text

Naïve vs Aware >>> import datetime! >>> now = datetime.datetime.now()! >>> print now! 2014-01-21 19:09:14.960786! >>> print now.replace(tzinfo=taipei)! 2014-01-21 19:09:14.960786+08:00

Slide 5

Slide 5 text

Naïve vs Aware >>> import datetime! >>> now = datetime.datetime.now()! >>> print now! 2014-01-21 19:09:14.960786! >>> print now.replace(tzinfo=taipei)! 2014-01-21 19:09:14.960786+08:00 naïve aware

Slide 6

Slide 6 text

naïve /nai-eev/ 1. Lacking worldly experience, wisdom, or judgement; unsophisticated. Surely you're not naïve enough to believe adverts! … http://en.wiktionary.org/wiki/naive

Slide 7

Slide 7 text

pytz • The Olson Timezone Database • pip install pytz • taipei = pytz.timezone(‘Asia/Taipei') • Django depends on this for timezone support http://pytz.sourceforge.net

Slide 8

Slide 8 text

USE_TZ • USE_TZ = False • Naïve objects with your default timezone • USE_TZ = True • Aware objects (Naïve ones generate warnings) • Django makes a date/time aware automatically when it’s retrieved from database

Slide 9

Slide 9 text

>>> from django.utils.timezone import *! >>> print now()! 2014-01-21 11:09:14.960786+00:00! >>> from django.conf import settings! >>> print settings.USE_TZ! True django.utils.timezone

Slide 10

Slide 10 text

HTTP • HTTP header doesn’t have a “Accept-Timezone” field • You need to manually set a request’s timezone if you need timezone support

Slide 11

Slide 11 text

from django.utils.timezone import *! from pytz import timezone! ! taipei = timezone('Asia/Taipei')! activate(taipei)! ! print get_current_timezone_name()! # Asia/Tapei! ! with override(utc):! print get_current_timezone_name()! # UTC! ! deactivete()

Slide 12

Slide 12 text

{# "value" is a date/time object #}! ! {% load tz %}! ! {# Neither of these respect USE_TZ #}! {% localtime on %}! {{ value }}! {% endlocaltime %}! ! {% localtime off %}! {{ value }}! {% endlocaltime %}

Slide 13

Slide 13 text

{% load tz %}! ! {% timezone "Europe/Paris" %}! Paris time: {{ value }}! {% endtimezone %}! ! {% get_current_timezone as timezone %}! ! {{ value|localtime }}! ! {{ value|utc }}! ! {{ value|timezone:"Europe/Paris" }}

Slide 14

Slide 14 text

DST • No universal standard • Varies every year • Some nations only partly use it (e.g. Australia) • pytz helps (but you still need to be cautious)

Slide 15

Slide 15 text

Modern Technology • HTML5 Geolocation API • Client-side processing • Still not reliable

Slide 16

Slide 16 text

Related Reading http://docs.python.org/2/library/datetime.html http://pytz.sourceforge.net https://docs.djangoproject.com/en/1.6/topics/i18n/timezones/ http://d.pr/S9UL (Datetime, timetuple and timestamp) https://gist.github.com/moskytw/7818553