pyRFC3339 2.0.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pyRFC3339-2.0.1.dist-info/LICENSE.txt +19 -0
- pyRFC3339-2.0.1.dist-info/METADATA +58 -0
- pyRFC3339-2.0.1.dist-info/RECORD +9 -0
- pyRFC3339-2.0.1.dist-info/WHEEL +5 -0
- pyRFC3339-2.0.1.dist-info/top_level.txt +1 -0
- pyrfc3339/__init__.py +19 -0
- pyrfc3339/generator.py +70 -0
- pyrfc3339/parser.py +94 -0
- pyrfc3339/utils.py +22 -0
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
Copyright (c) 2024 Kurt Raschke
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
5
|
+
in the Software without restriction, including without limitation the rights
|
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
8
|
+
furnished to do so, subject to the following conditions:
|
|
9
|
+
|
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
|
11
|
+
all copies or substantial portions of the Software.
|
|
12
|
+
|
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
19
|
+
THE SOFTWARE.
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: pyRFC3339
|
|
3
|
+
Version: 2.0.1
|
|
4
|
+
Summary: Generate and parse RFC 3339 timestamps
|
|
5
|
+
Home-page: https://github.com/kurtraschke/pyRFC3339
|
|
6
|
+
Author: Kurt Raschke
|
|
7
|
+
Author-email: kurt@kurtraschke.com
|
|
8
|
+
License: MIT
|
|
9
|
+
Keywords: rfc 3339 timestamp
|
|
10
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
13
|
+
Classifier: Programming Language :: Python
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Topic :: Internet
|
|
16
|
+
License-File: LICENSE.txt
|
|
17
|
+
|
|
18
|
+
Description
|
|
19
|
+
===========
|
|
20
|
+
|
|
21
|
+
.. image:: https://github.com/kurtraschke/pyRFC3339/actions/workflows/test-python.yml/badge.svg
|
|
22
|
+
:target: https://github.com/kurtraschke/pyRFC3339/actions/workflows/test-python.yml
|
|
23
|
+
:alt: Build Status
|
|
24
|
+
|
|
25
|
+
.. image:: https://readthedocs.org/projects/pyrfc3339/badge/?version=latest
|
|
26
|
+
:target: https://pyrfc3339.readthedocs.io/en/latest/?badge=latest
|
|
27
|
+
:alt: Documentation Status
|
|
28
|
+
|
|
29
|
+
pyRFC3339 parses and generates :RFC:`3339`-compliant timestamps using `Python <https://www.python.org/>`_ `datetime.datetime <https://docs.python.org/3/library/datetime.html#datetime-objects>`_ objects.
|
|
30
|
+
|
|
31
|
+
>>> from pyrfc3339 import generate, parse
|
|
32
|
+
>>> from datetime import datetime, timezone
|
|
33
|
+
>>> generate(datetime.now(timezone.utc)) #doctest:+ELLIPSIS
|
|
34
|
+
'...T...Z'
|
|
35
|
+
>>> parse('2009-01-01T10:01:02Z')
|
|
36
|
+
datetime.datetime(2009, 1, 1, 10, 1, 2, tzinfo=datetime.timezone.utc)
|
|
37
|
+
>>> parse('2009-01-01T14:01:02-04:00')
|
|
38
|
+
datetime.datetime(2009, 1, 1, 14, 1, 2, tzinfo=datetime.timezone(datetime.timedelta(days=-1, seconds=72000), '<UTC-04:00>'))
|
|
39
|
+
|
|
40
|
+
Installation
|
|
41
|
+
============
|
|
42
|
+
|
|
43
|
+
To install the latest version from `PyPI <https://pypi.python.org/pypi>`_:
|
|
44
|
+
|
|
45
|
+
``$ pip install pyRFC3339``
|
|
46
|
+
|
|
47
|
+
To install the latest development version:
|
|
48
|
+
|
|
49
|
+
``$ pip install https://github.com/kurtraschke/pyRFC3339/tarball/master#egg=pyRFC3339-dev``
|
|
50
|
+
|
|
51
|
+
To build the documentation with Sphinx:
|
|
52
|
+
|
|
53
|
+
#. ``$ pip install -r docs/requirements.txt``
|
|
54
|
+
#. ``$ sphinx-build -M html docs/source/ docs/build``
|
|
55
|
+
|
|
56
|
+
The documentation is also available online at:
|
|
57
|
+
|
|
58
|
+
``https://pyrfc3339.readthedocs.io/``
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
pyrfc3339/__init__.py,sha256=0eKGeQYscD4ZWToyZ5IZynBzUjrDKPgYAvLfO-_XAL0,658
|
|
2
|
+
pyrfc3339/generator.py,sha256=AMSlPRzLQ4oyfCwnMMEkgACKt21KhnQiDJVY-ONKtKA,2281
|
|
3
|
+
pyrfc3339/parser.py,sha256=g2QYL76wKfkQ_VOYgGJBHtZ1ML8EG2WFpAtZXQnhPEA,3565
|
|
4
|
+
pyrfc3339/utils.py,sha256=e4XL78hpystwCRubiuw33d7Gg4tslzsZzhQFPzlAFgU,527
|
|
5
|
+
pyRFC3339-2.0.1.dist-info/LICENSE.txt,sha256=7ztWK4RatorBTc4ZZX1RNIiy16pEBQgJ9GzwTtBvyYY,1056
|
|
6
|
+
pyRFC3339-2.0.1.dist-info/METADATA,sha256=bXAMeXcrGROCM9yfFqL111dcZdZlJ17v6ksUQyM8Erw,2088
|
|
7
|
+
pyRFC3339-2.0.1.dist-info/WHEEL,sha256=P9jw-gEje8ByB7_hXoICnHtVCrEwMQh-630tKvQWehc,91
|
|
8
|
+
pyRFC3339-2.0.1.dist-info/top_level.txt,sha256=2oJKzRP6wgrj8j7WsDqOVRAduxrIXvT1_DaW3LGR78E,10
|
|
9
|
+
pyRFC3339-2.0.1.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
pyrfc3339
|
pyrfc3339/__init__.py
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"""
|
|
2
|
+
pyRFC3339 parses and generates :RFC:`3339`-compliant timestamps using Python
|
|
3
|
+
:class:`datetime.datetime` objects.
|
|
4
|
+
|
|
5
|
+
>>> from pyrfc3339 import generate, parse
|
|
6
|
+
>>> from datetime import datetime, timezone
|
|
7
|
+
>>> generate(datetime.now(timezone.utc)) #doctest:+ELLIPSIS
|
|
8
|
+
'...T...Z'
|
|
9
|
+
>>> parse('2009-01-01T10:01:02Z')
|
|
10
|
+
datetime.datetime(2009, 1, 1, 10, 1, 2, tzinfo=datetime.timezone.utc)
|
|
11
|
+
>>> parse('2009-01-01T14:01:02-04:00')
|
|
12
|
+
datetime.datetime(2009, 1, 1, 14, 1, 2, tzinfo=datetime.timezone(datetime.timedelta(days=-1, seconds=72000), '<UTC-04:00>'))
|
|
13
|
+
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
from pyrfc3339.generator import generate
|
|
17
|
+
from pyrfc3339.parser import parse
|
|
18
|
+
|
|
19
|
+
__all__ = ["generate", "parse"]
|
pyrfc3339/generator.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import datetime
|
|
2
|
+
|
|
3
|
+
from pyrfc3339.utils import format_timezone
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def generate(dt, utc=True, accept_naive=False, microseconds=False):
|
|
7
|
+
"""
|
|
8
|
+
Generate an :RFC:`3339`-formatted timestamp from a
|
|
9
|
+
:class:`datetime.datetime`.
|
|
10
|
+
|
|
11
|
+
>>> from datetime import datetime, timezone
|
|
12
|
+
>>> from zoneinfo import ZoneInfo
|
|
13
|
+
>>> generate(datetime(2009, 1, 1, 12, 59, 59, 0, timezone.utc))
|
|
14
|
+
'2009-01-01T12:59:59Z'
|
|
15
|
+
|
|
16
|
+
The timestamp will use UTC unless `utc=False` is specified, in which case
|
|
17
|
+
it will use the timezone from the :class:`datetime.datetime`'s
|
|
18
|
+
:attr:`tzinfo` parameter.
|
|
19
|
+
|
|
20
|
+
>>> eastern = ZoneInfo('US/Eastern')
|
|
21
|
+
>>> dt = datetime(2009, 1, 1, 12, 59, 59, tzinfo=eastern)
|
|
22
|
+
>>> generate(dt)
|
|
23
|
+
'2009-01-01T17:59:59Z'
|
|
24
|
+
>>> generate(dt, utc=False)
|
|
25
|
+
'2009-01-01T12:59:59-05:00'
|
|
26
|
+
|
|
27
|
+
Unless `accept_naive=True` is specified, the `datetime` must not be naive.
|
|
28
|
+
|
|
29
|
+
>>> generate(datetime(2009, 1, 1, 12, 59, 59, 0))
|
|
30
|
+
Traceback (most recent call last):
|
|
31
|
+
...
|
|
32
|
+
ValueError: naive datetime and accept_naive is False
|
|
33
|
+
|
|
34
|
+
>>> generate(datetime(2009, 1, 1, 12, 59, 59, 0), accept_naive=True)
|
|
35
|
+
'2009-01-01T12:59:59Z'
|
|
36
|
+
|
|
37
|
+
If `accept_naive=True` is specified, the `datetime` is assumed to be UTC.
|
|
38
|
+
Attempting to generate a local timestamp from a naive datetime will result
|
|
39
|
+
in an error.
|
|
40
|
+
|
|
41
|
+
>>> generate(datetime(2009, 1, 1, 12, 59, 59, 0), accept_naive=True, utc=False)
|
|
42
|
+
Traceback (most recent call last):
|
|
43
|
+
...
|
|
44
|
+
ValueError: cannot generate a local timestamp from a naive datetime
|
|
45
|
+
|
|
46
|
+
"""
|
|
47
|
+
|
|
48
|
+
if dt.tzinfo is None:
|
|
49
|
+
if accept_naive is True:
|
|
50
|
+
if utc is True:
|
|
51
|
+
dt = dt.replace(tzinfo=datetime.timezone.utc)
|
|
52
|
+
else:
|
|
53
|
+
raise ValueError(
|
|
54
|
+
"cannot generate a local timestamp from a naive datetime"
|
|
55
|
+
)
|
|
56
|
+
else:
|
|
57
|
+
raise ValueError("naive datetime and accept_naive is False")
|
|
58
|
+
|
|
59
|
+
if utc is True:
|
|
60
|
+
dt = dt.astimezone(datetime.timezone.utc)
|
|
61
|
+
|
|
62
|
+
timestamp = dt.strftime("%Y-%m-%dT%H:%M:%S")
|
|
63
|
+
if microseconds is True:
|
|
64
|
+
timestamp += dt.strftime(".%f")
|
|
65
|
+
if dt.tzinfo is datetime.timezone.utc:
|
|
66
|
+
timestamp += "Z"
|
|
67
|
+
else:
|
|
68
|
+
timestamp += format_timezone(dt.tzinfo.utcoffset(dt).total_seconds())
|
|
69
|
+
|
|
70
|
+
return timestamp
|
pyrfc3339/parser.py
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import re
|
|
2
|
+
from datetime import datetime, timedelta, timezone
|
|
3
|
+
|
|
4
|
+
from pyrfc3339.utils import format_timezone
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def parse(timestamp, utc=False, produce_naive=False):
|
|
8
|
+
"""
|
|
9
|
+
Parse an :RFC:`3339`-formatted timestamp and return a
|
|
10
|
+
:class:`datetime.datetime`.
|
|
11
|
+
|
|
12
|
+
If the timestamp is presented in UTC, then the `tzinfo` parameter of the
|
|
13
|
+
returned `datetime` will be set to :attr:`datetime.timezone.utc`.
|
|
14
|
+
|
|
15
|
+
>>> parse('2009-01-01T10:01:02Z')
|
|
16
|
+
datetime.datetime(2009, 1, 1, 10, 1, 2, tzinfo=datetime.timezone.utc)
|
|
17
|
+
|
|
18
|
+
Otherwise, a :class:`datetime.timezone` instance is created with the appropriate offset, and
|
|
19
|
+
the `tzinfo` parameter of the returned `datetime` is set to that value.
|
|
20
|
+
|
|
21
|
+
>>> parse('2009-01-01T14:01:02-04:00')
|
|
22
|
+
datetime.datetime(2009, 1, 1, 14, 1, 2, tzinfo=datetime.timezone(datetime.timedelta(days=-1, seconds=72000), '<UTC-04:00>'))
|
|
23
|
+
|
|
24
|
+
However, if `parse()` is called with `utc=True`, then the returned
|
|
25
|
+
`datetime` will be normalized to UTC (and its tzinfo parameter set to
|
|
26
|
+
`datetime.timezone.utc`), regardless of the input timezone.
|
|
27
|
+
|
|
28
|
+
>>> parse('2009-01-01T06:01:02-04:00', utc=True)
|
|
29
|
+
datetime.datetime(2009, 1, 1, 10, 1, 2, tzinfo=datetime.timezone.utc)
|
|
30
|
+
|
|
31
|
+
The input is strictly required to conform to :RFC:`3339`, and appropriate
|
|
32
|
+
exceptions are thrown for invalid input.
|
|
33
|
+
|
|
34
|
+
>>> parse('2009-01-01T06:01:02')
|
|
35
|
+
Traceback (most recent call last):
|
|
36
|
+
...
|
|
37
|
+
ValueError: timestamp does not conform to RFC 3339
|
|
38
|
+
|
|
39
|
+
>>> parse('2009-01-01T25:01:02Z')
|
|
40
|
+
Traceback (most recent call last):
|
|
41
|
+
...
|
|
42
|
+
ValueError: hour must be in 0..23
|
|
43
|
+
|
|
44
|
+
"""
|
|
45
|
+
|
|
46
|
+
parse_re = re.compile(
|
|
47
|
+
r"""^(?:(?:(?P<date_fullyear>[0-9]{4})\-(?P<date_month>[0-9]{2})\-(?P<date_mday>[0-9]{2}))T(?:(?:(?P<time_hour>[0-9]{2})\:(?P<time_minute>[0-9]{2})\:(?P<time_second>[0-9]{2})(?P<time_secfrac>(?:\.[0-9]{1,}))?)(?P<time_offset>(?:Z|(?P<time_numoffset>(?P<time_houroffset>(?:\+|\-)[0-9]{2})\:(?P<time_minuteoffset>[0-9]{2}))))))$""",
|
|
48
|
+
re.I | re.X,
|
|
49
|
+
)
|
|
50
|
+
|
|
51
|
+
match = parse_re.match(timestamp)
|
|
52
|
+
|
|
53
|
+
if match is not None:
|
|
54
|
+
if match.group("time_offset") in ["Z", "z", "+00:00", "-00:00"]:
|
|
55
|
+
if produce_naive is True:
|
|
56
|
+
tzinfo = None
|
|
57
|
+
else:
|
|
58
|
+
tzinfo = timezone.utc
|
|
59
|
+
else:
|
|
60
|
+
if produce_naive is True:
|
|
61
|
+
raise ValueError(
|
|
62
|
+
"cannot produce a naive datetime from a local timestamp"
|
|
63
|
+
)
|
|
64
|
+
else:
|
|
65
|
+
tz_hours = int(match.group("time_houroffset"))
|
|
66
|
+
tz_minutes = int(match.group("time_minuteoffset"))
|
|
67
|
+
if tz_hours < 0:
|
|
68
|
+
tz_minutes *= -1
|
|
69
|
+
td = timedelta(hours=tz_hours, minutes=tz_minutes)
|
|
70
|
+
tzinfo = timezone(td, f"<UTC{format_timezone(td.total_seconds())}>")
|
|
71
|
+
|
|
72
|
+
secfrac = match.group("time_secfrac")
|
|
73
|
+
if secfrac is None:
|
|
74
|
+
microsecond = 0
|
|
75
|
+
else:
|
|
76
|
+
microsecond = int(round(float(secfrac) * 1000000))
|
|
77
|
+
|
|
78
|
+
dt_out = datetime(
|
|
79
|
+
year=int(match.group("date_fullyear")),
|
|
80
|
+
month=int(match.group("date_month")),
|
|
81
|
+
day=int(match.group("date_mday")),
|
|
82
|
+
hour=int(match.group("time_hour")),
|
|
83
|
+
minute=int(match.group("time_minute")),
|
|
84
|
+
second=int(match.group("time_second")),
|
|
85
|
+
microsecond=microsecond,
|
|
86
|
+
tzinfo=tzinfo,
|
|
87
|
+
)
|
|
88
|
+
|
|
89
|
+
if utc:
|
|
90
|
+
dt_out = dt_out.astimezone(timezone.utc)
|
|
91
|
+
|
|
92
|
+
return dt_out
|
|
93
|
+
else:
|
|
94
|
+
raise ValueError("timestamp does not conform to RFC 3339")
|
pyrfc3339/utils.py
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
def format_timezone(utcoffset):
|
|
2
|
+
"""
|
|
3
|
+
Return a string representing the timezone offset.
|
|
4
|
+
Remaining seconds are rounded to the nearest minute.
|
|
5
|
+
|
|
6
|
+
>>> format_timezone(3600)
|
|
7
|
+
'+01:00'
|
|
8
|
+
>>> format_timezone(5400)
|
|
9
|
+
'+01:30'
|
|
10
|
+
>>> format_timezone(-28800)
|
|
11
|
+
'-08:00'
|
|
12
|
+
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
hours, seconds = divmod(abs(utcoffset), 3600)
|
|
16
|
+
minutes = round(float(seconds) / 60)
|
|
17
|
+
|
|
18
|
+
if utcoffset >= 0:
|
|
19
|
+
sign = "+"
|
|
20
|
+
else:
|
|
21
|
+
sign = "-"
|
|
22
|
+
return "{0}{1:02d}:{2:02d}".format(sign, int(hours), int(minutes))
|