-
Notifications
You must be signed in to change notification settings - Fork 0
/
outline.txt
110 lines (86 loc) · 3.3 KB
/
outline.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# Theory: Dates and times for humans
- Times are pretty straightforward
Cut up the day into regular intervals
will never change
- Dates are more complicated
Cut up the year into irregular intervals
But the system doesn’t change ... any more
- Time zones are really complicated and change all the time
Time zones
map
- Defined in terms of Universal Coordinated Time, UTC for short
Very similar to historical Greenwich Mean Time
- Today, Calgary is UTC-0600
- At 2:00 a.m. on November 5, switches to UTC-0700
- Defined by local laws, can change with little or no notice
Local rules changed in 2007
- Egypt 2014: Daylight savings transitions in May, June, July, and September
timestamps/datetimes are meaningless unless you know the time zone,
12 o’clock ... where?
and dates can be ambiguous too unless you know the time zone
not much overlap between the workday for Australia and Vancouver
# Theory: Dates and times for computers
- Pick any arbitrary point in time
- Skip time zones: computers don’t care when the sunrise is
- Turns everything into simple arithmetic
- Number of seconds since the beginning of January 1, 1970, UTC.
# Common computations
- What time is it now?
humans are fine with rough answers: around 6:30
most computer applications need precise results
e.g., analyzing latency inside a web server
- What time is it in ___ ?
- What time is it here when it’s ___ in ___ ?
for these two, need a database of time zones
- What does the date 9/10/11 mean?
input formatting
human formats can be very ambiguous
computer formats tend to be more rigidly defined
- How long ago did ___ happen?
output formatting
- Remind me when ___ is about to happen
perhaps “in one month”
# Python’s standard library
Doesn’t know about time zones!
time.strftime("%s", time.gmtime())
'1506224887'
>>> datetime.datetime.utcnow().strftime("%s")
'1506221386'
>>> datetime.datetime.now().strftime("%s")
'1506199791'
second is actually the UTC time
Python’s `0 = 'banana'` moment
mysql> SELECT 0 = 'banana';
+--------------+
| 0 = 'banana' |
+--------------+
| 1 |
+--------------+
1 row in set, 1 warning (0.00 sec)
Don’t use it.
MySQL banana
# Pendulum
“Python datetimes made easy”
At this point, can go through sample operations one by one
automatically uses reliable time zone database
formatting: ISO8601 is actually a family of formats
# Random advice
- ISO8601 format has some pretty nice properties: unambiguous, sortable
- If the clock is likely to be in sync, store past events as UTC offsets
- Store most future human-entered events as local time + timezone name,
because the timezone definition might change
# What we haven’t covered:
- Pendulum may or may not cover these; you’re on your own here
- Other calendars
macos-calendar-list.png
- Localization
In theory, setting your computer’s locale 1/2 or 2/1
In practice, you should override it to YYYY-MM-DD
But Friday, January 15, 2016
- Leap seconds
GPS satellites launched in 80s, require up-to-date leap second list
# Conclusion
There are many advanced date, time, and time zone topics
Pendulum handles common date, time, and time zone computations
in an easier and safer way
than Python’s built-in libraries.