comparison env/lib/python3.7/site-packages/isodate/isotzinfo.py @ 0:26e78fe6e8c4 draft

"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
author shellac
date Sat, 02 May 2020 07:14:21 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:26e78fe6e8c4
1 ##############################################################################
2 # Copyright 2009, Gerhard Weis
3 # All rights reserved.
4 #
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions are met:
7 #
8 # * Redistributions of source code must retain the above copyright notice,
9 # this list of conditions and the following disclaimer.
10 # * Redistributions in binary form must reproduce the above copyright notice,
11 # this list of conditions and the following disclaimer in the documentation
12 # and/or other materials provided with the distribution.
13 # * Neither the name of the authors nor the names of its contributors
14 # may be used to endorse or promote products derived from this software
15 # without specific prior written permission.
16 #
17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 # CONTRACT, STRICT LIABILITY, OR TORT
26 ##############################################################################
27 '''
28 This module provides an ISO 8601:2004 time zone info parser.
29
30 It offers a function to parse the time zone offset as specified by ISO 8601.
31 '''
32 import re
33
34 from isodate.isoerror import ISO8601Error
35 from isodate.tzinfo import UTC, FixedOffset, ZERO
36
37 TZ_REGEX = r"(?P<tzname>(Z|(?P<tzsign>[+-])"\
38 r"(?P<tzhour>[0-9]{2})(:?(?P<tzmin>[0-9]{2}))?)?)"
39
40 TZ_RE = re.compile(TZ_REGEX)
41
42
43 def build_tzinfo(tzname, tzsign='+', tzhour=0, tzmin=0):
44 '''
45 create a tzinfo instance according to given parameters.
46
47 tzname:
48 'Z' ... return UTC
49 '' | None ... return None
50 other ... return FixedOffset
51 '''
52 if tzname is None or tzname == '':
53 return None
54 if tzname == 'Z':
55 return UTC
56 tzsign = ((tzsign == '-') and -1) or 1
57 return FixedOffset(tzsign * tzhour, tzsign * tzmin, tzname)
58
59
60 def parse_tzinfo(tzstring):
61 '''
62 Parses ISO 8601 time zone designators to tzinfo objecs.
63
64 A time zone designator can be in the following format:
65 no designator indicates local time zone
66 Z UTC
67 +-hhmm basic hours and minutes
68 +-hh:mm extended hours and minutes
69 +-hh hours
70 '''
71 match = TZ_RE.match(tzstring)
72 if match:
73 groups = match.groupdict()
74 return build_tzinfo(groups['tzname'], groups['tzsign'],
75 int(groups['tzhour'] or 0),
76 int(groups['tzmin'] or 0))
77 raise ISO8601Error('%s not a valid time zone info' % tzstring)
78
79
80 def tz_isoformat(dt, format='%Z'):
81 '''
82 return time zone offset ISO 8601 formatted.
83 The various ISO formats can be chosen with the format parameter.
84
85 if tzinfo is None returns ''
86 if tzinfo is UTC returns 'Z'
87 else the offset is rendered to the given format.
88 format:
89 %h ... +-HH
90 %z ... +-HHMM
91 %Z ... +-HH:MM
92 '''
93 tzinfo = dt.tzinfo
94 if (tzinfo is None) or (tzinfo.utcoffset(dt) is None):
95 return ''
96 if tzinfo.utcoffset(dt) == ZERO and tzinfo.dst(dt) == ZERO:
97 return 'Z'
98 tdelta = tzinfo.utcoffset(dt)
99 seconds = tdelta.days * 24 * 60 * 60 + tdelta.seconds
100 sign = ((seconds < 0) and '-') or '+'
101 seconds = abs(seconds)
102 minutes, seconds = divmod(seconds, 60)
103 hours, minutes = divmod(minutes, 60)
104 if hours > 99:
105 raise OverflowError('can not handle differences > 99 hours')
106 if format == '%Z':
107 return '%s%02d:%02d' % (sign, hours, minutes)
108 elif format == '%z':
109 return '%s%02d%02d' % (sign, hours, minutes)
110 elif format == '%h':
111 return '%s%02d' % (sign, hours)
112 raise ValueError('unknown format string "%s"' % format)