Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Timezone support in Django
Search
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
Tzu-ping Chung
January 22, 2014
Programming
180
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
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
220
Let’s fix extras in Core Metadata 3.0
uranusjr
0
700
Python Packaging: Why Don’t You Just…?
uranusjr
1
290
這樣的開發環境沒問題嗎?
uranusjr
9
2.7k
Django After Web 2.0
uranusjr
3
2.3k
We Store Cheese in A Warehouse
uranusjr
1
510
The Python You Don’t Know
uranusjr
17
3.4k
Python and Asynchrony
uranusjr
0
440
Graphics on Raspberry Pi with Qt 5
uranusjr
0
96k
Other Decks in Programming
See All in Programming
Building an Out-of-Order CPU
latte72
1
780
wkhtmltopdfの次どうするか問題2026
willnet
2
1.2k
AI Agent時代のリアーキテクチャ戦略と実践
hokaccha
9
4.7k
AIは賢い。でも実行環境は? CLIおじさんがAI時代に伝えたいこと ~ CLIおじさんがAI時代に伝えたいこと ~
curekoshimizu
1
240
Snowflakeで業務アプリを作ろう。 Snowflakeのアプリ機能解説&実践ガイド
ayumu_yamaguchi
1
280
C#の現在地 進化の歴史と、AI時代の.NET Everywhere
neuecc
4
2.1k
【高い買い物LT会】初任給で話題の国産フィジカルAIを買った話
akagami
PRO
0
160
JPUG勉強会 OSSデータベースの内部構造を理解しよう(第2回)
oga5
0
250
仕様駆動開発による爆速プロダクト開発 / Bakusoku Spec Driven Development
kobakei
0
120
GKE で Pod の見方を変えたら、スケールアウト時の挙動を真に捉えられた話
stkk
0
130
Jetpack Compose メカニズム
skydoves
0
440
WebAssembly in Android Apps 〜 WASMはJNIの夢を見るか
keiji
1
110
Featured
See All Featured
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
9
1.6k
End of SEO as We Know It (SMX Advanced Version)
ipullrank
3
4.4k
Discover your Explorer Soul
emna__ayadi
2
1.3k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
508
140k
Building Applications with DynamoDB
mza
96
7.2k
Leveraging Curiosity to Care for An Aging Population
cassininazir
1
490
Reality Check: Gamification 10 Years Later
codingconduct
0
2.3k
Faster Mobile Websites
deanohume
310
32k
Fireside Chat
paigeccino
43
4k
Bash Introduction
62gerente
615
220k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
25
2.1k
Building Flexible Design Systems
yeseniaperezcruz
330
41k
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