$30 off During Our Annual Pro Sale. View Details »
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
0
150
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
Datasets: What it is, and how it was made
uranusjr
0
120
Let’s fix extras in Core Metadata 3.0
uranusjr
0
470
Python Packaging: Why Don’t You Just…?
uranusjr
1
210
這樣的開發環境沒問題嗎?
uranusjr
9
2.5k
Django After Web 2.0
uranusjr
3
2k
We Store Cheese in A Warehouse
uranusjr
1
440
The Python You Don’t Know
uranusjr
17
3k
Python and Asynchrony
uranusjr
0
350
Graphics on Raspberry Pi with Qt 5
uranusjr
0
96k
Other Decks in Programming
See All in Programming
見せてあげますよ、「本物のLaravel批判」ってやつを。
77web
7
7.9k
Modular Monolith Monorepo ~シンプルさを保ちながらmonorepoのメリットを最大化する~
yuisakamoto
10
3.7k
かんたんデザイン編集やってみた~「完全に理解した」までの道のり~
morit4ryo
1
110
Java 23の概要とJava Web Frameworkの現状 / Java 23 and Java web framework
kishida
2
370
eBPF Deep Dive: Architecture and Safety Mechanisms
takehaya
12
1.1k
AWS Lambdaから始まった Serverlessの「熱」とキャリアパス / It started with AWS Lambda Serverless “fever” and career path
seike460
PRO
1
410
MoQとか勉強会#2 発表資料
yuki_uchida
1
120
タクシーアプリ『GO』のリアルタイムデータ分析基盤における機械学習サービスの活用
mot_techtalk
6
2.3k
Laravel や Symfony で手っ取り早く OpenAPI のドキュメントを作成する
azuki
2
140
Micro Frontends Unmasked Opportunities, Challenges, Alternatives
manfredsteyer
PRO
0
190
我々のデザインシステムは Chakra v3 にアップデートします
shunya078
2
1.5k
第5回日本眼科AI学会総会_AIコンテスト_3位解法
neilsaw
0
100
Featured
See All Featured
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
44
2.2k
What’s in a name? Adding method to the madness
productmarketing
PRO
22
3.2k
Facilitating Awesome Meetings
lara
50
6.1k
Git: the NoSQL Database
bkeepers
PRO
427
64k
Making Projects Easy
brettharned
115
5.9k
Bash Introduction
62gerente
608
210k
Measuring & Analyzing Core Web Vitals
bluesmoon
4
150
Docker and Python
trallard
40
3.1k
What's new in Ruby 2.0
geeforr
343
31k
Building a Modern Day E-commerce SEO Strategy
aleyda
38
7k
YesSQL, Process and Tooling at Scale
rocio
169
14k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
330
21k
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