File: __init__.py

package info (click to toggle)
python-aniso8601 10.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 544 kB
  • sloc: python: 7,776; makefile: 3
file content (614 lines) | stat: -rw-r--r-- 17,975 bytes parent folder | download
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
# -*- coding: utf-8 -*-

# Copyright (c) 2025, Brandon Nielsen
# All rights reserved.
#
# This software may be modified and distributed under the terms
# of the BSD license.  See the LICENSE file for details.

import calendar
from collections import namedtuple

from aniso8601.exceptions import (
    DayOutOfBoundsError,
    HoursOutOfBoundsError,
    ISOFormatError,
    LeapSecondError,
    MidnightBoundsError,
    MinutesOutOfBoundsError,
    MonthOutOfBoundsError,
    SecondsOutOfBoundsError,
    WeekOutOfBoundsError,
    YearOutOfBoundsError,
)

DateTuple = namedtuple("Date", ["YYYY", "MM", "DD", "Www", "D", "DDD"])
TimeTuple = namedtuple("Time", ["hh", "mm", "ss", "tz"])
DatetimeTuple = namedtuple("Datetime", ["date", "time"])
DurationTuple = namedtuple(
    "Duration", ["PnY", "PnM", "PnW", "PnD", "TnH", "TnM", "TnS"]
)
IntervalTuple = namedtuple("Interval", ["start", "end", "duration"])
RepeatingIntervalTuple = namedtuple("RepeatingInterval", ["R", "Rnn", "interval"])
TimezoneTuple = namedtuple("Timezone", ["negative", "Z", "hh", "mm", "name"])

Limit = namedtuple(
    "Limit",
    [
        "casterrorstring",
        "min",
        "max",
        "rangeexception",
        "rangeerrorstring",
        "rangefunc",
    ],
)


def cast(
    value,
    castfunction,
    caughtexceptions=(ValueError,),
    thrownexception=ISOFormatError,
    thrownmessage=None,
):
    try:
        result = castfunction(value)
    except caughtexceptions:
        raise thrownexception(thrownmessage)

    return result


def range_check(valuestr, limit):
    # Returns cast value if in range, raises defined exceptions on failure
    if valuestr is None:
        return None

    if "." in valuestr:
        castfunc = float
    else:
        castfunc = int

    value = cast(valuestr, castfunc, thrownmessage=limit.casterrorstring)

    if limit.min is not None and value < limit.min:
        raise limit.rangeexception(limit.rangeerrorstring)

    if limit.max is not None and value > limit.max:
        raise limit.rangeexception(limit.rangeerrorstring)

    return value


class BaseTimeBuilder(object):
    # Limit tuple format cast function, cast error string,
    # lower limit, upper limit, limit error string
    DATE_YYYY_LIMIT = Limit(
        "Invalid year string.",
        0000,
        9999,
        YearOutOfBoundsError,
        "Year must be between 1..9999.",
        range_check,
    )
    DATE_MM_LIMIT = Limit(
        "Invalid month string.",
        1,
        12,
        MonthOutOfBoundsError,
        "Month must be between 1..12.",
        range_check,
    )
    DATE_DD_LIMIT = Limit(
        "Invalid day string.",
        1,
        31,
        DayOutOfBoundsError,
        "Day must be between 1..31.",
        range_check,
    )
    DATE_WWW_LIMIT = Limit(
        "Invalid week string.",
        1,
        53,
        WeekOutOfBoundsError,
        "Week number must be between 1..53.",
        range_check,
    )
    DATE_D_LIMIT = Limit(
        "Invalid weekday string.",
        1,
        7,
        DayOutOfBoundsError,
        "Weekday number must be between 1..7.",
        range_check,
    )
    DATE_DDD_LIMIT = Limit(
        "Invalid ordinal day string.",
        1,
        366,
        DayOutOfBoundsError,
        "Ordinal day must be between 1..366.",
        range_check,
    )
    TIME_HH_LIMIT = Limit(
        "Invalid hour string.",
        0,
        24,
        HoursOutOfBoundsError,
        "Hour must be between 0..24 with 24 representing midnight.",
        range_check,
    )
    TIME_MM_LIMIT = Limit(
        "Invalid minute string.",
        0,
        59,
        MinutesOutOfBoundsError,
        "Minute must be between 0..59.",
        range_check,
    )
    TIME_SS_LIMIT = Limit(
        "Invalid second string.",
        0,
        60,
        SecondsOutOfBoundsError,
        "Second must be between 0..60 with 60 representing a leap second.",
        range_check,
    )
    TZ_HH_LIMIT = Limit(
        "Invalid timezone hour string.",
        0,
        23,
        HoursOutOfBoundsError,
        "Hour must be between 0..23.",
        range_check,
    )
    TZ_MM_LIMIT = Limit(
        "Invalid timezone minute string.",
        0,
        59,
        MinutesOutOfBoundsError,
        "Minute must be between 0..59.",
        range_check,
    )
    DURATION_PNY_LIMIT = Limit(
        "Invalid year duration string.",
        0,
        None,
        ISOFormatError,
        "Duration years component must be positive.",
        range_check,
    )
    DURATION_PNM_LIMIT = Limit(
        "Invalid month duration string.",
        0,
        None,
        ISOFormatError,
        "Duration months component must be positive.",
        range_check,
    )
    DURATION_PNW_LIMIT = Limit(
        "Invalid week duration string.",
        0,
        None,
        ISOFormatError,
        "Duration weeks component must be positive.",
        range_check,
    )
    DURATION_PND_LIMIT = Limit(
        "Invalid day duration string.",
        0,
        None,
        ISOFormatError,
        "Duration days component must be positive.",
        range_check,
    )
    DURATION_TNH_LIMIT = Limit(
        "Invalid hour duration string.",
        0,
        None,
        ISOFormatError,
        "Duration hours component must be positive.",
        range_check,
    )
    DURATION_TNM_LIMIT = Limit(
        "Invalid minute duration string.",
        0,
        None,
        ISOFormatError,
        "Duration minutes component must be positive.",
        range_check,
    )
    DURATION_TNS_LIMIT = Limit(
        "Invalid second duration string.",
        0,
        None,
        ISOFormatError,
        "Duration seconds component must be positive.",
        range_check,
    )
    INTERVAL_RNN_LIMIT = Limit(
        "Invalid duration repetition string.",
        0,
        None,
        ISOFormatError,
        "Duration repetition count must be positive.",
        range_check,
    )

    DATE_RANGE_DICT = {
        "YYYY": DATE_YYYY_LIMIT,
        "MM": DATE_MM_LIMIT,
        "DD": DATE_DD_LIMIT,
        "Www": DATE_WWW_LIMIT,
        "D": DATE_D_LIMIT,
        "DDD": DATE_DDD_LIMIT,
    }

    TIME_RANGE_DICT = {"hh": TIME_HH_LIMIT, "mm": TIME_MM_LIMIT, "ss": TIME_SS_LIMIT}

    DURATION_RANGE_DICT = {
        "PnY": DURATION_PNY_LIMIT,
        "PnM": DURATION_PNM_LIMIT,
        "PnW": DURATION_PNW_LIMIT,
        "PnD": DURATION_PND_LIMIT,
        "TnH": DURATION_TNH_LIMIT,
        "TnM": DURATION_TNM_LIMIT,
        "TnS": DURATION_TNS_LIMIT,
    }

    REPEATING_INTERVAL_RANGE_DICT = {"Rnn": INTERVAL_RNN_LIMIT}

    TIMEZONE_RANGE_DICT = {"hh": TZ_HH_LIMIT, "mm": TZ_MM_LIMIT}

    LEAP_SECONDS_SUPPORTED = False

    @classmethod
    def build_date(cls, YYYY=None, MM=None, DD=None, Www=None, D=None, DDD=None):
        raise NotImplementedError

    @classmethod
    def build_time(cls, hh=None, mm=None, ss=None, tz=None):
        raise NotImplementedError

    @classmethod
    def build_datetime(cls, date, time):
        raise NotImplementedError

    @classmethod
    def build_duration(
        cls, PnY=None, PnM=None, PnW=None, PnD=None, TnH=None, TnM=None, TnS=None
    ):
        raise NotImplementedError

    @classmethod
    def build_interval(cls, start=None, end=None, duration=None):
        # start, end, and duration are all tuples
        raise NotImplementedError

    @classmethod
    def build_repeating_interval(cls, R=None, Rnn=None, interval=None):
        # interval is a tuple
        raise NotImplementedError

    @classmethod
    def build_timezone(cls, negative=None, Z=None, hh=None, mm=None, name=""):
        raise NotImplementedError

    @classmethod
    def range_check_date(
        cls, YYYY=None, MM=None, DD=None, Www=None, D=None, DDD=None, rangedict=None
    ):
        if rangedict is None:
            rangedict = cls.DATE_RANGE_DICT

        if "YYYY" in rangedict:
            YYYY = rangedict["YYYY"].rangefunc(YYYY, rangedict["YYYY"])

        if "MM" in rangedict:
            MM = rangedict["MM"].rangefunc(MM, rangedict["MM"])

        if "DD" in rangedict:
            DD = rangedict["DD"].rangefunc(DD, rangedict["DD"])

        if "Www" in rangedict:
            Www = rangedict["Www"].rangefunc(Www, rangedict["Www"])

        if "D" in rangedict:
            D = rangedict["D"].rangefunc(D, rangedict["D"])

        if "DDD" in rangedict:
            DDD = rangedict["DDD"].rangefunc(DDD, rangedict["DDD"])

        if DD is not None:
            # Check calendar
            if DD > calendar.monthrange(YYYY, MM)[1]:
                raise DayOutOfBoundsError(
                    "{0} is out of range for {1}-{2}".format(DD, YYYY, MM)
                )

        if DDD is not None:
            if calendar.isleap(YYYY) is False and DDD == 366:
                raise DayOutOfBoundsError(
                    "{0} is only valid for leap year.".format(DDD)
                )

        return (YYYY, MM, DD, Www, D, DDD)

    @classmethod
    def range_check_time(cls, hh=None, mm=None, ss=None, tz=None, rangedict=None):
        # Used for midnight and leap second handling
        midnight = False  # Handle hh = '24' specially

        if rangedict is None:
            rangedict = cls.TIME_RANGE_DICT

        if "hh" in rangedict:
            try:
                hh = rangedict["hh"].rangefunc(hh, rangedict["hh"])
            except HoursOutOfBoundsError as e:
                if float(hh) > 24 and float(hh) < 25:
                    raise MidnightBoundsError("Hour 24 may only represent midnight.")

                raise e

        if "mm" in rangedict:
            mm = rangedict["mm"].rangefunc(mm, rangedict["mm"])

        if "ss" in rangedict:
            ss = rangedict["ss"].rangefunc(ss, rangedict["ss"])

        if hh is not None and hh == 24:
            midnight = True

        # Handle midnight range
        if midnight is True and (
            (mm is not None and mm != 0) or (ss is not None and ss != 0)
        ):
            raise MidnightBoundsError("Hour 24 may only represent midnight.")

        if cls.LEAP_SECONDS_SUPPORTED is True:
            if hh != 23 and mm != 59 and ss == 60:
                raise cls.TIME_SS_LIMIT.rangeexception(
                    cls.TIME_SS_LIMIT.rangeerrorstring
                )
        else:
            if hh == 23 and mm == 59 and ss == 60:
                # https://bitbucket.org/nielsenb/aniso8601/issues/10/sub-microsecond-precision-in-durations-is
                raise LeapSecondError("Leap seconds are not supported.")

            if ss == 60:
                raise cls.TIME_SS_LIMIT.rangeexception(
                    cls.TIME_SS_LIMIT.rangeerrorstring
                )

        return (hh, mm, ss, tz)

    @classmethod
    def range_check_duration(
        cls,
        PnY=None,
        PnM=None,
        PnW=None,
        PnD=None,
        TnH=None,
        TnM=None,
        TnS=None,
        rangedict=None,
    ):
        if rangedict is None:
            rangedict = cls.DURATION_RANGE_DICT

        if "PnY" in rangedict:
            PnY = rangedict["PnY"].rangefunc(PnY, rangedict["PnY"])

        if "PnM" in rangedict:
            PnM = rangedict["PnM"].rangefunc(PnM, rangedict["PnM"])

        if "PnW" in rangedict:
            PnW = rangedict["PnW"].rangefunc(PnW, rangedict["PnW"])

        if "PnD" in rangedict:
            PnD = rangedict["PnD"].rangefunc(PnD, rangedict["PnD"])

        if "TnH" in rangedict:
            TnH = rangedict["TnH"].rangefunc(TnH, rangedict["TnH"])

        if "TnM" in rangedict:
            TnM = rangedict["TnM"].rangefunc(TnM, rangedict["TnM"])

        if "TnS" in rangedict:
            TnS = rangedict["TnS"].rangefunc(TnS, rangedict["TnS"])

        return (PnY, PnM, PnW, PnD, TnH, TnM, TnS)

    @classmethod
    def range_check_repeating_interval(
        cls, R=None, Rnn=None, interval=None, rangedict=None
    ):
        if rangedict is None:
            rangedict = cls.REPEATING_INTERVAL_RANGE_DICT

        if "Rnn" in rangedict:
            Rnn = rangedict["Rnn"].rangefunc(Rnn, rangedict["Rnn"])

        return (R, Rnn, interval)

    @classmethod
    def range_check_timezone(
        cls, negative=None, Z=None, hh=None, mm=None, name="", rangedict=None
    ):
        if rangedict is None:
            rangedict = cls.TIMEZONE_RANGE_DICT

        if "hh" in rangedict:
            hh = rangedict["hh"].rangefunc(hh, rangedict["hh"])

        if "mm" in rangedict:
            mm = rangedict["mm"].rangefunc(mm, rangedict["mm"])

        return (negative, Z, hh, mm, name)

    @classmethod
    def _build_object(cls, parsetuple):
        # Given a TupleBuilder tuple, build the correct object
        if isinstance(parsetuple, DateTuple):
            return cls.build_date(
                YYYY=parsetuple.YYYY,
                MM=parsetuple.MM,
                DD=parsetuple.DD,
                Www=parsetuple.Www,
                D=parsetuple.D,
                DDD=parsetuple.DDD,
            )

        if isinstance(parsetuple, TimeTuple):
            return cls.build_time(
                hh=parsetuple.hh, mm=parsetuple.mm, ss=parsetuple.ss, tz=parsetuple.tz
            )

        if isinstance(parsetuple, DatetimeTuple):
            return cls.build_datetime(parsetuple.date, parsetuple.time)

        if isinstance(parsetuple, DurationTuple):
            return cls.build_duration(
                PnY=parsetuple.PnY,
                PnM=parsetuple.PnM,
                PnW=parsetuple.PnW,
                PnD=parsetuple.PnD,
                TnH=parsetuple.TnH,
                TnM=parsetuple.TnM,
                TnS=parsetuple.TnS,
            )

        if isinstance(parsetuple, IntervalTuple):
            return cls.build_interval(
                start=parsetuple.start, end=parsetuple.end, duration=parsetuple.duration
            )

        if isinstance(parsetuple, RepeatingIntervalTuple):
            return cls.build_repeating_interval(
                R=parsetuple.R, Rnn=parsetuple.Rnn, interval=parsetuple.interval
            )

        return cls.build_timezone(
            negative=parsetuple.negative,
            Z=parsetuple.Z,
            hh=parsetuple.hh,
            mm=parsetuple.mm,
            name=parsetuple.name,
        )

    @classmethod
    def _is_interval_end_concise(cls, endtuple):
        if isinstance(endtuple, TimeTuple):
            return True

        if isinstance(endtuple, DatetimeTuple):
            enddatetuple = endtuple.date
        else:
            enddatetuple = endtuple

        if enddatetuple.YYYY is None:
            return True

        return False

    @classmethod
    def _combine_concise_interval_tuples(cls, starttuple, conciseendtuple):
        starttimetuple = None
        startdatetuple = None

        endtimetuple = None
        enddatetuple = None

        if isinstance(starttuple, DateTuple):
            startdatetuple = starttuple
        else:
            # Start is a datetime
            starttimetuple = starttuple.time
            startdatetuple = starttuple.date

        if isinstance(conciseendtuple, DateTuple):
            enddatetuple = conciseendtuple
        elif isinstance(conciseendtuple, DatetimeTuple):
            enddatetuple = conciseendtuple.date
            endtimetuple = conciseendtuple.time
        else:
            # Time
            endtimetuple = conciseendtuple

        if enddatetuple is not None:
            if enddatetuple.YYYY is None and enddatetuple.MM is None:
                newenddatetuple = DateTuple(
                    YYYY=startdatetuple.YYYY,
                    MM=startdatetuple.MM,
                    DD=enddatetuple.DD,
                    Www=enddatetuple.Www,
                    D=enddatetuple.D,
                    DDD=enddatetuple.DDD,
                )
            else:
                newenddatetuple = DateTuple(
                    YYYY=startdatetuple.YYYY,
                    MM=enddatetuple.MM,
                    DD=enddatetuple.DD,
                    Www=enddatetuple.Www,
                    D=enddatetuple.D,
                    DDD=enddatetuple.DDD,
                )

            if endtimetuple is None:
                return newenddatetuple

        if (starttimetuple is not None and starttimetuple.tz is not None) and (
            endtimetuple is not None and endtimetuple.tz != starttimetuple.tz
        ):
            # Copy the timezone across
            endtimetuple = TimeTuple(
                hh=endtimetuple.hh,
                mm=endtimetuple.mm,
                ss=endtimetuple.ss,
                tz=starttimetuple.tz,
            )

        if enddatetuple is not None and endtimetuple is not None:
            return TupleBuilder.build_datetime(newenddatetuple, endtimetuple)

        return TupleBuilder.build_datetime(startdatetuple, endtimetuple)


class TupleBuilder(BaseTimeBuilder):
    # Builder used to return the arguments as a tuple, cleans up some parse methods
    @classmethod
    def build_date(cls, YYYY=None, MM=None, DD=None, Www=None, D=None, DDD=None):

        return DateTuple(YYYY, MM, DD, Www, D, DDD)

    @classmethod
    def build_time(cls, hh=None, mm=None, ss=None, tz=None):
        return TimeTuple(hh, mm, ss, tz)

    @classmethod
    def build_datetime(cls, date, time):
        return DatetimeTuple(date, time)

    @classmethod
    def build_duration(
        cls, PnY=None, PnM=None, PnW=None, PnD=None, TnH=None, TnM=None, TnS=None
    ):

        return DurationTuple(PnY, PnM, PnW, PnD, TnH, TnM, TnS)

    @classmethod
    def build_interval(cls, start=None, end=None, duration=None):
        return IntervalTuple(start, end, duration)

    @classmethod
    def build_repeating_interval(cls, R=None, Rnn=None, interval=None):
        return RepeatingIntervalTuple(R, Rnn, interval)

    @classmethod
    def build_timezone(cls, negative=None, Z=None, hh=None, mm=None, name=""):
        return TimezoneTuple(negative, Z, hh, mm, name)