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
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
210
Let’s fix extras in Core Metadata 3.0
uranusjr
0
680
Python Packaging: Why Don’t You Just…?
uranusjr
1
280
這樣的開發環境沒問題嗎?
uranusjr
9
2.7k
Django After Web 2.0
uranusjr
3
2.3k
We Store Cheese in A Warehouse
uranusjr
1
500
The Python You Don’t Know
uranusjr
17
3.3k
Python and Asynchrony
uranusjr
0
440
Graphics on Raspberry Pi with Qt 5
uranusjr
0
96k
Other Decks in Programming
See All in Programming
Welcome to the "Parametricity" 🏙️ − Generic だけど Specific な世界 −
guvalif
PRO
1
180
分散システム、なんですぐ死んでしまうん?耐障害性を高めたいあなたのためのレジリエンスパターン入門
mshibuya
7
6.9k
なぜ関数型プログラミングで「型」と「証明」が語られるのか #fp_matsuri
kajitack
3
1k
Built Our Own Background Agent at LayerX #aidevex_findy
layerx
PRO
8
3.4k
광주소프트웨어마이스터고등학교 DevFest 특강 - 바이브 코딩 시대에서 주니어 개발자로 살아남는 방법
utilforever
1
150
Hatena Engineer Seminar #37「言語モデルの活用に関する研究」
slashnephy
0
550
AIキャラアプリkaiwaの低遅延音声通話基盤をどう作ったか - AWS Gravitonで支える低遅延・低コストAI Agent基盤
mogamit
0
190
ここ半年くらいでAIに作らせたR用ツール
eitsupi
0
240
Laravelで学ぶ Webアプリケーションチューニング入門/web_application_tuning_101
hanhan1978
4
1.3k
【やさしく解説 設計編 #0】DDDのコード、読めるのに分からない人へ
panda728
PRO
2
280
信頼性について考えてみる(SRE NEXT 2026 miniLT)
hayama17
0
210
音楽のための関数型プログラミング言語mimiumにおける多段階計算の活用
tomoyanonymous
1
360
Featured
See All Featured
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
11
980
Avoiding the “Bad Training, Faster” Trap in the Age of AI
tmiket
0
200
Deep Space Network (abreviated)
tonyrice
0
230
Gemini Prompt Engineering: Practical Techniques for Tangible AI Outcomes
mfonobong
2
470
The World Runs on Bad Software
bkeepers
PRO
72
12k
Site-Speed That Sticks
csswizardry
13
1.3k
Breaking role norms: Why Content Design is so much more than writing copy - Taylor Woolridge
uxyall
0
350
職位にかかわらず全員がリーダーシップを発揮するチーム作り / Building a team where everyone can demonstrate leadership regardless of position
madoxten
64
56k
Java REST API Framework Comparison - PWX 2021
mraible
34
9.5k
How to optimise 3,500 product descriptions for ecommerce in one day using ChatGPT
katarinadahlin
PRO
1
3.7k
AI in Enterprises - Java and Open Source to the Rescue
ivargrimstad
0
1.4k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
31
3.3k
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