Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Speaker Deck
PRO
Sign in
Sign up for free
Timezone support in Django
Tzu-ping Chung
January 22, 2014
Programming
0
130
Timezone support in Django
On Python datetime and timezone support in Django. Not a pretty sight.
Tzu-ping Chung
January 22, 2014
Tweet
Share
More Decks by Tzu-ping Chung
See All by Tzu-ping Chung
Let’s fix extras in Core Metadata 3.0
uranusjr
0
130
Python Packaging: Why Don’t You Just…?
uranusjr
1
140
這樣的開發環境沒問題嗎?
uranusjr
9
2.1k
Django After Web 2.0
uranusjr
3
1.6k
We Store Cheese in A Warehouse
uranusjr
1
360
The Python You Don’t Know
uranusjr
16
2.3k
Python and Asynchrony
uranusjr
0
270
Graphics on Raspberry Pi with Qt 5
uranusjr
0
96k
You Might Not Want Async
uranusjr
6
2.4k
Other Decks in Programming
See All in Programming
監視せなあかんし、五大紙だけにオオカミってな🐺🐺🐺🐺🐺
sadnessojisan
2
1.5k
%q is for Quine
koic
0
410
Cloudflare WorkersでGoを動かすライブラリを作っている話
syumai
1
320
Remote SSHで行うVS Codeリモートホスト開発とトラブルシューティング
smt7174
1
470
Micro Frontends with Module Federation @MicroFrontend Summit 2023
manfredsteyer
PRO
0
560
Hatena Engineer Seminar #23「新卒研修で気軽に『ありがとう』を伝え合える Slack アプリを開発した話」
slashnephy
0
300
量子コンピュータ時代のプログラミングセミナー / 20221222_Amplify_seminar _route_optimization
fixstars
0
250
Step Functions Distributed Map を使ってみた
codemountains
0
110
AWSとCPUのムフフな関係
cmdemura
0
470
ペパカレで入社した私が感じた2つのギャップと向き合い方
kosuke_ito
0
280
LIFFで動く割り勘アプリTATEKAをリリースしてみた話
inoue2002
0
250
Hasura の Relationship と権限管理
karszawa
0
170
Featured
See All Featured
XXLCSS - How to scale CSS and keep your sanity
sugarenia
236
1.1M
ReactJS: Keep Simple. Everything can be a component!
pedronauck
657
120k
Robots, Beer and Maslow
schacon
154
7.3k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
24
4.5k
Keith and Marios Guide to Fast Websites
keithpitt
407
21k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
182
15k
Designing the Hi-DPI Web
ddemaree
273
32k
Building Adaptive Systems
keathley
27
1.3k
The Pragmatic Product Professional
lauravandoore
21
3.4k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
38
3.6k
Music & Morning Musume
bryan
37
4.6k
Bootstrapping a Software Product
garrettdimon
299
110k
Transcript
࣌ࠩॄኄత࠷౼Ԁྃ
Timezone in Websites • Totally broken in all possible ways
• Two negatives make a positive! • But if you (by chance) get some of them right…
Databases • All databases do not support timezones • PostgreSQL:
yes • SQLite: no • MySQL: sometimes • Django stores everything in UTC
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 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
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
pytz • The Olson Timezone Database • pip install pytz
• taipei = pytz.timezone(‘Asia/Taipei') • Django depends on this for timezone support http://pytz.sourceforge.net
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
>>> 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
HTTP • HTTP header doesn’t have a “Accept-Timezone” field •
You need to manually set a request’s timezone if you need timezone support
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()
{# "value" is a date/time object #}! ! {% load
tz %}! ! {# Neither of these respect USE_TZ #}! {% localtime on %}! {{ value }}! {% endlocaltime %}! ! {% localtime off %}! {{ value }}! {% endlocaltime %}
{% load tz %}! ! {% timezone "Europe/Paris" %}! Paris
time: {{ value }}! {% endtimezone %}! ! {% get_current_timezone as timezone %}! ! {{ value|localtime }}! ! {{ value|utc }}! ! {{ value|timezone:"Europe/Paris" }}
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)
Modern Technology • HTML5 Geolocation API • Client-side processing •
Still not reliable
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