aboutsummaryrefslogtreecommitdiffhomepage
path: root/libs/aniso8601/timezone.py
blob: 3b66105bd483628f6c1c56a10ff0ed2ad7e1ab4b (plain)
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
# -*- coding: utf-8 -*-

# Copyright (c) 2021, 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.

from aniso8601.builders.python import PythonTimeBuilder
from aniso8601.compat import is_string
from aniso8601.exceptions import ISOFormatError


def parse_timezone(tzstr, builder=PythonTimeBuilder):
    # tzstr can be Z, ±hh:mm, ±hhmm, ±hh
    if is_string(tzstr) is False:
        raise ValueError("Time zone must be string.")

    if len(tzstr) == 1 and tzstr[0] == "Z":
        return builder.build_timezone(negative=False, Z=True, name=tzstr)
    elif len(tzstr) == 6:
        # ±hh:mm
        hourstr = tzstr[1:3]
        minutestr = tzstr[4:6]

        if tzstr[0] == "-" and hourstr == "00" and minutestr == "00":
            raise ISOFormatError("Negative ISO 8601 time offset must not " "be 0.")
    elif len(tzstr) == 5:
        # ±hhmm
        hourstr = tzstr[1:3]
        minutestr = tzstr[3:5]

        if tzstr[0] == "-" and hourstr == "00" and minutestr == "00":
            raise ISOFormatError("Negative ISO 8601 time offset must not " "be 0.")
    elif len(tzstr) == 3:
        # ±hh
        hourstr = tzstr[1:3]
        minutestr = None

        if tzstr[0] == "-" and hourstr == "00":
            raise ISOFormatError("Negative ISO 8601 time offset must not " "be 0.")
    else:
        raise ISOFormatError('"{0}" is not a valid ISO 8601 time offset.'.format(tzstr))

    for componentstr in [hourstr, minutestr]:
        if componentstr is not None:
            if componentstr.isdigit() is False:
                raise ISOFormatError(
                    '"{0}" is not a valid ISO 8601 time offset.'.format(tzstr)
                )

    if tzstr[0] == "+":
        return builder.build_timezone(
            negative=False, hh=hourstr, mm=minutestr, name=tzstr
        )

    if tzstr[0] == "-":
        return builder.build_timezone(
            negative=True, hh=hourstr, mm=minutestr, name=tzstr
        )

    raise ISOFormatError('"{0}" is not a valid ISO 8601 time offset.'.format(tzstr))