-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathA Date module. « Python recipes « ActiveState Code.htm
1485 lines (1229 loc) · 89.3 KB
/
A Date module. « Python recipes « ActiveState Code.htm
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
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<!-- saved from url=(0057)http://code.activestate.com/recipes/117215-a-date-module/ -->
<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><script type="text/javascript" src="./A Date module. « Python recipes « ActiveState Code_files/auth015.js"></script><link rel="stylesheet" type="text/css" href="./A Date module. « Python recipes « ActiveState Code_files/widget116.css" media="all">
<title>A Date module. « Python recipes « ActiveState Code</title>
<meta http-equiv="X-UA-Compatible" content="IE=8">
<link rel="stylesheet" href="./A Date module. « Python recipes « ActiveState Code_files/activestyle.css" type="text/css">
<!--[if IE]>
<link rel="stylesheet" href="/static/css/ie.css" type="text/css" />
<![endif]-->
<!--[if IE 7]>
<link rel="stylesheet" href="/static/css/ie7.css" type="text/css" />
<![endif]-->
<!--[if IE 6]>
<link rel="stylesheet" href="/static/css/ie6.css" type="text/css" />
<![endif]-->
<link href="./A Date module. « Python recipes « ActiveState Code_files/prettify.css" type="text/css" rel="stylesheet">
<link href="./A Date module. « Python recipes « ActiveState Code_files/css" rel="stylesheet" type="text/css">
<link href="./A Date module. « Python recipes « ActiveState Code_files/css(1)" rel="stylesheet" type="text/css">
<link rel="alternate" href="http://code.activestate.com/feeds/news/" type="application/atom+xml" title="ActiveState Code News">
<link type="image/x-icon" href="http://code.activestate.com/static/activestyle/img/favicon.ico" rel="shortcut icon">
<script type="text/javascript" async="" src="./A Date module. « Python recipes « ActiveState Code_files/inpage_linkid.js" id="undefined"></script><script type="text/javascript" async="" src="./A Date module. « Python recipes « ActiveState Code_files/ga.js"></script><script src="./A Date module. « Python recipes « ActiveState Code_files/jquery-1.3.2.min.js" type="text/javascript"></script>
<script src="./A Date module. « Python recipes « ActiveState Code_files/jquery-ui-1.7.2.custom.min.js" type="text/javascript"></script>
<script src="./A Date module. « Python recipes « ActiveState Code_files/jquery-form-2.36.js" type="text/javascript"></script>
<link rel="canonical" href="./A Date module. « Python recipes « ActiveState Code_files/A Date module. « Python recipes « ActiveState Code.htm">
<!--[if IE]><style type="text/css"> div.codeblock, pre.prettyprint { overflow: visible; overflow-x: auto; overflow-y: hidden; padding-bottom:15px; } </style><![endif]-->
<!-- Start Google Analytics tracking code -->
<script type="text/javascript"> var _gaq = _gaq || [];
var pluginUrl = '//www.google-analytics.com/plugins/ga/inpage_linkid.js';
_gaq.push(['_require', 'inpage_linkid', pluginUrl]);
_gaq.push(['_setAccount', 'UA-20072-24']); _gaq.push(['_setDomainName', '.activestate.com']); _gaq.push(['_setAllowLinker', true]); _gaq.push(['_setAllowHash', false]); _gaq.push(['_trackPageview']);
(function() { var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async =
true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})(); </script>
<!-- End Google Analytics tracking code -->
</head>
<body>
<div id="as_wrapper">
<ul class="as_skip">
<li><a href="http://code.activestate.com/recipes/117215-a-date-module/#q">Skip to Search</a></li>
<li><a href="http://code.activestate.com/recipes/117215-a-date-module/#site_nav">Skip to Navigation</a></li>
<li><a href="http://code.activestate.com/recipes/117215-a-date-module/#as_content">Skip to Content</a></li>
</ul>
<div id="as_header_wrapper" class="nosearchhi">
<div id="as_header">
<div id="as_navbar" class="as_navbar_short">
<ul id="as_sitesnav">
<li class="as_logo"><a href="http://www.activestate.com/"><img height="18" width="90" src="./A Date module. « Python recipes « ActiveState Code_files/activestate.png" title="ActiveState | The Dynamic Language Experts"></a></li>
<span class="as_navsep"></span>
<li><a href="http://community.activestate.com/">Community</a></li>
<span class="as_navsep">|</span>
<li><b><a href="http://code.activestate.com/">Code</a></b></li>
<span class="as_navsep">|</span>
<li><a href="http://docs.activestate.com/">Docs</a></li>
<span class="as_navsep">|</span>
<li class="as_sitesnav_more"><a onclick="$('#as_sitesnav_downloads').slideToggle(100); return false;" href="http://www.activestate.com/downloads"><u>Downloads</u> <small>▼</small></a>
<ul style="display: none;" id="as_sitesnav_downloads">
<li><a href="http://www.activestate.com/stackato/download_vm" title="Download Stackato">Stackato</a></li>
<li><a href="http://www.activestate.com/activeperl/downloads" title="Download Perl">ActivePerl</a></li>
<li><a href="http://www.activestate.com/activepython/downloads" title="Download Python">ActivePython</a></li>
<li><a href="http://www.activestate.com/activetcl/downloads" title="Download Tcl">ActiveTcl</a></li>
<li><a href="http://www.activestate.com/activeperl-pro-studio/downloads" title="Download ActivePerl Pro Studio">ActivePerl Pro Studio</a></li>
<li><a href="http://www.activestate.com/activetcl-pro-studio/downloads" title="Download ActiveTcl Pro Studio">ActiveTcl Pro Studio</a></li>
<li><a href="http://www.activestate.com/perl-dev-kit/downloads" title="Download Perl Dev Kit">Perl Dev Kit</a></li>
<li><a href="http://www.activestate.com/tcl-dev-kit/downloads" title="Download Tcl Dev Kit">Tcl Dev Kit</a></li>
<li><a href="http://www.activestate.com/komodo-ide/downloads" title="Download Komodo IDE">Komodo IDE</a></li>
</ul>
</li>
<span class="as_navsep">|</span>
<li class="as_sitesnav_more"><a href="http://www.activestate.com/community" onclick="$('#as_sitesnav_more').slideToggle(100); return false;"><u>more</u> <small>▼</small></a>
<ul id="as_sitesnav_more" style="display: none;">
<li><a href="http://lists.activestate.com/">Lists</a></li>
<li><a href="http://bugs.activestate.com/">Bugs</a></li>
<li><a href="http://code.activestate.com/ppm">PPM Index</a></li>
<li><a href="http://code.activestate.com/pypm">PyPM Index</a></li>
</ul>
</li>
</ul>
<script type="text/javascript">
$('body').click(function() { $('#as_sitesnav_more').hide(); $('#as_sitesnav_downloads').hide(); });
</script>
<div id="as_accountnav">
Welcome, guest
<span class="as_navsep">|</span> <a href="https://account.activestate.com/signin/?next=http%3A//code.activestate.com/recipes/117215-a-date-module/">Sign In</a>
<span class="as_navsep">|</span> <a href="https://account.activestate.com/">My Account</a>
<span class="as_navsep">|</span> <a href="https://store.activestate.com/">Store</a>
<span class="as_navsep">|</span> <a href="https://store.activestate.com/cart" class="icon-cart">Cart</a>
</div>
</div>
<div id="as_bannerbar">
<div id="site_logo" class="as_banner_text">
<a class="as_banner_text" href="http://code.activestate.com/">ActiveState Code</a> » <a href="http://code.activestate.com/recipes/" class="as_banner_text">Recipes</a>
</div>
<div id="site_nav">
<form id="search_form" action="http://code.activestate.com/search/recipes/">
<input size="24" id="q" name="q" accesskey="s">
<input type="submit" value="Search">
</form>
</div>
<div style="clear:both"></div>
</div>
</div> <!-- end of as_header -->
<div id="as_sub_banner">
<div id="as_sub_site_nav">
<div id="as_sub_site_links">
<a href="http://code.activestate.com/recipes/add/" title="Add a Recipe">Add a Recipe</a>
<a href="http://code.activestate.com/recipes/langs/">Languages</a>
<a href="http://code.activestate.com/recipes/tags/">Tags</a>
<a href="http://code.activestate.com/recipes/users/">Authors</a>
<a href="http://code.activestate.com/recipes/sets/">Sets</a>
</div>
</div>
</div> <!-- end of as_sub_banner -->
</div> <!-- end of as_header_wrapper -->
<div id="as_middle">
<div id="as_content_wrapper">
<div id="as_content">
<div id="page_header">
<h1>
A Date module.
(Python recipe)
<span class="print_only secondary">by Gonçalo Rodrigues</span>
<div class="print_only secondary" style="margin-top: 10px;">
ActiveState Code (http://code.activestate.com/recipes/117215/)
</div>
</h1>
</div>
<div id="content">
<table class="gutter_table"><tbody><tr>
<td class="gutter">
<div id="recipe_scorevote">
<form action="http://code.activestate.com/recipes/117215-a-date-module/up/" method="POST"><div style="display:none"><input type="hidden" name="csrfmiddlewaretoken" value="8a03f5590aef1e84e35f4ce8633042d1"></div>
<button class=" upvote" title="Vote up" type="submit">▲</button>
</form>
<div class="recipe_score ">2</div>
<form action="http://code.activestate.com/recipes/117215-a-date-module/down/" method="POST"><div style="display:none"><input type="hidden" name="csrfmiddlewaretoken" value="8a03f5590aef1e84e35f4ce8633042d1"></div>
<button class=" downvote" title="Vote down" type="submit">▼</button>
</form>
</div>
</td>
<td>
<div id="description">
<p>This pure Python module defines a class Date and several methods
to deal with it, including conversions to some other formats.</p>
<p>Needs Python 2.2</p>
</div>
<div id="blocks">
<div id="block-0" class="block">
<div class="block-toolbar as_secondary">
<span style="float: right;">Python, 351 lines</span>
<div class="block-toolbar-button"><a href="http://code.activestate.com/recipes/117215-a-date-module/download/1/"><img src="./A Date module. « Python recipes « ActiveState Code_files/bullet_disk.png">Download</a></div>
<div id="clipboard_container" class="block-toolbar-button" style="position: relative; display: inline-block;"><div id="clipboard_button"><img src="./A Date module. « Python recipes « ActiveState Code_files/bullet_go16.png">Copy to clipboard</div><div style="position: absolute; left: 2px; top: 0px; width: 117px; height: 19px; z-index: 99;"><embed id="ZeroClipboardMovie_1" src="/static/zeroclipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="117" height="19" name="ZeroClipboardMovie_1" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=1&width=117&height=19" wmode="transparent"></div></div>
</div>
<div class="codeblock">
<table class="highlighttable"><tbody><tr><td class="linenos"><pre> 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</pre></td><td class="code"><div class="highlight"><pre><span class="sd">"""</span>
<span class="sd">The module defines a class Date and several methods to deal with it, including conversions.</span>
<span class="sd">The "format" of the Date class is as follows: Each instance has three attributes,</span>
<span class="sd">year, month and day, all represented as integers and writable. Although no constraints are</span>
<span class="sd">enforced, the intended range of values is:</span>
<span class="sd">1 <= day <= 31 (more precisely 1 <= day <= NumberDaysMonth(month, year))</span>
<span class="sd">1 <= month <= 12 (1 is January and 12 is December)</span>
<span class="sd">It is up to the client of this class to make sure that all assignments are correct.</span>
<span class="sd">In making conversions with the time module (wether in seconds or in a 9-tuple) local time</span>
<span class="sd">is always used.</span>
<span class="sd">History of changes:</span>
<span class="sd">version 2.0.1:</span>
<span class="sd"> - Added docstring to the module.</span>
<span class="sd"> - Changed implementation of next() and previous() to take advantage of NumberDaysMonth().</span>
<span class="sd">version 2.0: Complete rewrite of the module.</span>
<span class="sd"> - Removed weekday as instance attribute of the class.</span>
<span class="sd"> - Added conversion to and from Julian Day number. Added NumberDaysMonth function. Added</span>
<span class="sd"> __sub__ and __add__. Made the class hashable.</span>
<span class="sd"> - Added some (still insuficient and completely ad-hoc) test code when run as __main__.</span>
<span class="sd">"""</span>
<span class="n">__version__</span> <span class="o">=</span> <span class="mf">2.01</span>
<span class="n">__author__</span> <span class="o">=</span> <span class="s">"G. Rodrigues"</span>
<span class="k">import</span> <span class="nn">time</span>
<span class="c">#Needed for conversion to COM dates.</span>
<span class="k">import</span> <span class="nn">pythoncom</span>
<span class="k">def</span> <span class="nf">IsLeapYear</span><span class="p">(</span><span class="n">year</span><span class="p">):</span>
<span class="sd">"""Returns 1 if year is a leap year, zero otherwise."""</span>
<span class="k">if</span> <span class="n">year</span><span class="o">%</span><span class="mf">4</span> <span class="o">==</span> <span class="mf">0</span><span class="p">:</span>
<span class="k">if</span> <span class="n">year</span><span class="o">%</span><span class="mf">100</span> <span class="o">==</span> <span class="mf">0</span><span class="p">:</span>
<span class="k">if</span> <span class="n">year</span><span class="o">%</span><span class="mf">400</span> <span class="o">==</span> <span class="mf">0</span><span class="p">:</span>
<span class="k">return</span> <span class="mf">1</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">return</span> <span class="mf">0</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">return</span> <span class="mf">1</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">return</span> <span class="mf">0</span>
<span class="k">def</span> <span class="nf">NumberDaysYear</span><span class="p">(</span><span class="n">year</span><span class="p">):</span>
<span class="sd">"""Returns the number of days in the year."""</span>
<span class="k">return</span> <span class="mf">365</span> <span class="o">+</span> <span class="n">IsLeapYear</span><span class="p">(</span><span class="n">year</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">NumberDaysMonth</span><span class="p">(</span><span class="n">month</span> <span class="o">=</span> <span class="bp">None</span><span class="p">,</span> <span class="n">year</span> <span class="o">=</span> <span class="bp">None</span><span class="p">):</span>
<span class="sd">"""Returns the number of days in the month.</span>
<span class="sd"> If any of the arguments is missing (month or year) the current month/year is assumed."""</span>
<span class="k">if</span> <span class="n">month</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
<span class="n">m</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">localtime</span><span class="p">()[</span><span class="mf">1</span><span class="p">]</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">m</span> <span class="o">=</span> <span class="n">month</span>
<span class="k">if</span> <span class="n">year</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
<span class="n">y</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">localtime</span><span class="p">()[</span><span class="mf">0</span><span class="p">]</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">y</span> <span class="o">=</span> <span class="n">year</span>
<span class="k">if</span> <span class="n">m</span> <span class="o">==</span> <span class="mf">2</span><span class="p">:</span>
<span class="k">if</span> <span class="n">IsLeapYear</span><span class="p">(</span><span class="n">y</span><span class="p">):</span>
<span class="k">return</span> <span class="mf">29</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">return</span> <span class="mf">28</span>
<span class="k">elif</span> <span class="n">m</span> <span class="ow">in</span> <span class="p">(</span><span class="mf">1</span><span class="p">,</span> <span class="mf">3</span><span class="p">,</span> <span class="mf">5</span><span class="p">,</span> <span class="mf">7</span><span class="p">,</span> <span class="mf">8</span><span class="p">,</span> <span class="mf">10</span><span class="p">,</span> <span class="mf">12</span><span class="p">):</span>
<span class="k">return</span> <span class="mf">31</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">return</span> <span class="mf">30</span>
<span class="k">class</span> <span class="nc">Date</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="sd">"""The Date class."""</span>
<span class="n">Weekdays</span> <span class="o">=</span> <span class="p">[</span><span class="s">"Monday"</span><span class="p">,</span>
<span class="s">"Tuesday"</span><span class="p">,</span>
<span class="s">"Wednesday"</span><span class="p">,</span>
<span class="s">"Thursday"</span><span class="p">,</span>
<span class="s">"Friday"</span><span class="p">,</span>
<span class="s">"Saturday"</span><span class="p">,</span>
<span class="s">"Sunday"</span><span class="p">]</span>
<span class="n">Months</span> <span class="o">=</span> <span class="p">[</span><span class="s">"January"</span><span class="p">,</span>
<span class="s">"February"</span><span class="p">,</span>
<span class="s">"March"</span><span class="p">,</span>
<span class="s">"April"</span><span class="p">,</span>
<span class="s">"May"</span><span class="p">,</span>
<span class="s">"June"</span><span class="p">,</span>
<span class="s">"July"</span><span class="p">,</span>
<span class="s">"August"</span><span class="p">,</span>
<span class="s">"September"</span><span class="p">,</span>
<span class="s">"October"</span><span class="p">,</span>
<span class="s">"November"</span><span class="p">,</span>
<span class="s">"December"</span><span class="p">]</span>
<span class="c">#The slots in a Date object are constrained to allow more efficient operations.</span>
<span class="n">__slots__</span> <span class="o">=</span> <span class="p">[</span><span class="s">"year"</span><span class="p">,</span> <span class="s">"month"</span><span class="p">,</span> <span class="s">"day"</span><span class="p">]</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">tm</span> <span class="o">=</span> <span class="bp">None</span><span class="p">):</span>
<span class="sd">"""The initializer has an optional argument, time, in the time module format,</span>
<span class="sd"> wether as in seconds since the epoch (Unix time) wether as a tuple (time tuple).</span>
<span class="sd"> If it is not provided, then it returns the current date."""</span>
<span class="k">if</span> <span class="n">tm</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
<span class="n">t</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">localtime</span><span class="p">()</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">tm</span><span class="p">,</span> <span class="nb">int</span><span class="p">):</span>
<span class="n">t</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">localtime</span><span class="p">(</span><span class="n">tm</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">t</span> <span class="o">=</span> <span class="n">tm</span>
<span class="bp">self</span><span class="o">.</span><span class="n">year</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">month</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">day</span> <span class="o">=</span> <span class="n">t</span><span class="p">[:</span><span class="mf">3</span><span class="p">]</span>
<span class="k">def</span> <span class="nf">weekday</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="sd">"""Returns the weekday of the date.</span>
<span class="sd"> The format is as in the time module: Monday is 0 and sunday is 6."""</span>
<span class="n">a</span> <span class="o">=</span> <span class="p">(</span><span class="mf">14</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">month</span><span class="p">)</span><span class="o">//</span><span class="mf">12</span>
<span class="n">y</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">year</span> <span class="o">-</span> <span class="n">a</span>
<span class="n">m</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">month</span> <span class="o">+</span> <span class="mf">12</span><span class="o">*</span><span class="n">a</span> <span class="o">-</span><span class="mf">2</span>
<span class="n">d</span> <span class="o">=</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">day</span> <span class="o">+</span> <span class="n">y</span> <span class="o">+</span> <span class="n">y</span><span class="o">//</span><span class="mf">4</span> <span class="o">-</span> <span class="n">y</span><span class="o">//</span><span class="mf">100</span> <span class="o">+</span> <span class="n">y</span><span class="o">//</span><span class="mf">400</span> <span class="o">+</span> <span class="p">(</span><span class="mf">31</span><span class="o">*</span><span class="n">m</span><span class="o">//</span><span class="mf">12</span><span class="p">))</span><span class="o">%</span><span class="mf">7</span>
<span class="k">if</span> <span class="n">d</span><span class="p">:</span>
<span class="n">ret</span> <span class="o">=</span> <span class="n">d</span> <span class="o">-</span> <span class="mf">1</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">ret</span> <span class="o">=</span> <span class="mf">6</span>
<span class="k">return</span> <span class="n">ret</span>
<span class="k">def</span> <span class="nf">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">return</span> <span class="s">"</span><span class="si">%s</span><span class="s">, </span><span class="si">%d</span><span class="s">-</span><span class="si">%s</span><span class="s">-</span><span class="si">%d</span><span class="s">"</span> <span class="o">%</span> <span class="p">(</span><span class="n">Date</span><span class="o">.</span><span class="n">Weekdays</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">weekday</span><span class="p">()],</span>
<span class="bp">self</span><span class="o">.</span><span class="n">day</span><span class="p">,</span>
<span class="n">Date</span><span class="o">.</span><span class="n">Months</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">month</span> <span class="o">-</span> <span class="mf">1</span><span class="p">],</span>
<span class="bp">self</span><span class="o">.</span><span class="n">year</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">copy</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="sd">"""Deep copy of Date objects."""</span>
<span class="n">ret</span> <span class="o">=</span> <span class="n">Date</span><span class="p">()</span>
<span class="n">ret</span><span class="o">.</span><span class="n">year</span><span class="p">,</span> <span class="n">ret</span><span class="o">.</span><span class="n">month</span><span class="p">,</span> <span class="n">ret</span><span class="o">.</span><span class="n">day</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">year</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">month</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">day</span>
<span class="k">return</span> <span class="n">ret</span>
<span class="c">#The iterator protocol. The iteration is "destructive", like in files.</span>
<span class="k">def</span> <span class="nf">__iter__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">return</span> <span class="bp">self</span>
<span class="k">def</span> <span class="nf">next</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="c">#Last day of the month.</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">day</span> <span class="o">==</span> <span class="n">NumberDaysMonth</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">month</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">year</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">day</span> <span class="o">=</span> <span class="mf">1</span>
<span class="c">#December case.</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">month</span> <span class="o">==</span> <span class="mf">12</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">month</span> <span class="o">=</span> <span class="mf">1</span>
<span class="bp">self</span><span class="o">.</span><span class="n">year</span> <span class="o">+=</span> <span class="mf">1</span>
<span class="k">else</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">month</span> <span class="o">+=</span> <span class="mf">1</span>
<span class="k">else</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">day</span> <span class="o">+=</span> <span class="mf">1</span>
<span class="c">#Extended iterator protocol. One can go backwards.</span>
<span class="k">def</span> <span class="nf">previous</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="c">#First day of the month.</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">day</span> <span class="o">==</span> <span class="mf">1</span><span class="p">:</span>
<span class="c">#January case.</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">month</span> <span class="o">==</span> <span class="mf">1</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">month</span> <span class="o">=</span> <span class="mf">12</span>
<span class="bp">self</span><span class="o">.</span><span class="n">year</span> <span class="o">-=</span> <span class="mf">1</span>
<span class="k">else</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">month</span> <span class="o">-=</span> <span class="mf">1</span>
<span class="bp">self</span><span class="o">.</span><span class="n">day</span> <span class="o">=</span> <span class="n">NumberDaysMonth</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">month</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">year</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">day</span> <span class="o">-=</span> <span class="mf">1</span>
<span class="c">#Comparison methods.</span>
<span class="k">def</span> <span class="nf">__eq__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">date</span><span class="p">):</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">year</span> <span class="o">==</span> <span class="n">date</span><span class="o">.</span><span class="n">year</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">month</span> <span class="o">==</span> <span class="n">date</span><span class="o">.</span><span class="n">month</span> <span class="ow">and</span>\
<span class="bp">self</span><span class="o">.</span><span class="n">day</span> <span class="o">==</span> <span class="n">date</span><span class="o">.</span><span class="n">day</span>
<span class="k">def</span> <span class="nf">__lt__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
<span class="k">return</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">year</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">month</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">day</span><span class="p">)</span> <span class="o"><</span> <span class="p">(</span><span class="n">other</span><span class="o">.</span><span class="n">year</span><span class="p">,</span> <span class="n">other</span><span class="o">.</span><span class="n">month</span><span class="p">,</span> <span class="n">other</span><span class="o">.</span><span class="n">dy</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">__le__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
<span class="k">return</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">year</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">month</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">day</span><span class="p">)</span> <span class="o"><=</span> <span class="p">(</span><span class="n">other</span><span class="o">.</span><span class="n">year</span><span class="p">,</span> <span class="n">other</span><span class="o">.</span><span class="n">month</span><span class="p">,</span> <span class="n">other</span><span class="o">.</span><span class="n">dy</span><span class="p">)</span>
<span class="c">#Dates can be used as keys in dictionaries.</span>
<span class="k">def</span> <span class="nf">__hash__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">return</span> <span class="nb">hash</span><span class="p">((</span><span class="bp">self</span><span class="o">.</span><span class="n">year</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">month</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">day</span><span class="p">))</span>
<span class="c">#Some useful methods.</span>
<span class="k">def</span> <span class="nf">GetYearDay</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="sd">"""Returns the year day of a date."""</span>
<span class="n">ret</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">day</span>
<span class="k">for</span> <span class="n">month</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mf">1</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">month</span><span class="p">):</span>
<span class="n">ret</span> <span class="o">+=</span> <span class="n">NumberDaysMonth</span><span class="p">(</span><span class="n">month</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">year</span><span class="p">)</span>
<span class="k">return</span> <span class="n">ret</span>
<span class="k">def</span> <span class="nf">DaysToEndYear</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="sd">"""Returns the number of days until the end of the year."""</span>
<span class="n">ret</span> <span class="o">=</span> <span class="n">NumberDaysMonth</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">month</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">year</span><span class="p">)</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">day</span>
<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">month</span> <span class="o">+</span> <span class="mf">1</span><span class="p">,</span> <span class="mf">13</span><span class="p">):</span>
<span class="n">ret</span> <span class="o">+=</span> <span class="n">NumberDaysMonth</span><span class="p">(</span><span class="n">i</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">year</span><span class="p">)</span>
<span class="k">return</span> <span class="n">ret</span>
<span class="k">def</span> <span class="nf">GetWeekday</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="sd">"""Returns the weekday of the date in string format."""</span>
<span class="k">return</span> <span class="n">Date</span><span class="o">.</span><span class="n">Weekdays</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">weekday</span><span class="p">()]</span>
<span class="k">def</span> <span class="nf">GetMonth</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="sd">"""Returns the month of the date in string format."""</span>
<span class="k">return</span> <span class="n">Date</span><span class="o">.</span><span class="n">Months</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">month</span> <span class="o">-</span> <span class="mf">1</span><span class="p">]</span>
<span class="k">def</span> <span class="nf">ToJDNumber</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="sd">"""Returns the Julian day number of a date."""</span>
<span class="n">a</span> <span class="o">=</span> <span class="p">(</span><span class="mf">14</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">month</span><span class="p">)</span><span class="o">//</span><span class="mf">12</span>
<span class="n">y</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">year</span> <span class="o">+</span> <span class="mf">4800</span> <span class="o">-</span> <span class="n">a</span>
<span class="n">m</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">month</span> <span class="o">+</span> <span class="mf">12</span><span class="o">*</span><span class="n">a</span> <span class="o">-</span> <span class="mf">3</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">day</span> <span class="o">+</span> <span class="p">((</span><span class="mf">153</span><span class="o">*</span><span class="n">m</span> <span class="o">+</span> <span class="mf">2</span><span class="p">)</span><span class="o">//</span><span class="mf">5</span><span class="p">)</span> <span class="o">+</span> <span class="mf">365</span><span class="o">*</span><span class="n">y</span> <span class="o">+</span> <span class="n">y</span><span class="o">//</span><span class="mf">4</span> <span class="o">-</span> <span class="n">y</span><span class="o">//</span><span class="mf">100</span> <span class="o">+</span> <span class="n">y</span><span class="o">//</span><span class="mf">400</span> <span class="o">-</span> <span class="mf">32045</span>
<span class="c">#Binary operations.</span>
<span class="k">def</span> <span class="nf">__add__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">n</span><span class="p">):</span>
<span class="sd">"""Adds a (signed) number of days to the date."""</span>
<span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="nb">int</span><span class="p">):</span>
<span class="c">#Calculate julian day number and add n.</span>
<span class="n">temp</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">ToJDNumber</span><span class="p">()</span> <span class="o">+</span> <span class="n">n</span>
<span class="c">#Convert back to date format.</span>
<span class="k">return</span> <span class="n">DateFromJDNumber</span><span class="p">(</span><span class="n">temp</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">raise</span> <span class="ne">TypeError</span><span class="p">,</span> <span class="s">"</span><span class="si">%s</span><span class="s"> is not an integer."</span> <span class="o">%</span> <span class="nb">str</span><span class="p">(</span><span class="n">n</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">__sub__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">date</span><span class="p">):</span>
<span class="sd">"""Returns the (signed) difference of days between the dates."""</span>
<span class="c">#If it is an integer defer calculation to the __add__ method.</span>
<span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">date</span><span class="p">,</span> <span class="nb">int</span><span class="p">):</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">__add__</span><span class="p">(</span><span class="o">-</span><span class="n">date</span><span class="p">)</span>
<span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">date</span><span class="p">,</span> <span class="n">Date</span><span class="p">):</span>
<span class="c">#Case: The years are equal.</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">year</span> <span class="o">==</span> <span class="n">date</span><span class="o">.</span><span class="n">year</span><span class="p">:</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">GetYearDay</span><span class="p">()</span> <span class="o">-</span> <span class="n">date</span><span class="o">.</span><span class="n">GetYearDay</span><span class="p">()</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">if</span> <span class="bp">self</span> <span class="o"><</span> <span class="n">date</span><span class="p">:</span>
<span class="n">ret</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">DaysToEndYear</span><span class="p">()</span> <span class="o">+</span> <span class="n">date</span><span class="o">.</span><span class="n">GetYearDay</span><span class="p">()</span>
<span class="k">for</span> <span class="n">year</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">year</span> <span class="o">+</span> <span class="mf">1</span><span class="p">,</span> <span class="n">date</span><span class="o">.</span><span class="n">year</span><span class="p">):</span>
<span class="n">ret</span> <span class="o">+=</span> <span class="n">NumberDaysYear</span><span class="p">(</span><span class="n">year</span><span class="p">)</span>
<span class="k">return</span> <span class="o">-</span><span class="n">ret</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">ret</span> <span class="o">=</span> <span class="n">date</span><span class="o">.</span><span class="n">DaysToEndYear</span><span class="p">()</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">GetYearDay</span><span class="p">()</span>
<span class="k">for</span> <span class="n">year</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">date</span><span class="o">.</span><span class="n">year</span> <span class="o">+</span> <span class="mf">1</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">year</span><span class="p">):</span>
<span class="n">ret</span> <span class="o">+=</span> <span class="n">NumberDaysYear</span><span class="p">(</span><span class="n">year</span><span class="p">)</span>
<span class="k">return</span> <span class="n">ret</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">raise</span> <span class="ne">TypeError</span><span class="p">,</span> <span class="s">"</span><span class="si">%s</span><span class="s"> is neither an integer nor a Date."</span> <span class="o">%</span> <span class="nb">str</span><span class="p">(</span><span class="n">date</span><span class="p">)</span>
<span class="c">#Adding an integer is "commutative".</span>
<span class="k">def</span> <span class="nf">__radd__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">n</span><span class="p">):</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">__add__</span><span class="p">(</span><span class="n">n</span><span class="p">)</span>
<span class="c">#Conversion methods.</span>
<span class="k">def</span> <span class="nf">ToTimeTuple</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="sd">"""Convert a date into a time tuple (time module) corresponding to the</span>
<span class="sd"> same day with the midnight hour."""</span>
<span class="n">ret</span> <span class="o">=</span> <span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">year</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">month</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">day</span><span class="p">]</span>
<span class="n">ret</span><span class="o">.</span><span class="n">extend</span><span class="p">([</span><span class="mf">0</span><span class="p">,</span> <span class="mf">0</span><span class="p">,</span> <span class="mf">0</span><span class="p">])</span>
<span class="n">ret</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">weekday</span><span class="p">())</span>
<span class="n">ret</span><span class="o">.</span><span class="n">extend</span><span class="p">([</span><span class="bp">self</span><span class="o">.</span><span class="n">GetYearDay</span><span class="p">(),</span> <span class="mf">0</span><span class="p">])</span>
<span class="k">return</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">ret</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">ToUnixTime</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="sd">"""Convert a date into Unix time (seconds since the epoch) corresponding</span>
<span class="sd"> to the same day with the midnight hour."""</span>
<span class="k">return</span> <span class="n">time</span><span class="o">.</span><span class="n">mktime</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">ToTimeTuple</span><span class="p">())</span>
<span class="k">def</span> <span class="nf">ToCOMTime</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="sd">"""Convert a date into COM format."""</span>
<span class="k">return</span> <span class="n">pythoncom</span><span class="o">.</span><span class="n">MakeTime</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">ToUnixTime</span><span class="p">())</span>
<span class="c">#More conversion functions.</span>
<span class="k">def</span> <span class="nf">DateFromJDNumber</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
<span class="sd">"""Returns a date corresponding to the given Julian day number."""</span>
<span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="nb">int</span><span class="p">):</span>
<span class="k">raise</span> <span class="ne">TypeError</span><span class="p">,</span> <span class="s">"</span><span class="si">%s</span><span class="s"> is not an integer."</span> <span class="o">%</span> <span class="nb">str</span><span class="p">(</span><span class="n">n</span><span class="p">)</span>
<span class="n">a</span> <span class="o">=</span> <span class="n">n</span> <span class="o">+</span> <span class="mf">32044</span>
<span class="n">b</span> <span class="o">=</span> <span class="p">(</span><span class="mf">4</span><span class="o">*</span><span class="n">a</span> <span class="o">+</span> <span class="mf">3</span><span class="p">)</span><span class="o">//</span><span class="mf">146097</span>
<span class="n">c</span> <span class="o">=</span> <span class="n">a</span> <span class="o">-</span> <span class="p">(</span><span class="mf">146097</span><span class="o">*</span><span class="n">b</span><span class="p">)</span><span class="o">//</span><span class="mf">4</span>
<span class="n">d</span> <span class="o">=</span> <span class="p">(</span><span class="mf">4</span><span class="o">*</span><span class="n">c</span> <span class="o">+</span> <span class="mf">3</span><span class="p">)</span><span class="o">//</span><span class="mf">1461</span>
<span class="n">e</span> <span class="o">=</span> <span class="n">c</span> <span class="o">-</span> <span class="p">(</span><span class="mf">1461</span><span class="o">*</span><span class="n">d</span><span class="p">)</span><span class="o">//</span><span class="mf">4</span>
<span class="n">m</span> <span class="o">=</span> <span class="p">(</span><span class="mf">5</span><span class="o">*</span><span class="n">e</span> <span class="o">+</span> <span class="mf">2</span><span class="p">)</span><span class="o">//</span><span class="mf">153</span>
<span class="n">ret</span> <span class="o">=</span> <span class="n">Date</span><span class="p">()</span>
<span class="n">ret</span><span class="o">.</span><span class="n">day</span> <span class="o">=</span> <span class="n">e</span> <span class="o">+</span> <span class="mf">1</span> <span class="o">-</span> <span class="p">(</span><span class="mf">153</span><span class="o">*</span><span class="n">m</span> <span class="o">+</span> <span class="mf">2</span><span class="p">)</span><span class="o">//</span><span class="mf">5</span>
<span class="n">ret</span><span class="o">.</span><span class="n">month</span> <span class="o">=</span> <span class="n">m</span> <span class="o">+</span> <span class="mf">3</span> <span class="o">-</span> <span class="mf">12</span><span class="o">*</span><span class="p">(</span><span class="n">m</span><span class="o">//</span><span class="mf">10</span><span class="p">)</span>
<span class="n">ret</span><span class="o">.</span><span class="n">year</span> <span class="o">=</span> <span class="mf">100</span><span class="o">*</span><span class="n">b</span> <span class="o">+</span> <span class="n">d</span> <span class="o">-</span> <span class="mf">4800</span> <span class="o">+</span> <span class="n">m</span><span class="o">/</span><span class="mf">10</span>
<span class="k">return</span> <span class="n">ret</span>
<span class="k">def</span> <span class="nf">DateFromCOM</span><span class="p">(</span><span class="n">t</span><span class="p">):</span>
<span class="sd">"""Converts a COM time directly into the Date format."""</span>
<span class="k">return</span> <span class="n">Date</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">t</span><span class="p">))</span>
<span class="k">def</span> <span class="nf">strpdate</span><span class="p">(</span><span class="n">s</span><span class="p">):</span>
<span class="sd">"""This function reads a string in the standard date representation</span>
<span class="sd"> format and returns a date object."""</span>
<span class="n">ret</span> <span class="o">=</span> <span class="n">Date</span><span class="p">()</span>
<span class="n">temp</span> <span class="o">=</span> <span class="n">s</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">", "</span><span class="p">)</span>
<span class="n">temp</span> <span class="o">=</span> <span class="n">temp</span><span class="p">[</span><span class="mf">1</span><span class="p">]</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">"-"</span><span class="p">)</span>
<span class="n">ret</span><span class="o">.</span><span class="n">year</span><span class="p">,</span> <span class="n">ret</span><span class="o">.</span><span class="n">month</span><span class="p">,</span> <span class="n">ret</span><span class="o">.</span><span class="n">day</span> <span class="o">=</span> <span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">temp</span><span class="p">[</span><span class="mf">2</span><span class="p">]),</span>
<span class="n">Date</span><span class="o">.</span><span class="n">Months</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="n">temp</span><span class="p">[</span><span class="mf">1</span><span class="p">])</span> <span class="o">+</span> <span class="mf">1</span><span class="p">,</span>
<span class="nb">int</span><span class="p">(</span><span class="n">temp</span><span class="p">[</span><span class="mf">0</span><span class="p">]))</span>
<span class="k">return</span> <span class="n">ret</span>
<span class="c">#Some test code.</span>
<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">"__main__"</span><span class="p">:</span>
<span class="c">#Print the days still left in the month.</span>
<span class="n">temp</span> <span class="o">=</span> <span class="n">Date</span><span class="p">()</span>
<span class="n">curr_month</span> <span class="o">=</span> <span class="n">temp</span><span class="o">.</span><span class="n">month</span>
<span class="k">while</span> <span class="n">temp</span><span class="o">.</span><span class="n">month</span> <span class="o">==</span> <span class="n">curr_month</span><span class="p">:</span>
<span class="k">print</span> <span class="n">temp</span>
<span class="n">temp</span><span class="o">.</span><span class="n">next</span><span class="p">()</span>
<span class="k">print</span> <span class="s">"</span><span class="se">\n</span><span class="s">"</span>
<span class="c">#How many days until the end of the year?</span>
<span class="n">temp</span> <span class="o">=</span> <span class="n">Date</span><span class="p">()</span>
<span class="n">temp</span><span class="o">.</span><span class="n">day</span><span class="p">,</span> <span class="n">temp</span><span class="o">.</span><span class="n">month</span> <span class="o">=</span> <span class="mf">1</span><span class="p">,</span> <span class="mf">1</span>
<span class="n">curr_year</span> <span class="o">=</span> <span class="n">temp</span><span class="o">.</span><span class="n">year</span>
<span class="k">while</span> <span class="n">temp</span><span class="o">.</span><span class="n">year</span> <span class="o">==</span> <span class="n">curr_year</span><span class="p">:</span>
<span class="k">print</span> <span class="s">"</span><span class="si">%s</span><span class="s"> is </span><span class="si">%d</span><span class="s"> days away from the end of the year."</span> <span class="o">%</span> <span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">temp</span><span class="p">),</span>
<span class="n">temp</span><span class="o">.</span><span class="n">DaysToEndYear</span><span class="p">())</span>
<span class="n">temp</span> <span class="o">+=</span> <span class="n">NumberDaysMonth</span><span class="p">(</span><span class="n">temp</span><span class="o">.</span><span class="n">month</span><span class="p">)</span>
<span class="k">print</span> <span class="s">"</span><span class="se">\n</span><span class="s">"</span>
<span class="c">#Playing with __sub__.</span>
<span class="n">temp</span> <span class="o">=</span> <span class="n">Date</span><span class="p">()</span>
<span class="n">temp_list</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">curr_year</span> <span class="o">=</span> <span class="n">temp</span><span class="o">.</span><span class="n">year</span>
<span class="k">while</span> <span class="n">temp</span><span class="o">.</span><span class="n">year</span> <span class="o">==</span> <span class="n">curr_year</span><span class="p">:</span>
<span class="n">temp_list</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">temp</span><span class="p">)</span>
<span class="n">temp</span> <span class="o">+=</span> <span class="n">NumberDaysMonth</span><span class="p">(</span><span class="n">temp</span><span class="o">.</span><span class="n">month</span><span class="p">)</span>
<span class="k">for</span> <span class="n">elem</span> <span class="ow">in</span> <span class="n">temp_list</span><span class="p">:</span>
<span class="k">print</span> <span class="s">"</span><span class="si">%s</span><span class="s"> differs </span><span class="si">%d</span><span class="s"> days from current date: </span><span class="si">%s</span><span class="s">"</span> <span class="o">%</span> <span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">elem</span><span class="p">),</span>
<span class="n">elem</span> <span class="o">-</span> <span class="n">Date</span><span class="p">(),</span>
<span class="nb">str</span><span class="p">(</span><span class="n">Date</span><span class="p">()))</span>
<span class="k">print</span> <span class="s">"</span><span class="se">\n</span><span class="s">"</span>
<span class="c">#Swapping arguments works?</span>
<span class="k">print</span> <span class="mf">23</span> <span class="o">+</span> <span class="n">Date</span><span class="p">()</span>
</pre></div>
</td></tr></tbody></table>
</div>
</div>
<div id="block-1" class="block">
<div class="proseblock">
<p>This module should be sufficient for most needs when handling dates.
On the other hand, none of the calendar oddities (e.g. no zero year,
missing dates and other such whatnots) are covered, so, if you need
to handle dates before the definitive adoption of the gregorian
calendar (the adoption varied from country to country) then I
recommend the mxDateTime package.</p>
</div>
</div>
</div>
<div id="recipe_tools">
<!-- AddThis Button BEGIN -->
<script type="text/javascript">var addthis_config = {services_compact: 'delicious,digg,reddit,twitter,email,favorites,more', username: 'xa-4b4f86a749591ece', data_use_flash: false};</script>
<a class="addthis_button"><img width="125" height="16" border="0" alt="Share" src="./A Date module. « Python recipes « ActiveState Code_files/lg-share-en.gif"></a>
<script type="text/javascript" src="./A Date module. « Python recipes « ActiveState Code_files/addthis_widget.js"></script><div id="_atssh" style="visibility: hidden; height: 1px; width: 1px; position: absolute; z-index: 100000;"><iframe id="_atssh160" title="AddThis utility frame" style="height: 1px; width: 1px; position: absolute; z-index: 100000; border: 0px; left: 0px; top: 0px;" src="./A Date module. « Python recipes « ActiveState Code_files/sh138.htm"></iframe></div><script type="text/javascript" src="./A Date module. « Python recipes « ActiveState Code_files/core104.js"></script>
<!-- AddThis Button END -->
<a href="http://code.activestate.com/help/feedback/?type=flag&subject=Flag%20recipe%20117215&text=Please%20indicate%20**why**%20you%20want%20to%20flag%20that%20recipe."><img src="./A Date module. « Python recipes « ActiveState Code_files/flag.png"></a>
</div>
</td></tr></tbody></table> <!-- end of gutter_table -->
<h4 class="section_header">9 comments</h4>
<div id="comments">
<div class="section comment" id="c1">
<div class="comment_gutter">
<div class="gravatar30">
<img src="./A Date module. « Python recipes « ActiveState Code_files/avatar.php" height="30" width="30">
</div>
</div>
<div class="comment_content">
<div class="comment_header">
<a class="comment_author " href="http://code.activestate.com/recipes/users/325253/">John Machin</a>
<span class="comment_time">11 years, 6 months ago</span>
<span class="comment_actions">
<a title="permalink" href="http://code.activestate.com/recipes/117215-a-date-module/#c1"> #</a>
|
<a title="flag comment as requiring moderator attention" href="http://code.activestate.com/help/feedback/?type=flag&subject=Flag%20comment%201%20of%20recipe%20117215&text=Please%20indicate%20**why**%20you%20want%20to%20flag%20that%20comment.">flag</a>
</span>
</div>
<div class="comment_body">
<p>Try running Pychecker over your code.</p>
</div>
</div>
</div>
<div class="section comment" id="c2">
<div class="comment_gutter">
<div class="gravatar30">
<img src="./A Date module. « Python recipes « ActiveState Code_files/avatar(1).php" height="30" width="30">
</div>
</div>
<div class="comment_content">
<div class="comment_header">
<a class="comment_author " href="http://code.activestate.com/recipes/users/642575/">rhinospam</a>
<span class="comment_time">11 years, 1 month ago</span>
<span class="comment_actions">
<a title="permalink" href="http://code.activestate.com/recipes/117215-a-date-module/#c2"> #</a>
|
<a title="flag comment as requiring moderator attention" href="http://code.activestate.com/help/feedback/?type=flag&subject=Flag%20comment%202%20of%20recipe%20117215&text=Please%20indicate%20**why**%20you%20want%20to%20flag%20that%20comment.">flag</a>
</span>
</div>
<div class="comment_body">
<p><strong>Bug in __lt__ and __le__.</strong> There is a hidous bug in the __lt__ and __le__ operators
The fix should be something like the following.</p>
<pre class="prettyprint"><code><span class="pln"> </span><span class="kwd">def</span><span class="pln"> __lt__</span><span class="pun">(</span><span class="kwd">self</span><span class="pun">,</span><span class="pln"> date</span><span class="pun">):</span><span class="pln"><br> </span><span class="kwd">if</span><span class="pln"> </span><span class="kwd">self</span><span class="pun">.</span><span class="pln">year </span><span class="typ">There</span><span class="pln"> </span><span class="kwd">is</span><span class="pln"> a hidous bug </span><span class="kwd">in</span><span class="pln"> the __lt__ </span><span class="kwd">and</span><span class="pln"> __le__ operators<br></span><span class="typ">The</span><span class="pln"> fix should be something like the following</span><span class="pun">.</span><span class="pln"><br><br></span><span class="str"><pre></span><span class="pln"><br> </span><span class="kwd">def</span><span class="pln"> __lt__</span><span class="pun">(</span><span class="kwd">self</span><span class="pun">,</span><span class="pln"> date</span><span class="pun">):</span><span class="pln"><br> </span><span class="kwd">if</span><span class="pln"> </span><span class="kwd">self</span><span class="pun">.</span><span class="pln">year<br></span></code></pre>
<p></pre></p>
</div>
</div>
</div>
<div class="section comment" id="c3">