Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/aniso8601/timezone.py: 94%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# -*- coding: utf-8 -*-
3# Copyright (c) 2026, Brandon Nielsen
4# SPDX-License-Identifier: BSD-3-Clause
6from aniso8601.builders.python import PythonTimeBuilder
7from aniso8601.compat import is_string
8from aniso8601.exceptions import ISOFormatError
11def parse_timezone(tzstr, builder=PythonTimeBuilder):
12 # tzstr can be Z, ±hh:mm, ±hhmm, ±hh
13 if is_string(tzstr) is False:
14 raise ValueError("Time zone must be string.")
16 if len(tzstr) == 1 and tzstr[0] == "Z":
17 return builder.build_timezone(negative=False, Z=True, name=tzstr)
19 if len(tzstr) == 6:
20 # ±hh:mm
21 hourstr = tzstr[1:3]
22 minutestr = tzstr[4:6]
24 if tzstr[0] == "-" and hourstr == "00" and minutestr == "00":
25 raise ISOFormatError("Negative ISO 8601 time offset must not be 0.")
26 elif len(tzstr) == 5:
27 # ±hhmm
28 hourstr = tzstr[1:3]
29 minutestr = tzstr[3:5]
31 if tzstr[0] == "-" and hourstr == "00" and minutestr == "00":
32 raise ISOFormatError("Negative ISO 8601 time offset must not be 0.")
33 elif len(tzstr) == 3:
34 # ±hh
35 hourstr = tzstr[1:3]
36 minutestr = None
38 if tzstr[0] == "-" and hourstr == "00":
39 raise ISOFormatError("Negative ISO 8601 time offset must not be 0.")
40 else:
41 raise ISOFormatError('"{0}" is not a valid ISO 8601 time offset.'.format(tzstr))
43 for componentstr in [hourstr, minutestr]:
44 if componentstr is not None:
45 if componentstr.isdigit() is False:
46 raise ISOFormatError(
47 '"{0}" is not a valid ISO 8601 time offset.'.format(tzstr)
48 )
50 if tzstr[0] == "+":
51 return builder.build_timezone(
52 negative=False, hh=hourstr, mm=minutestr, name=tzstr
53 )
55 if tzstr[0] == "-":
56 return builder.build_timezone(
57 negative=True, hh=hourstr, mm=minutestr, name=tzstr
58 )
60 raise ISOFormatError('"{0}" is not a valid ISO 8601 time offset.'.format(tzstr))