File: Iso8601.py

package info (click to toggle)
python-pyvmomi 9.0.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 21,372 kB
  • sloc: python: 18,622; xml: 77; makefile: 3
file content (360 lines) | stat: -rw-r--r-- 12,212 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
#!/usr/bin/env python

# Copyright (c) 2009-2025 Broadcom. All Rights Reserved.
# The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.

# Tools for ISO 8601 parsing

import re
import time
from datetime import datetime, timedelta, tzinfo

from .five import iteritems

# Regular expression to parse a subset of ISO 8601 format
_dtExpr = re.compile(
    # XMLSchema datetime. Mandatory to have - and :
    # See: http://www.w3.org/TR/xmlschema-2/#isoformats
    # Note: python datetime cannot handle the following:
    #       - leap second, ie. 0-60 seconds (not 0-59)
    #       - BC (negative years)
    # year [-]0000..9999
    r'(?P<year>-?\d{4})'
    # month 01..12
    r'(-(?P<month>(0[1-9]|1[0-2]))'
    # day 01..31
    r'(-(?P<day>(0[1-9]|[1-2]\d|3[01])))?)?'
    # time separator 'T'
    r'(T'
    # hour 00..24
    r'(?P<hour>([01]\d|2[0-4]))'
    # minute 00..59
    r'((:(?P<minute>[0-5]\d))'
    # seconds 00..60 (leap second ok)
    r'(:(?P<second>([0-5]\d|60))'
    # microsecond. max 16 digits
    # - Should not allows trailing zeros. But python isoformat() put zeros
    #   after microseconds. Oh well, allows trailing zeros, quite harmless
    r'(\.(?P<microsecond>\d{1,16}))?)?)?'
    # UTC 'Z', or...
    r'((?P<tzutc>Z)'
    # tz [+-]00..13:0..59|14:00
    r'|((?P<tzhr>[+-](([0]\d)|(1[0-3])|(?P<tzlimit>)14))'
    r'(:(?P<tzmin>(?(tzlimit)00|([0-5]\d))))?))?'
    r')?$')

# Default date time val. Key should match the tags in _dtExpr
_dtExprKeyDefValMap = {
    'year': None,
    'month': 1,
    'day': 1,
    'hour': 0,
    'minute': 0,
    'second': 0,
    'microsecond': 0
}


class TZInfo(tzinfo):
    """ Timezone info class """

    timedelta0 = timedelta(hours=0)
    timedelta1 = timedelta(hours=1)

    def __init__(self, tzname='UTC', utcOffset=None, dst=None):
        self._tzname = tzname
        if not utcOffset:
            utcOffset = self.timedelta0
        self._utcOffset = utcOffset
        if not dst:
            dst = None
        self._dst = dst

    def utcoffset(self, dt):
        return self._utcOffset + self.dst(dt)

    def tzname(self, dt):
        return self._tzname

    def dst(self, dt):
        ret = self.timedelta0
        if self._dst:
            if self._dst[0] <= dt.replace(tzinfo=None) < self._dst[1]:
                ret = self.timedelta1
        return ret


class TZManager:
    """ Time zone manager """
    _tzInfos = {}

    @staticmethod
    def GetTZInfo(tzname='UTC', utcOffset=None, dst=None):
        """ Get / Add timezone info """
        key = (tzname, utcOffset, dst)
        tzInfo = TZManager._tzInfos.get(key)
        if not tzInfo:
            tzInfo = TZInfo(tzname, utcOffset, dst)
            TZManager._tzInfos[key] = tzInfo
        return tzInfo


def ParseISO8601(datetimeStr):
    """
    Parse ISO 8601 date time from string.
    Returns datetime if ok, None otherwise
    Note: Allows YYYY / YYYY-MM, but truncate YYYY -> YYYY-01-01,
                                              YYYY-MM -> YYYY-MM-01
    Truncate microsecond to most significant 6 digits
    """
    datetimeVal = None
    match = _dtExpr.match(datetimeStr)
    if match:
        try:
            dt = {}
            for key, defaultVal in iteritems(_dtExprKeyDefValMap):
                val = match.group(key)
                if val:
                    if key == 'microsecond':
                        val = val[:6] + '0' * (6 - len(val))
                    dt[key] = int(val)
                elif defaultVal:
                    dt[key] = defaultVal

            # Orig. XMLSchema don't allow all zeros year. But newer draft is ok
            # if dt['year'] == 0:
            #   # Year cannot be all zeros
            #   raise Exception('Year cannot be all zeros')

            # 24 is a special case.
            # It is actually represented as next day 00:00
            delta = None
            if dt.get('hour', 0) == 24:
                # Must be 24:00:00.0
                if (dt.get('minute', 0) == 0 and dt.get('second', 0) == 0
                        and dt.get('microsecond', 0) == 0):
                    dt['hour'] = 23
                    delta = timedelta(hours=1)
                else:
                    return None

            # Set tzinfo
            # TODO: dst
            tzInfo = None
            val = match.group('tzutc')
            if val:
                tzInfo = TZManager.GetTZInfo()
            else:
                val = match.group('tzhr')
                if val:
                    # tz hours offset
                    tzhr = int(val)
                    utcsign = val[0]

                    # tz minutes offset
                    tzmin = 0
                    val = match.group('tzmin')
                    if val:
                        tzmin = tzhr >= 0 and int(val) or -int(val)

                    # Better tzname (map UTC +-00:00 to UTC)
                    tzname = 'UTC'
                    if tzhr != 0 or tzmin != 0:
                        tzname += ' %s%02d:%02d' % (utcsign, abs(tzhr),
                                                    abs(tzmin))

                    tzInfo = TZManager.GetTZInfo(
                        tzname=tzname,
                        utcOffset=timedelta(hours=tzhr, minutes=tzmin))
            if tzInfo:
                dt['tzinfo'] = tzInfo

            datetimeVal = datetime(**dt)
            if delta:
                datetimeVal += delta
        except Exception:
            pass
    return datetimeVal


def GetUtcOffset():
    try:
        return time.localtime().tm_gmtoff
    except AttributeError:
        useAltZone = time.daylight and time.localtime().tm_isdst
        return -(time.altzone if useAltZone else time.timezone)


def ISO8601Format(dt):
    """
    Python datetime isoformat() has the following problems:
    - leave trailing 0 at the end of microseconds (violates XMLSchema rule)
    - tz print +00:00 instead of Z
    - Missing timezone offset for datetime without tzinfo
    """
    isoStr = dt.strftime('%Y-%m-%dT%H:%M:%S')
    if dt.microsecond:
        isoStr += ('.%06d' % dt.microsecond).rstrip('0')
    if dt.tzinfo:
        tz = dt.strftime('%z')
    else:
        utcOffset_minutes = GetUtcOffset() / 60
        tz = "%+.2d%.2d" % (utcOffset_minutes / 60,
                            (abs(utcOffset_minutes) % 60))
    if tz == '+0000':
        return isoStr + 'Z'
    elif tz:
        return isoStr + tz[:3] + ':' + tz[3:]
    else:
        # Local offset is unknown
        return isoStr + '-00:00'


# Testing
if __name__ == '__main__':
    # Valid entries
    for testStr in [
            '1971',  # 1971-01-01
            '1971-11',  # 1971-11-01
            '1971-11-02',
            '1971-11-02T23',
            '1971-11-02T23Z',
            '1971-11-02T23:04',
            '1971-11-02T23:04Z',
            '1971-11-02T23:04:15',
            '1971-11-02T23:04:15Z',
            '1971-11-02T23:04:15.1',
            '1971-11-02T23:04:15.01',
            '1971-11-02T23:04:15.023456',
            '1971-11-02T23:04:15.103456Z',
            '1971-11-02T23:04:15.123456+11',
            '1971-11-02T23:04:15.123456-11',
            '1971-11-02T23:04:15.123456+11:30',
            '1971-11-02T23:04:15.123456-11:30',
            '1971-11-02T23:04:15.123456+00:00',  # Same as Z
            '1971-11-02T23:04:15.123456-00:00',  # Same as Z
            '1971-01-02T23:04:15+14',
            '1971-01-02T23:04:15+14:00',
            '1971-01-02T23:04:15-14',
            '1971-01-02T23:04:15-14:00',

            # Valid: Truncate microsec to 6 digits
            '1971-01-02T23:04:15.123456891+11',
            '1971-01-02T24',  # 24 is valid. It should represent the 00:00 the
            # next day
            '1971-01-02T24:00',
            '1971-01-02T24:00:00',
            '1971-01-02T24:00:00.0',

            # Should NOT be valid but python isoformat adding trailing zeros
            '1971-01-02T23:04:15.123430',  # Microseconds ends in zero
            '1971-01-02T23:04:15.0',  # Microseconds ends in zero

            # Should be valid but python datetime don't support it
            # '2005-12-31T23:59:60Z', # Leap second
            # '-0001', # BC 1
    ]:
        dt = ParseISO8601(testStr)
        if dt is None:
            print('Failed to parse ({0})'.format(testStr))
            assert (False)

        # Make sure we can translate back
        isoformat = ISO8601Format(dt)
        dt1 = ParseISO8601(isoformat)
        if dt.tzinfo is None:
            dt = dt.replace(tzinfo=dt1.tzinfo)
        if dt1 != dt:
            print('ParseISO8601 -> ISO8601Format -> ParseISO8601 failed (%s)' %
                  testStr)
            assert (False)

        # Make sure we can parse python isoformat()
        dt2 = ParseISO8601(dt.isoformat())
        if dt2 is None:
            print('ParseISO8601("{0}".isoformat()) failed'.format(testStr))
            assert (False)

        print(testStr, '->', dt, isoformat)

    # Basic form
    for testStr in [
            '197111',  # 1971-11-01
            '19711102',
            '19711102T23',
            '19711102T23Z',
            '19711102T2304',
            '19711102T2304Z',
            '19711102T230415',
            '19711102T230415Z',
            '19711102T230415.123456',
            '19711102T230415.123456Z',
            '19711102T230415.123456+11',
            '19711102T230415.123456-11',
            '19711102T230415.123456+1130',
            '19711102T230415.123456-1130',
    ]:
        # Reject for now
        dt = ParseISO8601(testStr)
        if dt is not None:
            print('ParseISO8601 ({0}) should fail, but it did not'.format(testStr))
            assert (False)
        # print testStr, '->', dt
        # assert(dt != None)

    # Invalid entries
    for testStr in [
            # Xml schema reject year 0
            '0000',  # 0 years are not allowed
            '+0001',  # Leading + is not allowed
            '',  # Empty datetime str
            '09',  # Years must be at least 4 digits
            '1971-01-02T',  # T not follow by time
            '1971-01-02TZ',  # T not follow by time
            '1971-01-02T+10',  # T not follow by time
            '1971-01-02T-10',  # T not follow by time
            '1971-01-02T23:',  # extra :
            '1971-01-02T23:04:',  # extra :
            '1971-01-02T23:0d',  # 0d
            '1971-01-02T23:04:15.',  # Dot not follows by microsec
            '1971-01-02+12',  # time without T
            '1971Z',  # Z without T
            '1971-01-02T23:04:15.123456Z+11',  # Z follows by +
            '1971-01-02T23:04:15.123456Z-11',  # Z follows by -
            '1971-01-02T23:04:15.123456+:30',  # extra :
            '1971-01-02T23:04:15.123456+30:',  # extra :
            # Too many microseconds digits
            '1971-01-02T23:04:15.01234567890123456789',

            # Python isoformat leave trailing zeros in microseconds
            # Relax regular expression to accept it
            # '1971-01-02T23:04:15.123430', # Microseconds ends in zero
            # '1971-01-02T23:04:15.0', # Microseconds ends in zero

            # Timezone must be between +14 / -14
            '1971-01-02T23:04:15+15',
            '1971-01-02T23:04:15-15',
            '1971-01-02T23:04:15+14:01',
            '1971-01-02T23:04:15-14:01',

            # Mix basic form with extended format
            '197101-02T23:04:15.123456',
            '19710102T23:04:15.123456',
            '19710102T230415.123456+11:30',
            '1971-01-02T230415.123456',
            '1971-01-02T23:04:15.123456+1130',

            # Error captured by datetime class
            '1971-00-02',  # Less than 1 month
            '1971-13-02',  # Larger than 12 months
            '1971-01-00',  # Less than 1 day
            '1971-11-32',  # Larger than 30 days for Nov
            '1971-12-32',  # Larger than 31 days
            '1971-01-02T24:01',  # Larger than 23 hr
            '1971-01-02T23:61',  # Larger than 60 min
            '1971-01-02T23:60:61',  # Larger than 61 sec
    ]:
        dt = ParseISO8601(testStr)
        if dt is not None:
            print('ParseISO8601 ({0}) should fail, but it did not'.format(testStr))
            assert (False)