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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
|
% XXX what order should the types be discussed in?
\section{\module{datetime} ---
Basic date and time types}
\declaremodule{builtin}{datetime}
\modulesynopsis{Basic date and time types.}
\moduleauthor{Tim Peters}{tim@zope.com}
\sectionauthor{Tim Peters}{tim@zope.com}
\sectionauthor{A.M. Kuchling}{amk@amk.ca}
\sectionauthor{Raymond D. Hettinger}{python@rcn.com}
\versionadded{2.3}
\newcommand{\Naive}{Na\"ive}
\newcommand{\naive}{na\"ive}
The \module{datetime} module supplies classes for manipulating dates
and times in both simple and complex ways. While date and time
arithmetic is supported, the focus of the implementation is on
efficient field extraction, for output formatting and manipulation.
There are two kinds of date and time objects: ``\naive'' and ``aware''.
This distinction refers to whether the object has any notion of time
zone, daylight savings time, or other kind of algorithmic or political
time adjustment. Whether a {\naive} \class{datetime} object represents
Coordinated Universal Time (UTC), local time, or time in some other
timezone is purely up to the program, just like it's up to the program
whether a particular number represents meters, miles, or mass. {\Naive}
\class{datetime} objects are easy to understand and to work with, at
the cost of ignoring some aspects of reality.
For applications requiring more, ``aware'' \class{datetime} subclasses add an
optional time zone information object to the basic {\naive} classes.
These \class{tzinfo} objects capture information about the offset from
UTC time, the time zone name, and whether Daylight Savings Time is in
effect. Note that no concrete \class{tzinfo} classes are supplied by
the \module{datetime} module. Instead, they provide a framework for
incorporating the level of detail an app may require. The rules for
time adjustment across the world are more political than rational, and
there is no standard suitable for every app.
The \module{datetime} module exports the following constants:
\begin{datadesc}{MINYEAR}
The smallest year number allowed in a \class{date},
\class{datetime}, or \class{datetimetz} object. \constant{MINYEAR}
is \code{1}.
\end{datadesc}
\begin{datadesc}{MAXYEAR}
The largest year number allowed in a \class{date}, \class{datetime},
or \class{datetimetz} object. \constant{MAXYEAR} is \code{9999}.
\end{datadesc}
\begin{seealso}
\seemodule{calendar}{General calandar related functions.}
\seemodule{time}{Time access and conversions.}
\end{seealso}
\subsection{Available Types}
\begin{classdesc*}{date}
An idealized {\naive} date, assuming the current Gregorian calendar
always was, and always will be, in effect.
Attributes: \member{year}, \member{month}, and \member{day}.
\end{classdesc*}
\begin{classdesc*}{time}
An idealized {\naive} time, independent of any particular day, assuming
that every day has exactly 24*60*60 seconds (there is no notion
of "leap seconds" here).
Attributes: \member{hour}, \member{minute}, \member{second}, and
\member{microsecond}
\end{classdesc*}
\begin{classdesc*}{datetime}
A combination of a {\naive} date and a {\naive} time.
Attributes: \member{year}, \member{month}, \member{day},
\member{hour}, \member{minute}, \member{second},
and \member{microsecond}.
\end{classdesc*}
\begin{classdesc*}{timedelta}
A duration, expressing the difference between two \class{date},
\class{time}, or \class{datetime} instances, to microsecond
resolution.
\end{classdesc*}
\begin{classdesc*}{tzinfo}
An abstract base class for time zone information objects. These
are used by the \class{datetimetz} and \class{timetz} classes to
provided a customizable notion of time adjustment (for example, to
account for time zone and/or daylight savings time).
\end{classdesc*}
\begin{classdesc*}{timetz}
An aware subclass of \class{time}, supporting a customizable notion of
time adjustment.
\end{classdesc*}
\begin{classdesc*}{datetimetz}
An aware subclass of \class{datetime}, supporting a customizable notion of
time adjustment.
\end{classdesc*}
Objects of these types are immutable.
Objects of the \class{date}, \class{datetime}, and \class{time} types
are always \naive.
An object \code{D} of type \class{timetz} or \class{datetimetz} may be
{\naive} or aware. \code{D} is aware if \code{D.tzinfo} is not
\code{None}, and \code{D.tzinfo.utcoffset(D)} does not return
\code{None}. If \code{D.tzinfo} is \code{None}, or if \code{D.tzinfo}
is not \code{None} but \code{D.tzinfo.utcoffset(D)} returns
\code{None}, \code{D} is \naive.
The distinction between {\naive} and aware doesn't apply to
\code{timedelta} objects.
Subclass relationships:
\begin{verbatim}
object
timedelta
tzinfo
time
timetz
date
datetime
datetimetz
\end{verbatim}
\subsection{\class{timedelta} Objects \label{datetime-timedelta}}
\begin{classdesc}{timedelta}{days=0, seconds=0, microseconds=0,
milliseconds=0, minutes=0, hours=0, weeks=0}
A \class{timedelta} object represents a duration, the difference
between two dates or times.
All arguments are optional. Arguments may be ints, longs, or floats,
and may be positive or negative.
Only \var{days}, \var{seconds} and \var{microseconds} are stored
internally. Arguments are converted to those units:
\begin{verbatim}
A millisecond is converted to 1000 microseconds.
A minute is converted to 60 seconds.
An hour is converted to 3600 seconds.
A week is converted to 7 days.
\end{verbatim}
and days, seconds and microseconds are then normalized so that the
representation is unique, with
\begin{itemize}
\item \code{0 <= \var{microseconds} < 1000000}
\item \code{0 <= \var{seconds} < 3600*24} (the number of seconds in one day)
\item \code{-999999999 <= \var{days} <= 999999999}
\end{itemize}
If any argument is a float, and there are fractional microseconds,
the fractional microseconds left over from all arguments are combined
and their sum is rounded to the nearest microsecond. If no
argument is a flost, the conversion and normalization processes
are exact (no information is lost).
If the normalized value of days lies outside the indicated range,
\exception{OverflowError} is raised.
Note that normalization of negative values may be surprising at first.
For example,
\begin{verbatim}
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
\end{verbatim}
\end{classdesc}
Class attributes are:
\begin{tableii}{c|l}{code}{Attribute}{Value}
\lineii{min}{The most negative \class{timedelta} object,
\code{timedelta(-999999999)}}
\lineii{max}{The most positive \class{timedelta} object,
timedelta(days=999999999, hours=23, minutes=59, seconds=59,
microseconds=999999)}
\lineii{resolution}{The smallest possible difference between non-equal
\class{timedelta} objects, \code{timedelta(microseconds=1)}}
\end{tableii}
Note that, because of normalization, timedelta.max > -timedelta.min.
-timedelta.max is not representable as a \class{timedelta} object.
Instance attributes (read-only):
\begin{tableii}{c|l}{code}{Attribute}{Value}
\lineii{days}{Between -999999999 and 999999999 inclusive}
\lineii{seconds}{Between 0 and 86399 inclusive}
\lineii{microseconds}{Between 0 and 999999 inclusive}
\end{tableii}
Supported operations:
\begin{tableiii}{c|l|c}{code}{Operation}{Result}{Notes}
\lineii{t1 = t2 + t3}{Sum of t2 and t3.
Afterwards t1-t2 == t3 and t1-t3 == t2 are true.}{(1)}
\lineii{t1 = t2 - t3}{Difference of t2 and t3. Afterwards t1 = t2 - t3 and
t2 == t1 + t3 are true.}{(1)}
\lineii{t1 = t2 * i or t1 = i * t2}{Delta multiplied by an integer or long.
Afterwards t1 // i == t2 is true, provided i != 0.
In general, t1 * i == t1 * (i-1) + t1 is true.}{(1)}
\lineii{t1 = t2 // i}{The floor is computed and the remainder (if any) is
thrown away.}{(2)}
\end{tableii}
\noindent
Notes:
\begin{description}
\item[(1)]
This is exact, but may overflow.
\item[(2)]
Division by 0 raises \exception{ZeroDivisionError}.
\end{description}
\begin{itemize}
\item
certain additions and subtractions with date, datetime, and datimetz
objects (see below)
\item
+timedelta -> timedelta
Returns a \class{timedelta} object with the same value.
\item
-timedelta -> timedelta
-t is equivalent to timedelta(-t.days, -t.seconds, -t.microseconds),
and to t*-1. This is exact, but may overflow (for example,
-timedelta.max is not representable as a \class{timedelta} object).
\item
\code{abs(timedelta) -> timedelta}:
\code{abs(t)} is equivalent to +t when \code{t.days >= 0}, and to -t when
\code{t.days < 0}. This is exact, and cannot overflow.
\item
comparison of \class{timedelta} to timedelta; the \class{timedelta} representing
the smaller duration is considered to be the smaller timedelta
\item
hash, use as dict key
\item
efficient pickling
\item
in Boolean contexts, a \class{timedelta} object is considered to be true
if and only if it isn't equal to \code{timedelta(0)}
\end{itemize}
\subsection{\class{date} Objects \label{datetime-date}}
A \class{date} object represents a date (year, month and day) in an idealized
calendar, the current Gregorian calendar indefinitely extended in both
directions. January 1 of year 1 is called day number 1, January 2 of year
1 is called day number 2, and so on. This matches the definition of the
"proleptic Gregorian" calendar in Dershowitz and Reingold's book
"Calendrical Calculations", where it's the base calendar for all
computations. See the book for algorithms for converting between
proleptic Gregorian ordinals and many other calendar systems.
\begin{funcdesc}{date}{year, month, day}
All arguments are required. Arguments may be ints or longs, in the
following ranges:
\begin{itemize}
\item \code{MINYEAR <= \var{year} <= MAXYEAR}
\item \code{1 <= \var{month} <= 12}
\item \code{1 <= \var{day} <= number of days in the given month and year}
\end{itemize}
If an argument outside those ranges is given, \exception{ValueError}
is raised.
\end{funcdesc}
Other constructors, all class methods:
\begin{methoddesc}{today}{}
Return the current local date. This is equivalent to
\code{date.fromtimestamp(time.time())}.
\end{methoddesc}
\begin{methoddesc}{fromtimestamp}{timestamp}
Return the local date corresponding to the POSIX timestamp, such
as is returned by \function{time.time()}. This may raise
\exception{ValueError}, if the timestamp is out of the range of
values supported by the platform C \cfunction{localtime()}
function. It's common for this to be restricted to years from 1970
through 2038.
\end{methoddesc}
\begin{methoddesc}{fromordinal}{ordinal}
Return the date corresponding to the proleptic Gregorian ordinal,
where January 1 of year 1 has ordinal 1. \exception{ValueError}
is raised unless \code{1 <= \var{ordinal} <= date.max.toordinal()}. For any
date \var{d}, \code{date.fromordinal(\var{d}.toordinal()) == \var{d}}.
\end{methoddesc}
Class attributes:
\begin{memberdesc}{min}
The earliest representable date, \code{date(MINYEAR, 1, 1)}.
\end{memberdesc}
\begin{memberdesc}{max}
The latest representable date, \code{date(MAXYEAR, 12, 31)}.
\end{memberdesc}
\begin{memberdesc}{resolution}
The smallest possible difference between non-equal date
objects, \code{timedelta(days=1)}.
\end{memberdesc}
Instance attributes (read-only):
\begin{memberdesc}{year}
Between \constant{MINYEAR} and \constant{MAXYEAR} inclusive
\end{memberdesc}
\begin{memberdesc}{month}
Between 1 and 12 inclusive.
\end{memberdesc}
\begin{memberdesc}{day}
Between 1 and the number of days in the given month
of the given year.
\end{memberdesc}
Supported operations:
\begin{itemize}
\item
date1 + timedelta -> date2
timedelta + date1 -> date2
date2 is timedelta.days days removed from the date1, moving forward
in time if timedelta.days > 0, or backward if timedetla.days < 0.
date2 - date1 == timedelta.days after. timedelta.seconds and
timedelta.microseconds are ignored. \exception{OverflowError} is
raised if date2.year would be smaller than \constant{MINYEAR} or
larger than \constant{MAXYEAR}.
\item
date1 - timedelta -> date2
Computes the date2 such that date2 + timedelta == date1. This
isn't quite equivalent to date1 + (-timedelta), because -timedelta
in isolation can overflow in cases where date1 - timedelta does
not. timedelta.seconds and timedelta.microseconds are ignored.
\item
date1 - date2 -> timedelta
This is exact, and cannot overflow. timedelta.seconds and
timedelta.microseconds are 0, and date2 + timedelta == date1
after.
\item
comparison of date to date, where date1 is considered less than
date2 when date1 precedes date2 in time. In other words,
date1 < date2 if and only if date1.toordinal() < date2.toordinal().
\item
hash, use as dict key
\item
efficient pickling
\item
in Boolean contexts, all \class{date} objects are considered to be true
\end{itemize}
Instance methods:
\begin{methoddesc}{replace}{year, month, day}
Return a date with the same value, except for those fields given
new values by whichever keyword arguments are specified. For
example, if \code{d == date(2002, 12, 31)}, then
\code{d.replace(day=26) == date(2000, 12, 26)}.
\end{methoddesc}
\begin{methoddesc}{timetuple}{}
Return a 9-element tuple of the form returned by
\function{time.localtime()}. The hours, minutes and seconds are
0, and the DST flag is -1.
d.timetuple() is equivalent to
(d.year, d.month, d.day,
0, 0, 0, \# h, m, s
d.weekday(), \# 0 is Monday
d.toordinal() - date(d.year, 1, 1).toordinal() + 1, \# day of year
-1)
\end{methoddesc}
\begin{methoddesc}{toordinal}{}
Return the proleptic Gregorian ordinal of the date, where January 1
of year 1 has ordinal 1. For any \class{date} object \var{d},
\code{date.fromordinal(\var{d}.toordinal()) == \var{d}}.
\end{methoddesc}
\begin{methoddesc}{weekday}{}
Return the day of the week as an integer, where Monday is 0 and
Sunday is 6. For example, date(2002, 12, 4).weekday() == 2, a
Wednesday.
See also \method{isoweekday()}.
\end{methoddesc}
\begin{methoddesc}{isoweekday}{}
Return the day of the week as an integer, where Monday is 1 and
Sunday is 7. For example, date(2002, 12, 4).isoweekday() == 3, a
Wednesday.
See also \method{weekday()}, \method{isocalendar()}.
\end{methoddesc}
\begin{methoddesc}{isocalendar}{}
Return a 3-tuple, (ISO year, ISO week number, ISO weekday).
The ISO calendar is a widely used variant of the Gregorian calendar.
See \url{http://www.phys.uu.nl/~vgent/calendar/isocalendar.htm}
for a good explanation.
The ISO year consists of 52 or 53 full weeks, and where a week starts
on a Monday and ends on a Sunday. The first week of an ISO year is
the first (Gregorian) calendar week of a year containing a Thursday.
This is called week number 1, and the ISO year of that Thursday is
the same as its Gregorian year.
For example, 2004 begins on a Thursday, so the first week of ISO
year 2004 begins on Monday, 29 Dec 2003 and ends on Sunday, 4 Jan
2004, so that
date(2003, 12, 29).isocalendar() == (2004, 1, 1)
date(2004, 1, 4).isocalendar() == (2004, 1, 7)
\end{methoddesc}
\begin{methoddesc}{isoformat}{}
Return a string representing the date in ISO 8601 format,
'YYYY-MM-DD'. For example,
date(2002, 12, 4).isoformat() == '2002-12-04'.
\end{methoddesc}
\begin{methoddesc}{__str__}{}
For a date \var{d}, \code{str(\var{d})} is equivalent to
\code{\var{d}.isoformat()}.
\end{methoddesc}
\begin{methoddesc}{ctime}{}
Return a string representing the date, for example
date(2002, 12, 4).ctime() == 'Wed Dec 4 00:00:00 2002'.
d.ctime() is equivalent to time.ctime(time.mktime(d.timetuple()))
on platforms where the native C \cfunction{ctime()} function
(which \function{time.ctime()} invokes, but which
\method{date.ctime()} does not invoke) conforms to the C standard.
\end{methoddesc}
\begin{methoddesc}{strftime}{format}
Return a string representing the date, controlled by an explicit
format string. Format codes referring to hours, minutes or seconds
will see 0 values.
See the section on \method{strftime()} behavior.
\end{methoddesc}
\subsection{\class{datetime} Objects \label{datetime-datetime}}
A \class{datetime} object is a single object containing all the
information from a \class{date} object and a time object. Like a
\class{date} object, \class{datetime} assumes the current Gregorian
calendar extended in both directions; like a time object,
\class{datetime} assumes there are exactly 3600*24 seconds in every
day.
\begin{funcdesc}datetime{year, month, day,
hour=0, minute=0, second=0, microsecond=0}
The year, month and day arguments are required. Arguments may be ints
or longs, in the following ranges:
\begin{itemize}
\item \code{\member{MINYEAR} <= \var{year} <= \member{MAXYEAR}}
\item \code{1 <= \var{month} <= 12}
\item \code{1 <= \var{day} <= number of days in the given month and year}
\item \code{0 <= \var{hour} < 24}
\item \code{0 <= \var{minute} < 60}
\item \code{0 <= \var{second} < 60}
\item \code{0 <= \var{microsecond} < 1000000}
\end{itemize}
If an argument outside those ranges is given,
\exception{ValueError} is raised.
\end{funcdesc}
Other constructors, all class methods:
\begin{methoddesc}{today}{}
Return the current local datetime. This is equivalent to
\code{datetime.fromtimestamp(time.time())}.
See also \method{now()}, \method{fromtimestamp()}.
\end{methoddesc}
\begin{methoddesc}{now}{}
Return the current local datetime. This is like \method{today()},
but, if possible, supplies more precision than can be gotten from
going through a \function{time.time()} timestamp (for example,
this may be possible on platforms that supply the C
\cfunction{gettimeofday()} function).
See also \method{today()}, \method{utcnow()}.
\end{methoddesc}
\begin{methoddesc}{utcnow}{}
Return the current UTC datetime. This is like \method{now()}, but
returns the current UTC date and time.
See also \method{now()}.
\end{methoddesc}
\begin{methoddesc}{fromtimestamp}{timestamp}
Return the local \class{datetime} corresponding to the \POSIX{}
timestamp, such as is returned by \function{time.time()}. This
may raise \exception{ValueError}, if the timestamp is out of the
range of values supported by the platform C
\cfunction{localtime()} function. It's common for this to be
restricted to years in 1970 through 2038.
See also \method{utcfromtimestamp()}.
\end{methoddesc}
\begin{methoddesc}{utcfromtimestamp}{timestamp}
Return the UTC \class{datetime} corresponding to the \POSIX{}
timestamp. This may raise \exception{ValueError}, if the
timestamp is out of the range of values supported by the platform
C \cfunction{gmtime()} function. It's common for this to be
restricted to years in 1970 through 2038.
See also \method{fromtimestamp()}.
\end{methoddesc}
\begin{methoddesc}{fromordinal}{ordinal}
Return the \class{datetime} corresponding to the proleptic
Gregorian ordinal, where January 1 of year 1 has ordinal 1.
\exception{ValueError} is raised unless 1 <= ordinal <=
datetime.max.toordinal(). The hour, minute, second and
microsecond of the result are all 0.
\end{methoddesc}
\begin{methoddesc}{combine}{date, time}
Return a new \class{datetime} object whose date components are
equal to the given \class{date} object's, and whose time components are
equal to the given time object's. For any \class{datetime} object
d, d == datetime.combine(d.date(), d.time()).
If date is a \class{datetime} or \class{datetimetz} object, its
time components are ignored. If date is \class{datetimetz}
object, its \member{tzinfo} component is also ignored. If time is
a \class{timetz} object, its \member{tzinfo} component is ignored.
\end{methoddesc}
Class attributes:
\begin{memberdesc}{min}
The earliest representable \class{datetime},
\code{datetime(MINYEAR, 1, 1)}.
\end{memberdesc}
\begin{memberdesc}{max}
The latest representable \class{datetime},
\code{datetime(MAXYEAR, 12, 31, 23, 59, 59, 999999)}.
\end{memberdesc}
\begin{memberdesc}{resolution}
The smallest possible difference between non-equal \class{datetime}
objects, \code{timedelta(microseconds=1)}.
\end{memberdesc}
Instance attributes (read-only):
\begin{memberdesc}{year}
Between \constant{MINYEAR} and \constant{MAXYEAR} inclusive
\end{memberdesc}
\begin{memberdesc}{month}
Between 1 and 12 inclusive
\end{memberdesc}
\begin{memberdesc}{day}
Between 1 and the number of days in the given month
of the given year.
\end{memberdesc}
\begin{memberdesc}{hour}
In \code{range(24)}.
\end{memberdesc}
\begin{memberdesc}{minute}
In \code{range(60)}.
\end{memberdesc}
\begin{memberdesc}{second}
In \code{range(60)}.
\end{memberdesc}
\begin{memberdesc}{microsecond}
In \code{range(1000000)}.
\end{memberdesc}
Supported operations:
\begin{itemize}
\item
datetime1 + timedelta -> datetime2
timedelta + datetime1 -> datetime2
datetime2 is a duration of timedelta removed from datetime1, moving
forward in time if timedelta.days > 0, or backward if
timedelta.days < 0. datetime2 - datetime1 == timedelta after.
\exception{OverflowError} is raised if datetime2.year would be
smaller than \constant{MINYEAR} or larger than \constant{MAXYEAR}.
\item
datetime1 - timedelta -> datetime2
Computes the datetime2 such that datetime2 + timedelta == datetime1.
This isn't quite equivalent to datetime1 + (-timedelta), because
-timedelta in isolation can overflow in cases where
datetime1 - timedelta does not.
\item
datetime1 - datetime2 -> timedelta
This is exact, and cannot overflow.
datetime2 + timedelta == datetime1 after.
\item
comparison of \class{datetime} to datetime, where datetime1 is
considered less than datetime2 when datetime1 precedes datetime2
in time.
\item
hash, use as dict key
\item
efficient pickling
\item
in Boolean contexts, all \class{datetime} objects are considered
to be true
\end{itemize}
Instance methods:
\begin{methoddesc}{date}{}
Return \class{date} object with same year, month and day.
\end{methoddesc}
\begin{methoddesc}{time}{}
Return time object with same hour, minute, second and microsecond.
\end{methoddesc}
\begin{methoddesc}{replace}{year=, month=, day=, hour=, minute=, second=, microsecond=}
Return a datetime with the same value, except for those fields given
new values by whichever keyword arguments are specified.
\end{methoddesc}
\begin{methoddesc}{astimezone}{tz}
Return a \class{datetimetz} with the same date and time fields, and
with \member{tzinfo} member \var{tz}. \var{tz} must be \code{None},
or an instance of a \class{tzinfo} subclass.
\end{methoddesc}
\begin{methoddesc}{timetuple}{}
Return a 9-element tuple of the form returned by
\function{time.localtime()}.
The DST flag is -1. \code{d.timetuple()} is equivalent to
(d.year, d.month, d.day,
d.hour, d.minute, d.second,
d.weekday(), \# 0 is Monday
d.toordinal() - date(d.year, 1, 1).toordinal() + 1, \# day of year
-1)
\end{methoddesc}
\begin{methoddesc}{toordinal}{}
Return the proleptic Gregorian ordinal of the date. The same as
\method{date.toordinal()}.
\end{methoddesc}
\begin{methoddesc}{weekday}{}
Return the day of the week as an integer, where Monday is 0 and
Sunday is 6. The same as \method{date.weekday()}.
See also \method{isoweekday()}.
\end{methoddesc}
\begin{methoddesc}{isoweekday}{}
Return the day of the week as an integer, where Monday is 1 and
Sunday is 7. The same as \method{date.isoweekday()}.
See also \method{weekday()}, \method{isocalendar()}.
\end{methoddesc}
\begin{methoddesc}{isocalendar}{}
Return a 3-tuple, (ISO year, ISO week number, ISO weekday). The
same as \method{date.isocalendar()}.
\end{methoddesc}
\begin{methoddesc}{isoformat}{sep='T'}
Return a string representing the date and time in ISO 8601 format,
YYYY-MM-DDTHH:MM:SS.mmmmmm
or, if self.microsecond is 0,
YYYY-MM-DDTHH:MM:SS
The optional argument \var{sep} (default \code{'T'}) is a
one-character separator, placed between the date and time portions
of the result. For example,
datetime(2002, 12, 4, 1, 2, 3, 4).isoformat(' ') ==
'2002-12-04 01:02:03.000004'
\end{methoddesc}
\begin{methoddesc}{__str__}{}
For a \class{datetime} instance \var{d}, \code{str(\var{d})} is
equivalent to \code{\var{d}.isoformat(' ')}.
\end{methoddesc}
\begin{methoddesc}{ctime}{}
Return a string representing the date, for example
datetime(2002, 12, 4, 20, 30, 40).ctime() == 'Wed Dec 4 20:30:40 2002'.
\code{d.ctime()} is equivalent to
\code{time.ctime(time.mktime(d.timetuple()))} on platforms where
the native C \cfunction{ctime()} function (which
\function{time.ctime()} invokes, but which
\method{datetime.ctime()} does not invoke) conforms to the C
standard.
\end{methoddesc}
\begin{methoddesc}{strftime}{format}
Return a string representing the date and time, controlled by an
explicit format string. See the section on \method{strftime()}
behavior.
\end{methoddesc}
\subsection{\class{time} Objects \label{datetime-time}}
A time object represents an idealized time of day, independent of day
and timezone.
Constructor:
time(hour=0, minute=0, second=0, microsecond=0)
All arguments are optional. They may be ints or longs, in the
following ranges:
0 <= hour < 24
0 <= minute < 60
0 <= second < 60
0 <= microsecond < 1000000
If an argument outside those ranges is given,
\exception{ValueError} is raised.
Class attributes:
\begin{memberdesc}{min}
The earliest representable \class{time}, \code{time(0, 0, 0, 0)}.
\end{memberdesc}
\begin{memberdesc}{max}
The latest representable \class{time}, \code{time(23, 59, 59, 999999)}.
\end{memberdesc}
\begin{memberdesc}{resolution}
The smallest possible difference between non-equal \class{time}
objects, \code{timedelta(microseconds=1)}, although note that
arithmetic on \class{time} objects is not supported.
\end{memberdesc}
Instance attributes (read-only):
\begin{memberdesc}{hour}
In \code{range(24)}.
\end{memberdesc}
\begin{memberdesc}{minute}
In \code{range(60)}.
\end{memberdesc}
\begin{memberdesc}{second}
In \code{range(60)}.
\end{memberdesc}
\begin{memberdesc}{microsecond}
In \code{range(1000000)}.
\end{memberdesc}
Supported operations:
\begin{itemize}
\item
comparison of time to time, where time1 is considered
less than time2 when time1 precedes time2 in time.
\item
hash, use as dict key
\item
efficient pickling
\item
in Boolean contexts, a time object is considered to be true
if and only if it isn't equal to time(0)
\end{itemize}
Instance methods:
\begin{methoddesc}{replace}{hour=, minute=, second=, microsecond=}
Return a time with the same value, except for those fields given
new values by whichever keyword arguments are specified.
\end{methoddesc}
\begin{methoddesc}{isoformat}{}
Return a string representing the time in ISO 8601 format,
HH:MM:SS.mmmmmm
or, if self.microsecond is 0
HH:MM:SS
\end{methoddesc}
\begin{methoddesc}{__str__}{}
For a time \var{t}, \code{str(\var{t})} is equivalent to
\code{\var{t}.isoformat()}.
\end{methoddesc}
\begin{methoddesc}{strftime}{format}
Return a string representing the time, controlled by an explicit
format string. See the section on \method{strftime()} behavior.
\end{methoddesc}
\subsection{\class{tzinfo} Objects \label{datetime-tzinfo}}
\class{tzinfo} is an abstract base clase, meaning that this class
should not be instantiated directly. You need to derive a concrete
subclass, and (at least) supply implementations of the standard
\class{tzinfo} methods needed by the \class{datetime} methods you
use. The \module{datetime} module does not supply any concrete
subclasses of \class{tzinfo}.
An instance of (a concrete subclass of) \class{tzinfo} can be passed
to the constructors for \class{datetimetz} and \class{timetz} objects.
The latter objects view their fields as being in local time, and the
\class{tzinfo} object supports methods revealing offset of local time
from UTC, the name of the time zone, and DST offset, all relative to a
date or time object passed to them.
Special requirement for pickling: A tzinfo subclass must have an
\method{__init__} method that can be called with no arguments, else it
can be pickled but possibly not unpickled again. This is a technical
requirement that may be relaxed in the future.
A concrete subclass of \class{tzinfo} may need to implement the
following methods. Exactly which methods are needed depends on the
uses made of aware \module{datetime} objects; if in doubt, simply
implement all of them. The methods are called by a \class{datetimetz}
or \class{timetz} object, passing itself as the argument. A
\class{tzinfo} subclass's methods should be prepared to accept a dt
argument of \code{None} or of type \class{timetz} or
\class{datetimetz}.
\begin{methoddesc}{utcoffset}{dt}
Return offset of local time from UTC, in minutes east of UTC. If
local time is west of UTC, this should be negative. Note that this
is intended to be the total offset from UTC; for example, if a
\class{tzinfo} object represents both time zone and DST adjustments,
\method{utcoffset()} should return their sum. If the UTC offset
isn't known, return \code{None}. Else the value returned must be
an integer, in the range -1439 to 1439 inclusive (1440 = 24*60;
the magnitude of the offset must be less than one day), or a
\class{timedelta} object representing a whole number of minutes
in the same range.
\end{methoddesc}
\begin{methoddesc}{tzname}{dt}
Return the timezone name corresponding to the \class{datetime} represented
by dt, as a string. Nothing about string names is defined by the
\module{datetime} module, and there's no requirement that it mean anything
in particular. For example, "GMT", "UTC", "-500", "-5:00", "EDT",
"US/Eastern", "America/New York" are all valid replies. Return
\code{None} if a string name isn't known. Note that this is a method
rather than a fixed string primarily because some \class{tzinfo} objects
will wish to return different names depending on the specific value
of dt passed, especially if the \class{tzinfo} class is accounting for DST.
\end{methoddesc}
\begin{methoddesc}{dst}{dt}
Return the DST offset, in minutes east of UTC, or \code{None} if
DST information isn't known. Return 0 if DST is not in effect.
If DST is in effect, return the offset as an integer or
\class{timedelta} object (see \method{utcoffset()} for details).
Note that DST offset, if applicable, has
already been added to the UTC offset returned by
\method{utcoffset()}, so there's no need to consult \method{dst()}
unless you're interested in displaying DST info separately. For
example, \method{datetimetz.timetuple()} calls its \member{tzinfo}
member's \method{dst()} method to determine how the
\member{tm_isdst} flag should be set.
\end{methoddesc}
Example \class{tzinfo} classes:
\verbatiminput{tzinfo-examples.py}
\subsection{\class{timetz} Objects \label{datetime-timetz}}
A time object represents a (local) time of day, independent of any
particular day, and subject to adjustment via a \class{tzinfo} object.
Constructor:
\begin{funcdesc}{time}{hour=0, minute=0, second=0, microsecond=0, tzinfo=None}
All arguments are optional. \var{tzinfo} may be \code{None}, or
an instance of a \class{tzinfo} subclass. The remaining arguments
may be ints or longs, in the following ranges:
\begin{itemize}
\item \code{0 <= \var{hour} < 24}
\item \code{0 <= \var{minute} < 60}
\item \code{0 <= \var{second} < 60}
\item \code{0 <= \var{microsecond} < 1000000}.
\end{itemize}
If an argument outside those ranges is given,
\exception{ValueError} is raised.
\end{funcdesc}
Class attributes:
\begin{memberdesc}{min}
The earliest representable time, \code{timetz(0, 0, 0, 0)}.
\end{memberdesc}
\begin{memberdesc}{max}
The latest representable time, \code{timetz(23, 59, 59, 999999)}.
\end{memberdesc}
\begin{memberdesc}{resolution}
The smallest possible difference between non-equal \class{timetz}
objects, \code{timedelta(microseconds=1)}, although note that
arithmetic on \class{timetz} objects is not supported.
\end{memberdesc}
Instance attributes (read-only):
.hour in range(24)
.minute in range(60)
.second in range(60)
.microsecond in range(1000000)
.tzinfo the object passed as the tzinfo argument to the
\class{timetz} constructor, or \code{None} if none
was passed.
Supported operations:
\begin{itemize}
\item
comparison of \class{timetz} to \class{time} or \class{timetz},
where \var{a} is considered less than \var{b} when \var{a} precedes
\var{b} in time. If one comparand is {\naive} and the other is aware,
\exception{TypeError} is raised. If both comparands are aware, and
have the same \member{tzinfo} member, the common \member{tzinfo}
member is ignored and the base times are compared. If both
comparands are aware and have different \member{tzinfo} members,
the comparands are first adjusted by subtracting their UTC offsets
(obtained from \code{self.utcoffset()}).
\item
hash, use as dict key
\item
pickling
\item
in Boolean contexts, a \class{timetz} object is considered to be
true if and only if, after converting it to minutes and
subtracting \method{utcoffset()} (or \code{0} if that's
\code{None}), the result is non-zero.
\end{itemize}
Instance methods:
\begin{methoddesc}{replace}(hour=, minute=, second=, microsecond=, tzinfo=)
Return a \class{timetz} with the same value, except for those fields given
new values by whichever keyword arguments are specified. Note that
\code{tzinfo=None} can be specified to create a {\naive} \class{timetz} from an
aware \class{timetz}.
\end{methoddesc}
\begin{methoddesc}{isoformat}{}
Return a string representing the time in ISO 8601 format,
HH:MM:SS.mmmmmm
or, if self.microsecond is 0,
HH:MM:SS
If \method{utcoffset()} does not return \code{None}, a 6-character
string is appended, giving the UTC offset in (signed) hours and
minutes:
HH:MM:SS.mmmmmm+HH:MM
or, if self.microsecond is 0,
HH:MM:SS+HH:MM
\end{methoddesc}
\begin{methoddesc}{__str__}{}
For a \class{timetz} \var{t}, \code{str(\var{t})} is equivalent to
\code{\var{t}.isoformat()}.
\end{methoddesc}
\begin{methoddesc}{strftime}{format}
Return a string representing the time, controlled by an explicit
format string. See the section on \method{strftime()} behavior.
\end{methoddesc}
\begin{methoddesc}{utcoffset}{}
If \member{tzinfo} is \code{None}, returns \code{None}, else
\code{tzinfo.utcoffset(self)} converted to a \class{timedelta}
object.
\end{methoddesc}
\begin{methoddesc}{tzname}{}
If \member{tzinfo} is \code{None}, returns \code{None}, else
\code{tzinfo.tzname(self)}.
\end{methoddesc}
\begin{methoddesc}{dst}{}
If \member{tzinfo} is \code{None}, returns \code{None}, else
\code{tzinfo.dst(self)} converted to a \class{timedelta} object.
\end{methoddesc}
\subsection{ \class{datetimetz} Objects \label{datetime-datetimetz}}
\begin{notice}[warning]
I think this is \emph{still} missing some methods from the
Python implementation.
\end{notice}
A \class{datetimetz} object is a single object containing all the information
from a \class{date} object and a \class{timetz} object.
Constructor:
\begin{funcdesc}{datetimetz}{year, month, day,
hour=0, minute=0, second=0, microsecond=0, tzinfo=None}
The year, month and day arguments are required. \var{tzinfo} may
be \code{None}, or an instance of a \class{tzinfo} subclass. The
remaining arguments may be ints or longs, in the following ranges:
\begin{itemize}
\item \code{MINYEAR <= \var{year} <= MAXYEAR}
\item \code{1 <= \var{month} <= 12}
\item \code{1 <= \var{day} <= number of days in the given month and year}
\item \code{0 <= \var{hour} < 24}
\item \code{0 <= \var{minute} < 60}
\item \code{0 <= \var{second} < 60}
\item \code{0 <= \var{microsecond} < 1000000}
\end{itemize}
If an argument outside those ranges is given,
\exception{ValueError} is raised.
\end{funcdesc}
Other constructors (class methods):
\begin{funcdesc}{today}{}
\methodline{utcnow}{}
\methodline{utcfromtimestamp}{timestamp}
\methodline{fromordinal}{ordinal}
These are the same as the \class{datetime} class methods of the
same names, except that they construct a \class{datetimetz}
object, with tzinfo \code{None}.
\end{funcdesc}
\begin{funcdesc}{now}{\optional{tzinfo=None}}
\methodline{fromtimestamp}{timestamp\optional{, tzinfo=None}}
These are the same as the \class{datetime} class methods of the same names,
except that they accept an additional, optional tzinfo argument, and
construct a \class{datetimetz} object with that \class{tzinfo} object attached.
\end{funcdesc}
\begin{funcdesc}{combine}{date, time}
This is the same as \method{datetime.combine()}, except that it constructs
a \class{datetimetz} object, and, if the time object is of type timetz,
the \class{datetimetz} object has the same \class{tzinfo} object as the time object.
\end{funcdesc}
Class attributes:
\begin{memberdesc}{min}
The earliest representable \class{datetimetz},
\code{datetimetz(MINYEAR, 1, 1)}.
\end{memberdesc}
\begin{memberdesc}{max}
The latest representable \class{datetime},
\code{datetimetz(MAXYEAR, 12, 31, 23, 59, 59, 999999)}.
\end{memberdesc}
\begin{memberdesc}{resolution}
The smallest possible difference between non-equal \class{datetimetz}
objects, \code{timedelta(microseconds=1)}.
\end{memberdesc}
Instance attributes, all read-only:
\begin{memberdesc}{year}
Between MINYEAR and MAXYEAR inclusive
\end{memberdesc}
\begin{memberdesc}{month}
Between 1 and 12 inclusive
\end{memberdesc}
\begin{memberdesc}{day}
Between 1 and the number of days in the given month
of the given year
\end{memberdesc}
\begin{memberdesc}{hour}
In \code{range(24)}.
\end{memberdesc}
\begin{memberdesc}{minute}
In \code{range(60)}.
\end{memberdesc}
\begin{memberdesc}{second}
In \code{range(60)}.
\end{memberdesc}
\begin{memberdesc}{microsecond}
In \code{range(1000000)}.
\end{memberdesc}
\begin{memberdesc}{tzinfo}
The object passed as the \var{tzinfo} argument to
the \class{datetimetz} constructor, or \code{None}
if none was passed.
\end{memberdesc}
Supported operations:
\begin{itemize}
\item
datetimetz1 + timedelta -> datetimetz2
timedelta + datetimetz1 -> datetimetz2
The same as addition of \class{datetime} objects, except that
datetimetz2.tzinfo is set to datetimetz1.tzinfo.
\item
datetimetz1 - timedelta -> datetimetz2
The same as addition of \class{datetime} objects, except that
datetimetz2.tzinfo is set to datetimetz1.tzinfo.
\item
aware_datetimetz1 - aware_datetimetz2 -> timedelta
{\naive}_datetimetz1 - {\naive}_datetimetz2 -> timedelta
{\naive}_datetimetz1 - datetime2 -> timedelta
datetime1 - {\naive}_datetimetz2 -> timedelta
Subtraction of a \class{datetime} or \class{datetimetz}, from a
\class{datetime} or \class{datetimetz}, is defined only if both
operands are \naive, or if both are aware. If one is aware and the
other is \naive, \exception{TypeError} is raised.
If both are \naive, or both are aware and have the same \member{tzinfo}
member, subtraction acts as for \class{datetime} subtraction.
If both are aware and have different \member{tzinfo} members,
\code{a-b} acts as if \var{a} and \var{b} were first converted to UTC
datetimes (by subtracting \code{a.utcoffset()} minutes from \var{a},
and \code{b.utcoffset()} minutes from \var{b}), and then doing
\class{datetime} subtraction, except that the implementation never
overflows.
\item
comparison of \class{datetimetz} to \class{datetime} or
\class{datetimetz}, where \var{a} is considered less than \var{b}
when \var{a} precedes \var{b} in time. If one comparand is naive and
the other is aware, \exception{TypeError} is raised. If both
comparands are aware, and have the same \member{tzinfo} member,
the common \member{tzinfo} member is ignored and the base datetimes
are compared. If both comparands are aware and have different
\member{tzinfo} members, the comparands are first adjusted by
subtracting their UTC offsets (obtained from \code{self.utcoffset()}).
\item
hash, use as dict key
\item
efficient pickling
\item
in Boolean contexts, all \class{datetimetz} objects are considered to be
true
\end{itemize}
Instance methods:
\begin{methoddesc}{date}{}
\methodline{time}{}
\methodline{toordinal}{}
\methodline{weekday}{}
\methodline{isoweekday}{}
\methodline{isocalendar}{}
\methodline{ctime}{}
\methodline{__str__}{}
\methodline{strftime}{format}
These are the same as the \class{datetime} methods of the same names.
\end{methoddesc}
\begin{methoddesc}{timetz}{}
Return \class{timetz} object with same hour, minute, second, microsecond,
and tzinfo.
\end{methoddesc}
\begin{methoddesc}{replace}{year=, month=, day=, hour=, minute=, second=, microsecond=,
tzinfo=}
Return a datetimetz with the same value, except for those fields given
new values by whichever keyword arguments are specified. Note that
\code{tzinfo=None} can be specified to create a naive datetimetz from
an aware datetimetz.
\end{methoddesc}
\begin{methoddesc}{astimezone}{tz}
Return a \class{datetimetz} with new tzinfo member \var{tz}. \var{tz}
must be \code{None}, or an instance of a \class{tzinfo} subclass. If
\var{tz} is \code{None}, self is naive, or
\code{tz.utcoffset(self)} returns \code{None},
\code{self.astimezone(tz)} is equivalent to
\code{self.replace(tzinfo=tz)}: a new timezone object is attached
without any conversion of date or time fields. If self is aware and
\code{tz.utcoffset(self)} does not return \code{None}, the date and
time fields are adjusted so that the result is local time in timezone
tz, representing the same UTC time as self. \code{self.astimezone(tz)}
is then equivalent to
\begin{verbatim}
(self - (self.utcoffset() - tz.utcoffset(self)).replace(tzinfo=tz)
\end{verbatim}
where the result of \code{tz.utcoffset(self)} is converted to a
\class{timedelta} if it's an integer.
\end{methoddesc}
\begin{methoddesc}{utcoffset}{}
If \member{tzinfo} is \code{None}, returns \code{None}, else
\code{tzinfo.utcoffset(self)} converted to a \class{timedelta}
object.
\end{methoddesc}
\begin{methoddesc}{tzname}{}
If \member{tzinfo} is \code{None}, returns \code{None}, else
returns \code{tzinfo.tzname(self)}.
\end{methoddesc}
\begin{methoddesc}{dst}{}
If \member{tzinfo} is \code{None}, returns \code{None}, else
\code{tzinfo.dst(self)} converted to a \class{timedelta}
object.
\end{methoddesc}
\begin{methoddesc}{timetuple}{}
Like \function{datetime.timetuple()}, but sets the
\member{tm_isdst} flag according to the \method{dst()} method: if
\method{dst()} returns \code{None}, \member{tm_isdst} is set to
\code{-1}; else if \method{dst()} returns a non-zero value,
\member{tm_isdst} is set to \code{1}; else \code{tm_isdst} is set
to \code{0}.
\end{methoddesc}
\begin{methoddesc}{utctimetuple}{}
If \class{datetimetz} instance \var{d} is \naive, this is the same as
\code{\var{d}.timetuple()} except that \member{tm_isdst} is forced to 0
regardless of what \code{d.dst()} returns. DST is never in effect
for a UTC time.
If \var{d} is aware, \var{d} is normalized to UTC time, by subtracting
\code{\var{d}.utcoffset()} minutes, and a timetuple for the
normalized time is returned. \member{tm_isdst} is forced to 0.
Note that the result's \member{tm_year} field may be
\constant{MINYEAR}-1 or \constant{MAXYEAR}+1, if \var{d}.year was
\code{MINYEAR} or \code{MAXYEAR} and UTC adjustment spills over a
year boundary.
\end{methoddesc}
\begin{methoddesc}{isoformat}{sep='T'}
Return a string representing the date and time in ISO 8601 format,
YYYY-MM-DDTHH:MM:SS.mmmmmm
or, if \member{microsecond} is 0,
YYYY-MM-DDTHH:MM:SS
If \method{utcoffset()} does not return \code{None}, a 6-character
string is appended, giving the UTC offset in (signed) hours and
minutes:
YYYY-MM-DDTHH:MM:SS.mmmmmm+HH:MM
or, if \member{microsecond} is 0
YYYY-MM-DDTHH:MM:SS+HH:MM
The optional argument \var{sep} (default \code{'T'}) is a
one-character separator, placed between the date and time portions
of the result. For example,
\begin{verbatim}
>>> from datetime import *
>>> class TZ(tzinfo):
... def utcoffset(self, dt): return -399
...
>>> datetimetz(2002, 12, 25, tzinfo=TZ()).isoformat(' ')
'2002-12-25 00:00:00-06:39'
\end{verbatim}
\end{methoddesc}
\code{str(\var{d})} is equivalent to \code{\var{d}.isoformat(' ')}.
\subsection{\method{strftime()} Behavior}
\class{date}, \class{datetime}, \class{datetimetz}, \class{time},
and \class{timetz} objects all support a \code{strftime(\var{format})}
method, to create a string representing the time under the control of
an explicit format string. Broadly speaking,
\code{d.strftime(fmt)}
acts like the \refmodule{time} module's
\code{time.strftime(fmt, d.timetuple())}
although not all objects support a \method{timetuple()} method.
For \class{time} and \class{timetz} objects, the format codes for
year, month, and day should not be used, as time objects have no such
values. If they're used anyway, \code{1900} is substituted for the
year, and \code{0} for the month and day.
For \class{date} objects, the format codes for hours, minutes, and
seconds should not be used, as \class{date} objects have no such
values. If they're used anyway, \code{0} is substituted for them.
For a {\naive} object, the \code{\%z} and \code{\%Z} format codes are
replaced by empty strings.
For an aware object:
\begin{itemize}
\item[\code{\%z}]
\method{utcoffset()} is transformed into a 5-character string of
the form +HHMM or -HHMM, where HH is a 2-digit string giving the
number of UTC offset hours, and MM is a 2-digit string giving the
number of UTC offset minutes. For example, if
\method{utcoffset()} returns \code{timedelta(hours=-3, minutes=-30)},
\code{\%z} is replaced with the string \code{'-0330'}.
\item[\code{\%Z}]
If \method{tzname()} returns \code{None}, \code{\%Z} is replaced
by an empty string. Otherwise \code{\%Z} is replaced by the returned
value, which must be a string.
\end{itemize}
The full set of format codes supported varies across platforms,
because Python calls the platform C library's \function{strftime()}
function, and platform variations are common. The documentation for
Python's \refmodule{time} module lists the format codes that the C
standard (1989 version) requires, and those work on all platforms
with a standard C implementation. Note that the 1999 version of the
C standard added additional format codes.
The exact range of years for which \method{strftime()} works also
varies across platforms. Regardless of platform, years before 1900
cannot be used.
\begin{comment}
\subsection{C API}
Struct typedefs:
PyDateTime_Date
PyDateTime_DateTime
PyDateTime_DateTimeTZ
PyDateTime_Time
PyDateTime_TimeTZ
PyDateTime_Delta
PyDateTime_TZInfo
Type-check macros:
PyDate_Check(op)
PyDate_CheckExact(op)
PyDateTime_Check(op)
PyDateTime_CheckExact(op)
PyDateTimeTZ_Check(op)
PyDateTimeTZ_CheckExact(op)
PyTime_Check(op)
PyTime_CheckExact(op)
PyTimeTZ_Check(op)
PyTimeTZ_CheckExact(op)
PyDelta_Check(op)
PyDelta_CheckExact(op)
PyTZInfo_Check(op)
PyTZInfo_CheckExact(op
Accessor macros:
All objects are immutable, so accessors are read-only. All macros
return ints:
For \class{date}, \class{datetime}, and \class{datetimetz} instances:
PyDateTime_GET_YEAR(o)
PyDateTime_GET_MONTH(o)
PyDateTime_GET_DAY(o)
For \class{datetime} and \class{datetimetz} instances:
PyDateTime_DATE_GET_HOUR(o)
PyDateTime_DATE_GET_MINUTE(o)
PyDateTime_DATE_GET_SECOND(o)
PyDateTime_DATE_GET_MICROSECOND(o)
For \class{time} and \class{timetz} instances:
PyDateTime_TIME_GET_HOUR(o)
PyDateTime_TIME_GET_MINUTE(o)
PyDateTime_TIME_GET_SECOND(o)
PyDateTime_TIME_GET_MICROSECOND(o)
\end{comment}
|