This repository has been archived by the owner on Dec 23, 2022. It is now read-only.
/
DateTime.py
1156 lines (945 loc) · 35.4 KB
/
DateTime.py
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
""" Python part of the low-level DateTime[Delta] type implementation.
Copyright (c) 1998-2001, Marc-Andre Lemburg; mailto:mal@lemburg.com
Copyright (c) 2000-2015, eGenix.com Software GmbH; mailto:info@egenix.com
See the documentation for further information on copyrights,
or contact the author. All Rights Reserved.
"""
# Import C extension module
from mxDateTime import *
from mxDateTime import __version__
# Singletons
oneSecond = DateTimeDelta(0,0,0,1)
oneMinute = DateTimeDelta(0,0,1)
oneHour = DateTimeDelta(0,1)
oneDay = DateTimeDelta(1)
oneWeek = DateTimeDelta(7)
Epoch = DateTimeFromAbsDateTime(1, 0)
# Shortcuts for pickle; for backward compatibility only (they are now
# defined in __init__.py to further reduce the pickles length)
def _DT(absdate,abstime):
return DateTimeFromAbsDateTime(absdate,abstime)
def _DTD(seconds):
return DateTimeDeltaFromSeconds(seconds)
# Module init
class modinit:
global _time, _math, _types, _sys, _pydatetime, _pydate, _pytime, _pytimedelta
import time, math, types, sys
_time = time
_math = math
_types = types
_sys = sys
try:
import datetime
_pydatetime = datetime.datetime
_pydate = datetime.date
_pytime = datetime.time
_pytimedelta = datetime.timedelta
except ImportError:
# Set them to (), so that isinstance() works with them
_pydatetime = ()
_pydate = ()
_pytime = ()
_pytimedelta = ()
del modinit
### Helpers
def _isstring(arg,
isinstance=isinstance, types=_types):
if isinstance(arg, types.StringType):
return 1
try:
if isinstance(arg, types.UnicodeType):
return 1
except AttributeError:
pass
return 0
### Compatibility APIs
# Aliases and functions to make 'from mx.DateTime import *' work much
# like 'from time import *'
def localtime(ticks=None,
# Locals:
time=_time.time,float=float,localtime=_time.localtime,
round=round,int=int,DateTime=DateTime,floor=_math.floor):
"""localtime(ticks=None)
Construct a DateTime instance using local time from ticks. If
ticks are not given, it defaults to the current time. The
result is similar to time.localtime(). Fractions of a second
are rounded to the nearest micro-second.
"""
if ticks is None:
ticks = time()
else:
ticks = float(ticks)
ticks = round(ticks, 6)
fticks = floor(ticks)
Y,M,D,h,m,s = localtime(fticks)[:6]
s = s + (ticks - fticks)
return DateTime(Y,M,D,h,m,s)
def gmtime(ticks=None,
# Locals:
time=_time.time,float=float,gmtime=_time.gmtime,
round=round,int=int,DateTime=DateTime,floor=_math.floor,
DateTimeDeltaFromSeconds=DateTimeDeltaFromSeconds,
Unix_Epoch=DateTime(1970, 1, 1)):
"""gmtime(ticks=None)
Construct a DateTime instance using UTC time from ticks. If
ticks are not given, it defaults to the current time. The
result is similar to time.gmtime(). Fractions of a second are
rounded to the nearest micro-second.
"""
if ticks is None:
ticks = time()
else:
ticks = float(ticks)
ticks = round(ticks, 6)
if POSIX:
# On POSIX platforms, the calulation is easy (and this also
# makes the calulation safe against year 2038 wrap-arounds and
# errors); see #421
return Unix_Epoch + DateTimeDeltaFromSeconds(float(ticks))
fticks = floor(ticks)
Y,M,D,h,m,s = gmtime(ticks)[:6]
s = s + (ticks - fticks)
return DateTime(Y,M,D,h,m,s)
def mktime((year,month,day,hour,minute,second,dow,doy,dst),
# Locals:
DateTime=DateTime):
"""mktime((year,month,day,hour,minute,second,dow,doy,dst))
Same as the DateTime() constructor accept that the interface
used is compatible to the similar time.mktime() API.
Note that the tuple elements dow, doy and dst are not used in
any way.
"""
return DateTime(year,month,day,hour,minute,second)
def ctime(datetime):
"""ctime(datetime)
Returns a string representation of the given DateTime instance
using the current locale's default settings.
"""
return datetime.strftime('%c')
def today(hour=0,minute=0,second=0.0,
# Locals:
localtime=_time.localtime,time=_time.time,DateTime=DateTime):
"""today(hour=0,minute=0,second=0.0)
Returns a DateTime instance for today (in local time) at the
given time (defaults to midnight).
"""
Y,M,D = localtime(time())[:3]
return DateTime(Y,M,D,hour,minute,second)
def TimeDelta(hours=0.0,minutes=0.0,seconds=0.0,
# Locals:
DateTimeDelta=DateTimeDelta):
"""TimeDelta(hours=0.0,minutes=0.0,seconds=0.0)
Returns a DateTimeDelta-object reflecting the given time
delta. Seconds can be given as float to indicate fractions.
"""
return DateTimeDelta(0,hours,minutes,seconds)
def gm2local(datetime):
""" gm2local(datetime)
Convert a DateTime instance holding UTC time to a DateTime
instance using local time.
"""
return localtime(datetime.gmticks())
def local2gm(datetime):
""" local2gm(datetime)
Convert a DateTime instance holding local time to a DateTime
instance using UTC time.
"""
return gmtime(datetime.ticks())
# Alias
gmt = utc
# Default value for DateTimeFromTJD's tjd_myriad parameter
current_myriad = localtime().tjd_myriad
def DateTimeFromTJD(tjd,tjd_myriad=current_myriad):
""" DateTimeFromTJD(tjd[,myriad])
Return a DateTime instance for the given Truncated Julian Day.
myriad defaults to the TJD myriad current at package import
time.
Note that this version of Truncated Julian Day number does
real truncation of important information. It's use is
discouraged and unsupported.
"""
return DateTimeFromAbsDays(tjd + tjd_myriad * 10000.0 - 1721425.0)
def DateTimeFromJDN(jdn):
""" DateTimeFromJDN(jdn)
Return a DateTime instance for the given Julian Day Number.
References:
-----------
Gregorian 2000-01-01 12:00:00 corresponds to JDN 2451545.0.
Gregorian 1858-11-17 00:00:00.00 corresponds to JDN 2400000.5; MJD 0.0.
Julian -4712-01-01 12:00:00.00 corresponds to JDN 0.0.
Gregorian -4713-11-24 12:00:00.00 corresponds to JDN 0.0.
"""
return DateTimeFromAbsDays(jdn - 1721425.5)
def DateTimeFromMJD(mjd):
""" DateTimeFromMJD(mjd)
Return a DateTime instance for the given Modified Julian Day
(MJD). The MJD is calculated the same way as the JDN except
that 1858-11-17 00:00:00.00 is taken as origin of the scale.
"""
return DateTimeFromAbsDays(mjd + 678575.0)
def DateTimeFrom(*args, **kws):
""" DateTimeFrom(*args, **kws)
Generic DateTime instance constructor. Can handle parsing
strings, numbers and keywords.
"""
if len(args) == 1:
# Single argument
arg = args[0]
argtype = type(arg)
if _isstring(arg):
import Parser
return Parser.DateTimeFromString(*args, **kws)
elif argtype is DateTimeType:
return arg
elif argtype is DateTimeDeltaType:
raise TypeError,'cannot convert DateTimeDelta to DateTime'
elif isinstance(arg, _pydatetime):
if arg.tzinfo is not None:
# Convert to UTC
offset = arg.utcoffset()
arg -= offset
return DateTime(
arg.year, arg.month, arg.day,
arg.hour, arg.minute,
arg.second + arg.microsecond * 1e-6)
elif isinstance(arg, _pydate):
# Note: _pydatetime are subclasses of _pydates !
return DateTime(arg.year, arg.month, arg.day)
else:
try:
value = float(arg)
except (TypeError, ValueError):
value = int(arg)
assert not kws
return DateTimeFromTicks(value)
elif len(args) > 1:
# More than one argument
if len(args) == 2 and _isstring(args[0]) and _isstring(args[1]):
# interpret as date and time string
import Parser
return Parser.DateTimeFromString(
args[0] + ' ' + args[1],
**kws)
# Assume the arguments are the same as for DateTime()
if 'defaultdate' in kws:
raise TypeError('defaultdate is only supported when parsing '
'strings or using keyword arguments only')
return DateTime(*args, **kws)
elif len(kws) > 0:
# Keyword arguments only; add defaults... today at 0:00:00
defaultdate = kws.get('defaultdate', None)
if defaultdate is None:
defaultdate = today()
hour = kws.get('hour', defaultdate.hour)
minute = kws.get('minute', defaultdate.minute)
second = kws.get('second', defaultdate.second)
day = kws.get('day', defaultdate.day)
month = kws.get('month', defaultdate.month)
year = kws.get('year', defaultdate.year)
return DateTime(year,month,day,hour,minute,second)
else:
raise TypeError(
'cannot convert date/time arguments to DateTime: '
'args=%r, kws=%r' % (args, kws))
def DateFrom(*args, **kws):
""" DateFrom(*args, **kws)
Generic DateTime instance constructor. Can handle parsing
strings, numbers and keywords, but only works on the date
part.
"""
if len(args) == 1:
# Single argument
arg = args[0]
argtype = type(arg)
if _isstring(arg):
import Parser
return Parser.DateFromString(*args, **kws)
elif argtype is DateTimeType:
return arg
elif argtype is DateTimeDeltaType:
raise TypeError,'cannot convert DateTimeDelta to DateTime'
elif isinstance(arg, _pydate):
# Note: _pydatetime are subclasses of _pydates !
return DateTime(arg.year, arg.month, arg.day)
else:
try:
value = float(arg)
except (TypeError, ValueError):
value = int(arg)
assert not kws
return DateTimeFromTicks(value)
elif len(args) > 1:
# Assume the arguments are the same as for DateTime()
return DateTime(*args, **kws)
elif len(kws) > 0:
# Keyword arguments; add defaults... today at 0:00:00
today = now()
day = kws.get('day',today.day)
month = kws.get('month',today.month)
year = kws.get('year',today.year)
return DateTime(year,month,day)
else:
raise TypeError(
'cannot convert date arguments to DateTime: args=%r, kws=%r' %
(args, kws))
def DateTimeDeltaFrom(*args, **kws):
""" DateTimeDeltaFrom(*args, **kws)
Generic DateTimeDelta instance constructor. Can handle parsing
strings, numbers and keywords.
"""
if len(args) == 1:
# Single argument
arg = args[0]
if _isstring(arg):
import Parser
return Parser.DateTimeDeltaFromString(*args, **kws)
elif type(arg) is DateTimeDeltaType:
return arg
elif type(arg) is DateTimeType:
raise TypeError,'cannot convert DateTime to DateTimeDelta'
elif isinstance(arg, _pytime):
if arg.tzinfo is not None:
# Convert to UTC
offset = arg.utcoffset()
# Another datetime module quirk: time - timedelta
# doesn't work, so we have to use a different method
# of adjusting the result
return DateTimeDelta(0,
arg.hour, arg.minute,
arg.second + arg.microsecond * 1e-6
- offset.seconds)
else:
return DateTimeDelta(0,
arg.hour, arg.minute,
arg.second + arg.microsecond * 1e-6)
elif isinstance(arg, _pytimedelta):
return DateTimeDelta(arg.days,
0, 0,
arg.seconds + arg.microseconds * 1e-6)
else:
try:
value = float(arg)
except TypeError:
value = int(arg)
assert not kws
return DateTimeDeltaFromSeconds(value)
elif len(args) > 1:
# Assume the arguments are the same as for DateTimeDelta()
return DateTimeDelta(*args, **kws)
elif len(kws) > 0:
# Keyword arguments; default: 00:00:00:00.00
hours = kws.get('hours',0)
minutes = kws.get('minutes',0)
seconds = kws.get('seconds',0.0)
days = kws.get('days',0)
return DateTimeDelta(days,hours,minutes,seconds)
else:
raise TypeError,'cannot convert arguments to DateTimeDelta'
def TimeDeltaFrom(*args, **kws):
""" TimeDeltaFrom(*args, **kws)
Generic TimeDelta instance constructor. Can handle parsing
strings, numbers and keywords.
"""
if len(args) > 1:
# Assume the arguments are the same as for TimeDelta(): without
# days part !
return DateTimeDelta(0, *args, **kws)
else:
# Otherwise treat the arguments just like for DateTimeDelta
# instances.
return DateTimeDeltaFrom(*args, **kws)
def DateFromTicks(ticks,
# Locals:
DateTime=DateTime,localtime=_time.localtime):
""" DateFromTicks(ticks)
Constructs a DateTime instance pointing to the local time date
at 00:00:00.00 (midnight) indicated by the given ticks value.
The time part is ignored.
"""
return DateTime(*localtime(ticks)[:3])
def TimestampFromTicks(ticks,
# Locals:
DateTime=DateTime,localtime=_time.localtime):
""" TimestampFromTicks(ticks)
Constructs a DateTime instance pointing to the local date and
time indicated by the given ticks value.
"""
return DateTime(*localtime(ticks)[:6])
def TimeFromTicks(ticks,
# Locals:
DateTimeDelta=DateTimeDelta,localtime=_time.localtime):
""" TimeFromTicks(ticks)
Constructs a DateTimeDelta instance pointing to the local time
indicated by the given ticks value. The date part is ignored.
"""
return DateTimeDelta(0, *localtime(ticks)[3:6])
# Aliases
utctime = gmtime
utc2local = gm2local
local2utc = local2gm
DateTimeFromTicks = localtime
Date = DateTime
Time = TimeDelta
Timestamp = DateTime
TimeFrom = TimeDeltaFrom
TimestampFrom = DateTimeFrom
GregorianDateTime = DateTime
GregorianDate = Date
JulianDate = JulianDateTime
### For backward compatibility (these are depreciated):
def gmticks(datetime):
"""gmticks(datetime)
[DEPRECIATED: use the .gmticks() method]
Returns a ticks value based on the values stored in
datetime under the assumption that they are given in UTC,
rather than local time.
"""
return datetime.gmticks()
# Alias
utcticks = gmticks
def tz_offset(datetime,
# Locals:
oneSecond=oneSecond):
"""tz_offset(datetime)
[DEPRECIATED: use the .gmtoffset() method]
Returns a DateTimeDelta instance representing the UTC
offset for datetime assuming that the stored values refer
to local time. If you subtract this value from datetime,
you'll get UTC time.
"""
return datetime.gmtoffset()
### Constants (only English; see Locale.py for other languages)
# Weekdays
Monday = 0
Tuesday = 1
Wednesday = 2
Thursday = 3
Friday = 4
Saturday = 5
Sunday = 6
# as mapping
Weekday = {'Saturday': 5, 6: 'Sunday', 'Sunday': 6, 'Thursday': 3,
'Wednesday': 2, 'Friday': 4, 'Tuesday': 1, 'Monday': 0,
5: 'Saturday', 4: 'Friday', 3: 'Thursday', 2: 'Wednesday',
1: 'Tuesday', 0: 'Monday'}
# Months
January = 1
February = 2
March = 3
April = 4
May = 5
June = 6
July = 7
August = 8
September = 9
October = 10
November = 11
December = 12
# as mapping
Month = {2: 'February', 3: 'March', None: 0, 'July': 7, 11: 'November',
'December': 12, 'June': 6, 'January': 1, 'September': 9, 'August':
8, 'March': 3, 'November': 11, 'April': 4, 12: 'December', 'May':
5, 10: 'October', 9: 'September', 8: 'August', 7: 'July', 6:
'June', 5: 'May', 4: 'April', 'October': 10, 'February': 2, 1:
'January', 0: None}
# Limits (also see MAX_ABSDATE_VALUE / MAX_DATETIMEDELTA_SECONDS in
# mxDateTime.c)
if _sys.maxint > 2147483648:
MaxDateTime = DateTimeFromAbsDateTime(9223372036854775234)
MinDateTime = DateTimeFromAbsDateTime(-9223372036854775234)
MaxDateTimeDelta = DateTimeDeltaFromSeconds(9007199254740992.0)
else:
MaxDateTime = DateTimeFromAbsDateTime(2147483090)
MinDateTime = DateTimeFromAbsDateTime(-2147483090)
MaxDateTimeDelta = DateTimeDeltaFromSeconds(185542587100800.0)
MinDateTimeDelta = -MaxDateTimeDelta
###
class RelativeDateTime:
"""RelativeDateTime(years=0,months=0,days=0,
hours=0,minutes=0,seconds=0,
year=0,month=0,day=0,
hour=None,minute=None,second=None,
weekday=None,weeks=None)
Returns a RelativeDateTime instance for the specified relative
time. The constructor handles keywords, so you'll only have to
give those parameters which should be changed when you add the
relative to an absolute DateTime instance.
Adding RelativeDateTime instances is supported with the
following rules: deltas will be added together, right side
absolute values override left side ones.
Adding RelativeDateTime instances to DateTime instances will
return DateTime instances with the appropriate calculations
applied, e.g. to get a DateTime instance for the first of next
month, you'd call now() + RelativeDateTime(months=+1,day=1).
"""
years = 0
months = 0
days = 0
year = None
month = 0
day = 0
hours = 0
minutes = 0
seconds = 0
hour = None
minute = None
second = None
weekday = None
# cached hash value
_hash = None
# For Zope security:
__roles__ = None
__allow_access_to_unprotected_subobjects__ = 1
def __init__(self,
years=0,months=0,days=0,
hours=0,minutes=0,seconds=0,
year=None,month=None,day=None,
hour=None,minute=None,second=None,
weekday=None,weeks=0):
self.years = years
self.months = months
self.days = days + weeks*7
self.year = year
self.month = month
self.day = day
self.hours = hours
self.minutes = minutes
self.seconds = seconds
self.hour = hour
self.minute = minute
self.second = second
if weekday is not None:
# Make sure we've got a 2-tuple
assert len(weekday) == 2
self.weekday = weekday
def __add__(self,other,
# Locals:
isinstance=isinstance):
if isinstance(other,RelativeDateTime):
# RelativeDateTime (self) + RelativeDateTime (other)
r = RelativeDateTime()
# date deltas
r.years = self.years + other.years
r.months = self.months + other.months
r.days = self.days + other.days
# absolute entries of other override those in self, if given
r.year = other.year or self.year
r.month = other.month or self.month
r.day = other.day or self.day
r.weekday = other.weekday or self.weekday
# time deltas
r.hours = self.hours + other.hours
r.minutes = self.minutes + other.minutes
r.seconds = self.seconds + other.seconds
# absolute entries of other override those in self, if given
r.hour = other.hour or self.hour
r.minute = other.minute or self.minute
r.second = other.second or self.second
return r
else:
raise TypeError,"can't add the two types"
def __radd__(self,other,
# Locals:
isinstance=isinstance,DateTimeType=DateTimeType,
DateTime=DateTime,DateTimeDelta=DateTimeDelta):
if isinstance(other,DateTimeType):
# DateTime (other) + RelativeDateTime (self)
# date
if self.year is None:
year = other.year + self.years
else:
year = self.year + self.years
if self.month is None:
month = other.month + self.months
else:
month = self.month + self.months
if self.day is None:
day = other.day
else:
day = self.day
if day < 0:
# fix negative day values
month = month + 1
day = day + 1
day = day + self.days
# time
if self.hour is None:
hour = other.hour + self.hours
else:
hour = self.hour + self.hours
if self.minute is None:
minute = other.minute + self.minutes
else:
minute = self.minute + self.minutes
if self.second is None:
second = other.second + self.seconds
else:
second = self.second + self.seconds
# Refit into proper ranges:
if month < 1 or month > 12:
month = month - 1
yeardelta, monthdelta = divmod(month, 12)
year = year + yeardelta
month = monthdelta + 1
# Make sure we have integers
year = int(year)
month = int(month)
day = int(day)
if self.weekday is None:
return DateTime(year, month, 1) + \
DateTimeDelta(day-1,hour,minute,second)
# Adjust to the correct weekday
day_of_week,index = self.weekday
d = DateTime(year, month, 1) + \
DateTimeDelta(day-1,hour,minute,second)
if index == 0:
# 0 index: next weekday if no match
return d + (day_of_week - d.day_of_week)
elif index > 0:
# positive index (1 == first weekday of month)
first = d - (d.day - 1)
diff = day_of_week - first.day_of_week
if diff >= 0:
return first + (diff + (index-1) * 7)
else:
return first + (diff + index * 7)
else:
# negative index (-1 == last weekday of month)
last = d + (d.days_in_month - d.day)
diff = day_of_week - last.day_of_week
if diff <= 0:
return last + (diff + (index+1) * 7)
else:
return last + (diff + index * 7)
else:
raise TypeError,"can't add the two types"
def __sub__(self,other):
if isinstance(other,RelativeDateTime):
# RelativeDateTime (self) - RelativeDateTime (other)
r = RelativeDateTime()
# date deltas
r.years = self.years - other.years
r.months = self.months - other.months
r.days = self.days - other.days
# absolute entries of other override those in self, if given
r.year = other.year or self.year
r.month = other.month or self.month
r.day = other.day or self.day
r.weekday = other.weekday or self.weekday
# time deltas
r.hours = self.hours - other.hours
r.minutes = self.minutes - other.minutes
r.seconds = self.seconds - other.seconds
# absolute entries of other override those in self, if given
r.hour = other.hour or self.hour
r.minute = other.minute or self.minute
r.second = other.second or self.second
return r
else:
raise TypeError,"can't subtract the two types"
def __rsub__(self,other,
# Locals:
isinstance=isinstance,DateTimeType=DateTimeType):
if isinstance(other,DateTimeType):
# DateTime (other) - RelativeDateTime (self)
return other + self.__neg__()
else:
raise TypeError,"can't subtract the two types"
def __neg__(self):
# - RelativeDateTime(self)
r = RelativeDateTime()
# negate date deltas
r.years = - self.years
r.months = - self.months
r.days = - self.days
# absolute entries don't change
r.year = self.year
r.month = self.month
r.day = self.day
r.weekday = self.weekday
# negate time deltas
r.hours = - self.hours
r.minutes = - self.minutes
r.seconds = - self.seconds
# absolute entries don't change
r.hour = self.hour
r.minute = self.minute
r.second = self.second
return r
def __nonzero__(self):
# RelativeDateTime instances are considered false in case
# they do not define any alterations
if (self.year is None and
self.years == 0 and
self.month is None and
self.months == 0 and
self.day is None and
self.weekday is None and
self.days == 0 and
self.hour is None and
self.hours == 0 and
self.minute is None and
self.minutes == 0 and
self.second is None and
self.seconds == 0):
return 0
else:
return 1
def __mul__(self,other):
# RelativeDateTime (self) * Number (other)
factor = float(other)
r = RelativeDateTime()
# date deltas
r.years = factor * self.years
r.months = factor * self.months
r.days = factor * self.days
# time deltas
r.hours = factor * self.hours
r.minutes = factor * self.minutes
r.seconds = factor * self.seconds
return r
__rmul__ = __mul__
def __div__(self,other):
# RelativeDateTime (self) / Number (other)
return self.__mul__(1/float(other))
def __eq__(self, other):
if isinstance(self, RelativeDateTime) and \
isinstance(other, RelativeDateTime):
# RelativeDateTime (self) == RelativeDateTime (other)
if (self.years == other.years and
self.months == other.months and
self.days == other.days and
self.year == other.year and
self.day == other.day and
self.hours == other.hours and
self.minutes == other.minutes and
self.seconds == other.seconds and
self.hour == other.hour and
self.minute == other.minute and
self.second == other.second and
self.weekday == other.weekday):
return 1
else:
return 0
else:
raise TypeError,"can't compare the two types"
def __hash__(self):
if self._hash is not None:
return self._hash
x = 1234
for value in (self.years, self.months, self.days,
self.year, self.day,
self.hours, self.minutes, self.seconds,
self.hour, self.minute, self.second,
self.weekday):
if value is None:
x = 135051820 ^ x
else:
x = hash(value) ^ x
self._hash = x
return x
def __str__(self):
l = []
append = l.append
# Format date part
if self.year is not None:
append('%04i-' % self.year)
elif self.years:
append('(%0+5i)-' % self.years)
else:
append('YYYY-')
if self.month is not None:
append('%02i-' % self.month)
elif self.months:
append('(%0+3i)-' % self.months)
else:
append('MM-')
if self.day is not None:
append('%02i' % self.day)
elif self.days:
append('(%0+3i)' % self.days)
else:
append('DD')
if self.weekday:
append(' %s:%i' % (Weekday[self.weekday[0]][:3],self.weekday[1]))
append(' ')
# Normalize relative time values to avoid fractions
hours = self.hours
minutes = self.minutes
seconds = self.seconds
hours_fraction = hours - int(hours)
minutes = minutes + hours_fraction * 60.0
minutes_fraction = minutes - int(minutes)
seconds = seconds + minutes_fraction * 6.0
seconds_fraction = seconds - int(seconds)
if 0:
# Normalize to standard time ranges
if seconds > 60.0:
extra_minutes, seconds = divmod(seconds, 60.0)
minutes = minutes + extra_minutes
elif seconds < -60.0:
extra_minutes, seconds = divmod(seconds, -60.0)
minutes = minutes - extra_minutes
if minutes >= 60.0:
extra_hours, minutes = divmod(minutes, 60.0)
hours = hours + extra_hours
elif minutes <= -60.0:
extra_hours, minutes = divmod(minutes, -60.0)
hours = hours - extra_hours
# Format time part
if self.hour is not None:
append('%02i:' % self.hour)
elif hours:
append('(%0+3i):' % hours)
else:
append('HH:')
if self.minute is not None:
append('%02i:' % self.minute)
elif minutes:
append('(%0+3i):' % minutes)
else:
append('MM:')
if self.second is not None:
append('%02i' % self.second)
elif seconds:
append('(%0+3i)' % seconds)
else:
append('SS')
return ''.join(l)
def __repr__(self):
return "<%s instance for '%s' at 0x%x>" % (
self.__class__.__name__,
self.__str__(),
id(self))
# Alias
RelativeDate = RelativeDateTime
def RelativeDateTimeFrom(*args, **kws):
""" RelativeDateTimeFrom(*args, **kws)
Generic RelativeDateTime instance constructor. Can handle
parsing strings and keywords.
"""
if len(args) == 1:
# Single argument