/
index.html
743 lines (610 loc) · 18.8 KB
/
index.html
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
<!DOCTYPE html>
<!--
Google HTML5 slide template
Authors: Luke Mahé (code)
Marcin Wichary (code and design)
Dominic Mazzoni (browser compatibility)
Charles Chen (ChromeVox support)
URL: http://code.google.com/p/html5slides/
-->
<html>
<head>
<title>A Date with Perl</title>
<meta charset='utf-8'>
<script src='slides.js'></script>
</head>
<style>
/* Your individual styles here, or just use inline styles if that’s
what you want. */
article {
font-family: Arial;
}
h3 {
font-size: 42px !important;
}
.bigger {
font-size: 140%;
}
ul ul:first-child {
margin-top: .5em;
}
.wrong {
color: red;
}
div.alternate-title {
font-size: 80%;
font-weight: bold;
margin-top: 0.5em;
}
p.license {
font-size: 50%;
position: fixed;
bottom: 4em;
}
</style>
<body style='display: none'>
<section class='slides layout-regular template-default'>
<!-- Your slides (<article>s) go here. Delete or comment out the
slides below. -->
<article>
<h1>A Date with Perl</h1>
<p>
Dave Rolsky
<br />
autarch@urth.org
<br />
IRC: autarch
<br />
June 24, 2014
<br />
YAPC::NA 2014
</p>
<p class="license">
Copyright © David Rolsky 2012-2014
<br />
<span xmlns:dc="http://purl.org/dc/elements/1.1/" href="http://purl.org/dc/dcmitype/Text" property="dc:title" rel="dc:type">A Date with Perl</span> by <span xmlns:cc="http://creativecommons.org/ns#" property="cc:attributionName">David Rolsky</span> is licensed under a <a rel="license" href="http://creativecommons.org/licenses/by-sa/3.0/us/">Creative Commons Attribution-Share Alike 3.0 United States License</a>.
</p>
</article>
<article>
<h3>Dates and Times are Insane</h3>
<ul>
<li>Calendars</li>
<li>Time Zones</li>
<li>Daylight Saving Time</li>
<li>Leap Seconds!</li>
</ul>
</article>
<article>
<h3>Do Not Write Your Own Date and Time Manipulation Code!</h3>
<ul>
<li>Do Not Write Your Own Date and Time Manipulation Code!</li>
<li><strong>Do Not Write Your Own Date and Time Manipulation Code!</strong></li>
<li class="bigger"><strong>Do Not Write Your Own Date and Time Manipulation Code!</strong></li>
</ul>
</article>
<article>
<h3>Seriously</h3>
<ul>
<li>Just don't</li>
</ul>
</article>
<article>
<h3>Gregorian Calendar</h3>
<ul>
<li>Based only on earth's revolution around the sun</li>
<li>Current world standard</li>
<li>DateTime.pm == Gregorian</li>
</ul>
</article>
<article>
<h3>Gregorian Calendar for Dummies</h3>
<ul>
<li>365 days in a regular year</li>
<li>366 in a leap year</li>
<li>Begins on January 1, year 1 (0001-01-01)</li>
</ul>
</article>
<article>
<h3>Gregorian Calendar for Dummies</h3>
<ul>
<li>Year 0 == 1 BC(E)</li>
<li>May need to tweak the leap year algorithm around year 3000</li>
<li>Earth's revolution is slowing down</li>
</ul>
</article>
<article>
<h3>Simple Dates</h3>
<pre><code>use DateTime;
my $dt = DateTime->new(
year => 2013,
month => 6,
day => 5,
);
say $dt->date(); # 2013-06-05
say $dt->month_name(); # June
</code></pre>
</article>
<article>
<h3>Les Dates Simples</h3>
<pre><code>use DateTime;
my $dt = DateTime->new(
year => 2013,
month => 6,
day => 5,
locale => 'fr',
);
say $dt->date(); # 2013-06-05
say $dt->month_name(); # Juin
</code></pre>
</article>
<article>
<h3>Other Calendars</h3>
<pre><code>use DateTime;
use DateTime::Calendar::Chinese;
my $dt = DateTime->new(
year => 2013,
month => 6,
day => 5,
);
my $chdt = DateTime::Calendar::Chinese->from_object( object => $dt );
say $chdt->cycle(); # 78
say $chdt->zodiac_animal(); # snake
say $chdt->celestial_stem(), $chdt->terrestrial_branch(); # 癸巳
</code></pre>
</article>
<article>
<h3>Time for Dummies</h3>
<ul>
<li>Day length == 1 rotation of the Earth around its axis</li>
<li>1 day == 24 hours</li>
<li>First hour is hour 0</li>
<li>Last hour is hour 23</li>
<li>1 hour == 60 minutes</li>
<li>1 minute == 60 seconds (almost all of the time)</li>
<li>1 day == 86,400 seconds (more or less)</li>
</ul>
</article>
<article>
<h3>Atomic Clocks</h3>
<ul>
<li>Ties length of second to physicsy stuff</li>
<li>Length of second never changes</li>
<li>TAI is the international atomic time standard</li>
</ul>
</article>
<article>
<h3>Leap Seconds</h3>
<ul>
<li>AKA "The Devil", "A Really, Really Bad Idea"</li>
<li>The earth's rotation is slowing down</li>
<li>The length of a second is not</li>
<li>We need to resync midnight</li>
<li>Bam, leap second announced!</li>
</ul>
</article>
<article>
<h3>UTC</h3>
<ul>
<li>Coordinated Universal Time</li>
<li>Temps Universel Coordineé</li>
<li>UTC = TAI (atomic time) + leap seconds to date (37)</li>
<li>Current world standard</li>
<li>Based on time in Greenwich, England</li>
<li>Time zones are based on an offset from UTC</li>
</ul>
</article>
<article>
<h3>Time Zone Warning</h3>
<ul>
<li>Time zones are political</li>
<li>Change all the time for dumb reasons</li>
<li>Last US change was pointless</li>
</ul>
</article>
<article>
<h3>Time Zone Standards</h3>
<ul>
<li>IANA (née Olson) time zone database is the standard</li>
<li><a href="http://www.iana.org/time-zones">http://www.iana.org/time-zones</a></li>
<li>An open source data/software project</li>
<li>Microsoft does their own thing (of course)</li>
</ul>
</article>
<article>
<h3>Time Zone 101</h3>
<ul>
<li>An offset in minutes and hours from UTC</li>
<li>Washington, DC is currently at -04:00</li>
<li>Also has a name</li>
<li>Names are (mostly) continent or ocean + major city
<ul>
<li>America/Chicago</li>
<li>Asia/Taipei</li>
<li>Pacific/Fiji</li>
<li>America/Argentina/San_Juan</li>
</ul>
</li>
</ul>
</article>
<article>
<h3>Time Zone 102</h3>
<ul>
<li>A named zone is a collection of rules</li>
<li>Rules define historical and future DST changes</li>
<li>Also define short names like CDT</li>
<li>Short names are not unique!</li>
<li>Only use short names for display</li>
</ul>
</article>
<article>
<h3>Picking Time Zones</h3>
<ul>
<li>The Olson database includes many historical zones</li>
<li>America/Chicago == America/Menominee</li>
<li>Menominee moved from Eastern to Central in 1973</li>
<li>No API for finding current time zones (yet?)</li>
</ul>
</article>
<article>
<h3>What Time is it There?</h3>
<pre><code>use DateTime;
my $dt = DateTime->new(
year => 2013,
month => 6,
day => 5,
hour => 9,
minute => 30,
time_zone => 'America/Chicago',
);
say $dt->datetime(); # 2013-06-05T09:30:00
$dt->set_time_zone('Asia/Taipei');
say $dt->datetime(); # 2013-06-05T22:30:00
</code></pre>
</article>
<article>
<h3>The Floating Time Zone</h3>
<pre><code>use DateTime;
my $dt = DateTime->now(
time_zone => 'floating',
);
</code></pre>
<ul>
<li>No time zone at all</li>
<li>No offset conversion when set to a real zone</li>
<li>No leap seconds</li>
</ul>
</article>
<article>
<h3>The Floating Time Zone</h3>
<pre><code>use DateTime;
my $dt = DateTime->new(
year => 2013,
month => 6,
day => 5,
hour => 9,
minute => 30,
time_zone => 'floating',
);
say $dt->datetime(); # 2013-06-05T09:30:00
$dt->set_time_zone('Asia/Taipei');
say $dt->datetime(); # 2013-06-05T09:30:00
</code></pre>
</article>
<article>
<h3>Epochs and the Unix Epoch</h3>
<ul>
<li>An epoch is a reference point for a calendar's start date & time</li>
<li>The Unix epoch == 1970-01-01T00:00:00 UTC</li>
<li>Unix epoch is counted in seconds</li>
<li>Not really UTC since POSIX says we skip leap seconds</li>
<li>But not really TAI cause NTP is UTC-based</li>
</ul>
</article>
<article>
<h3>Calculating the Epoch</h3>
<pre><code>use DateTime;
my $dt = DateTime->new(
year => 2013,
month => 6,
day => 5,
hour => 9,
minute => 30,
time_zone => 'America/Chicago',
);
say $dt->epoch(); # 1370442600
</code></pre>
</article>
<article>
<h3>The y2.038k problem</h3>
<ul>
<li>The epoch will no longer fit in a 32-bit int</li>
<li>2038-01-19T03:14:07 UTC</li>
<li>A 30-year mortgage in 2013 ends in 2043</li>
<li>As of Perl 5.12, Perl always uses a large-enough epoch</li>
</ul>
</article>
<article>
<h3>DateTime::* Ecosystem</h3>
<ul>
<li>Formatter/parsers</li>
<li>Other calendars</li>
<li>Event and recurrence modules</li>
<li>DateTimeX modules</li>
</ul>
</article>
<article>
<h2>Recommendations and Gotchas</h2>
</article>
<article>
<h3>There's no DateTime::Date Class</h3>
<ul>
<li>If I could do it all over again ...</li>
<li>Use the floating time zone</li>
<li>Use the <code>delta_md()</code> and <code>delta_days</code> methods for math</li>
</ul>
</article>
<article>
<h3>Calculating the Difference Between Two Dates</h3>
<pre><code>my $dt1 = DateTime->new(
year => 2013,
month => 6,
day => 5,
time_zone => 'floating',
);
my $dt2 = DateTime->new(
year => 1973,
month => 12,
day => 6,
time_zone => 'floating',
);
my $duration = $dt1->delta_days($dt2);
say $duration->in_units('days'); # 14426
</code></pre>
</article>
<article>
<h3>DateTime::Duration Has a Terrible API</h3>
<ul>
<li>What moron created this?</li>
<li>Internally it stores months, days, minutes, seconds, and nanoseconds</li>
<li>Externally has <code>years()</code>, <code>months()</code>, <code>weeks()</code>, <code>days()</code>, etc. methods</li>
<li>When you call <code>$duration->days()</code> you get days but not weeks</li>
<li>Use <code>$duration->in_units('days')</code> instead</li>
</ul>
</article>
<article>
<h3>Say What, DateTime::Duration?</h3>
<pre><code>my $dt1 = DateTime->new(
year => 2013,
month => 6,
day => 5,
time_zone => 'floating',
);
my $dt2 = DateTime->new(
year => 1973,
month => 12,
day => 6,
time_zone => 'floating',
);
my $duration = $dt1->delta_days($dt2);
say $duration->days(); # 6 ... WTF?
say $duration->weeks(); # 2060
say $duration->in_units('days'); # 14426
</code></pre>
</article>
<article>
<h2>DateTime Math is Hard<br/>Let's Go Shopping</h2>
</article>
<article>
<h3>How Long is a Month?</h3>
<pre><code>my $dt = ...; # 2009-02-01
$dt->add( days => 28 );
say $dt; # 2009-03-01
$dt->add( days => 28 );
say $dt; # 2009-03-29
</code></pre>
</article>
<article>
<h3>How Long is a Month??</h3>
<pre><code>my $dt = ...; # 2009-01-30
$dt->add( months => 1 );
say $dt; # 2009-03-02
$dt->add( months => 1 );
say $dt; # 2009-04-02
</code></pre>
</article>
<article>
<h3>How Long is a Month??!</h3>
<pre><code>my $dt = ...; # 2009-01-30
$dt->add( months => 1, end_of_month => 'limit' );
say $dt; # 2009-02-28
$dt->add( months => 1 );
say $dt; # 2009-03-28
</code></pre>
<ul>
<li>End of month modes
<ul>
<li>wrap - default for adding months</li>
<li>preserve - default for subtracting months</li>
<li>limit</li>
</ul>
</li>
</ul>
</article>
<article>
<h3>How Long is a Day?</h3>
<pre><code>my $dt = DateTime->new(
year => 2012,
month => 11,
day => 4,
hour => 0,
time_zone => 'America/Chicago',
);
say $dt; # 2012-11-04T00:00:00
$dt->add( hours => 1 );
say $dt; # 2012-11-04T01:00:00
$dt->add( hours => 1 );
say $dt; # 2012-11-04T01:00:00
</code></pre>
</article>
<article>
<h3>How Long is a Day?</h3>
<pre><code>my $dt = DateTime->new(
year => 2012,
month => 3,
day => 11,
hour => 0,
time_zone => 'America/Chicago',
);
say $dt; # 2012-03-11T00:00:00
$dt->add( hours => 1 );
say $dt; # 2012-03-11T01:00:00
$dt->add( hours => 1 );
say $dt; # 2012-03-11T03:00:00
</code></pre>
</article>
<article>
<h3>No 02:00 for You!</h3>
<pre><code>my $dt = DateTime->new(
year => 2012,
month => 3,
day => 10,
hour => 2,
time_zone => 'America/Chicago',
);
say $dt; # 2012-03-11T00:02:00
$dt->add( days => 1 ); # Throws an exception ...
# Invalid local time for date in time zone: America/Chicago
# But this works
$dt->add( hours => 24 );
say $dt; # 2012-03-11T03:00:00
</code></pre>
</article>
<article>
<h3>Math Order Matters</h3>
<pre><code>my $dt = DateTime->new(
year => 2011,
month => 2,
day => 28,
);
$dt->add( months => 1, days => 1 );
say $dt; # 2011-04-01, not 2011-03-29
</code></pre>
<p>Want control? Make separate calls:</p>
<pre><code>$dt->add( months => 1 )->add( days => 1, );
say $dt; # 2011-03-29
</code></pre>
</article>
<article>
<h3>More math gotchas</h3>
<ul>
<li>Math is not always reversible
<ul>
<li>$dt1 - $dt2 = $dur</li>
<li>$dt2 + $dur != $dt1</li>
</ul>
</li>
<li>Math across DST changes is confusing
<ul>
<li>$dst_date - $non_dst_date = ?</li>
<li>Does the duration include the DST change's hour?</li>
</ul>
</li>
<li>Leap years</li>
<li>Leap seconds</li>
</ul>
</article>
<article>
<h3>How to Do Math Safely</h3>
<ul>
<li>Always use <code>add()</code>, <code>delta_days()</code>, <code>subtract()</code>, etc.</li>
<li>Never write something like this:<br/>
<code class="wrong">$dt->set( day => $dt->day() + 1 )</code></li>
<li>Use the floating time zone if you can</li>
<li>Use UTC if you can - UTC has no DST changes</li>
</ul>
</article>
<article>
<h3>Ambiguous Local Times</h3>
<pre><code>my $dt = DateTime->new(
year => 2003,
month => 10,
day => 26,
hour => 1,
minute => 30,
second => 0,
time_zone => 'America/Chicago',
);
</code></pre>
<ul>
<li>Is this standard or DST time?</li>
<li>There is a DST change on 2003-10-26 from 01:59:59 (DST) to 01:00:00 (standard)</li>
<li>DateTime.pm always picks the latest UTC time</li>
</ul>
</article>
<article>
<h3>Storage and Presentation</h3>
<ul>
<li>Store datetimes as floating or UTC whenever possible</li>
<li>Or store them as a datetime + time zone (Pgs's <code>TIMESTAMP WITH TZ</code> type)</li>
<li>Also store the named time zone if the database only stores an offset</li>
<li>Don't store an epoch, store a datetime</li>
<li>Use time zones for presentation to users</li>
<li><bold>Never</bold> just store datetimes in the machine's current local time zone</li>
<li>What happens when you move?</li>
</ul>
</article>
<article>
<h2>(Stupid?) Performance tricks</h2>
</article>
<article>
<h3>My Rules of Optimization</h3>
<ol>
<li>Don't optimize</li>
<li>Don't optimize, I'm serious</li>
<li>Don't optimize without benchmarking first</li>
<li>Don't benchmark without profiling first</li>
<li>See rule #1</li>
</ol>
</article>
<article>
<h3>Cache the time zone object</h3>
<pre><code>my $dt = DateTime->new(
year => 2013,
month => 6,
day => 5,
hour => 9,
minute => 30,
time_zone => 'America/Chicago',
);
</code></pre>
<pre><code>my $tz = DateTime::TimeZone->new( name => 'America/Chicago' );
my $dt = DateTime->new(
year => 2013,
month => 6,
day => 5,
hour => 9,
minute => 30,
time_zone => $tz,
);
</code></pre>
</article>
<article>
<h3>Don't Use a Parser</h3>
<ul>
<li>If your data only comes in one flavor</li>
</ul>
<pre><code>my $dt = DateTime::Format::Foo->parse_datetime($string);</code></pre>
<pre><code>my ( $y, $m, $d ) = $string =~ /^(\d{4})-(\d{2})-(\d{2})/;
my $dt = DateTime->new(
year => $y,
month => $m,
day => $d,
);
</code></pre>
</article>
<article>
<h2>Thank You</h2>
</article>
</section>
</body>
</html>