Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Timezone support in Django
Search
Tzu-ping Chung
January 22, 2014
Programming
170
0
Share
Timezone support in Django
On Python datetime and timezone support in Django. Not a pretty sight.
Tzu-ping Chung
January 22, 2014
More Decks by Tzu-ping Chung
See All by Tzu-ping Chung
Datasets: What it is, and how it was made
uranusjr
0
200
Let’s fix extras in Core Metadata 3.0
uranusjr
0
650
Python Packaging: Why Don’t You Just…?
uranusjr
1
280
這樣的開發環境沒問題嗎?
uranusjr
9
2.7k
Django After Web 2.0
uranusjr
3
2.2k
We Store Cheese in A Warehouse
uranusjr
1
500
The Python You Don’t Know
uranusjr
17
3.3k
Python and Asynchrony
uranusjr
0
430
Graphics on Raspberry Pi with Qt 5
uranusjr
0
96k
Other Decks in Programming
See All in Programming
Make SRE Operations Easier with Azure SRE Agent
kkamegawa
0
3.1k
決定論的オーケストレーションの設計と実装 / Design and Implementation of Deterministic Orchestration
nrslib
2
380
Why Laravel apps break—Mastering the fundamentals to keep them maintainable
kentaroutakeda
1
330
JavaDoc 再入門
nagise
0
250
CLIであることを活かしたGitHub Copilot CLI活用術 / GitHub Copilot CLI Pro Tips & Tricks
nao_mk2
1
1.2k
Inspired By RubyKaigi (EN)
atzzcokek
0
500
AIとRubyの静的型付け
ukin0k0
0
510
AI時代の仕事技芸論 — ソフトウェア開発で「遊ぶように働く」職人的熟達のすすめ
kuranuki
1
600
サーバーレスで作る、動画データ管理基盤
oyasumipants
0
340
運用エージェントは "作る" から "育てる" へ - 記憶と自己進化の3層設計パターン / self-evolving-agents-three-layer-agent-design
gawa
12
3.4k
AIエージェントの隔離技術の徹底比較
kawayu
0
450
Lemonade + Foundry Toolkit でお手軽アプリ開発
seosoft
1
270
Featured
See All Featured
Navigating Weather and Climate Data
rabernat
0
210
Un-Boring Meetings
codingconduct
0
300
Digital Ethics as a Driver of Design Innovation
axbom
PRO
1
300
VelocityConf: Rendering Performance Case Studies
addyosmani
333
25k
Making Projects Easy
brettharned
120
6.7k
Automating Front-end Workflow
addyosmani
1370
210k
SEO Brein meetup: CTRL+C is not how to scale international SEO
lindahogenes
1
2.7k
Art, The Web, and Tiny UX
lynnandtonic
304
22k
jQuery: Nuts, Bolts and Bling
dougneiner
66
8.5k
The State of eCommerce SEO: How to Win in Today's Products SERPs - #SEOweek
aleyda
2
11k
Leo the Paperboy
mayatellez
7
1.8k
Designing Experiences People Love
moore
143
24k
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