Skip to main content

License

rfc3339lib

rfc339lib is a Python library to quickly and efficiently manage date-time entities that confrom to the RFC3339 Specification for dates and times. rfc339lib is written primarily in C with Cython bindings, and so it is extremely fast.

Distribution

Installation

Use the package manager pip to install rfc3339lib.

pip install rfc3339lib

Usage

Many examples of usage are available in the main test files included in the t/ subdirectory.

import rfc3339lib
import datetime

now = datetime.datetime.now()
rfcnow = rfc3339lib.to_rfc3339(now)
print(rfcnow)

dt = rfc3339lib.from_rfc3339(rfcnow)
print(dt)

print( rfc3339lib.is_rfc3339(rfcnow), "should be true")

# Sometimes separators are different. For example, a ':' can't be in a MacOS filename.
macsafe = rfcnow.replace(':', '.')
print(macsafe)

print(rfc3339lib.is_rfc3339(macsafe, strict=False), "should be true")
 # yes, okay

print(rfc3339lib.is_rfc3339(macsafe, strict=True), 'should be false') #no, false

print("This should be okay:", rfc3339lib.from_rfc3339(macsafe, strict=False))

try:
    rfc3339lib.from_rfc3339(macsafe, strict=True)
    raise Exception("This should not be reached because macsafe is not, strictly speaking, a valid string")
except ValueError as ve:
    print("Correctly threw value error for manipulated timestamp in strict mode")

Quicks, Implementation, and Rationale

strictness and permissive parsing

All strings generated by totimestamp() will be ISO8601/RFC3339 strings. However, the default approach is to take a somewhat permissive approach to parsing strings under the assumption that, if the programmer is requesting a datetime object for a formatted string, they are more interested in the the actual date than whether the source was candidly aware of the nuances of the ISO8601 standard. There are other situations in which the standard cannot be met (as shown in the example) - MacOS does not like colons in filenames, and so if they are parsing timestamp-encoded files (mydata.{timestamp}.csv), the source provider may verywell have changed the colon to some unknown-but-definitely-not-a-number character.

So, there are essentially three levels of checking conducted. There is nonstrict, permissive checking, that requires the format to be roughly correct. There is strict checking, that requires all non-numeric characters to be exactly valid. There is range checking that ensures that the values of numbers are valid, i.e. the second and the day are within appropriate ranges, and this is effectively a separate issue from the non-numeric strictness.

So, here are implementation points to be aware of:

  • In nonstrict mode, a single-digit hour or a signle-digit hour timezone is permitted. ** In nonstrict mode, there are 3 classifications of characters: numerals, alphanumerals, and non-alphanumeric. ** Time-component separators, date-component separators must be non-alphanumeric. ** The separator between Time and Date must be non-numeric, allowing for 'T', space, or some other characer, but not '-' or ':' as that would suggest a time/date separation. ** The separator to indicate a fractional section may be '.' or ',', keeping with iso8601 even though RFC3339 indicates '.' is the only acceptable value
  • In strict mode: ** As required by ISO8061, hours must be 2 digit and 0-padded. ** Time separators must be '-' ** Date separators must be ':' ** The separator between time and date must be 't', 'T', or ' ' (RFC3339 notes that it is permissible to use another character "(say) a space character" for readability).
  • Hour=24. ISO8601-2004 allowed, but advised against, an hour of 24, and it was prohibited in RFC3339. The new ISO8601-2019 specification has updated ISO8601-20004 to prohibit it. While it is possible that archival timestamps pushing against the advisement may have an hour of 24, it is not permitted in range checking.

Leap Seconds

Leap seconds are real and part of the specification in ISO8601 and RFC3339. However, they cannot be calculated, and so it would not be reliable to attempt to validate them. Further, Python's native datetime.datetime object does not allow for a leap second. This has led to the following design decisions:

The range checking will permit a value of 60 for second when parsing the value, but does not verify that it falls on a listed leap second.

The request to actually parse a timestamp and return the time values in the C/Cython code may return 60 as a second value.

The translation into a Python datetime object, by default, will convert a leap second to second=59,microsecond=9999999 to avoid inexplicable program crashes in the roughly 1-in-50million seconds that are leap.

This translation can be overrriden - see the function definition; however, note that if a leap second is encounted, the creation of the datetime.datetime object will throw an exception in the current version of Python.

Potential Errors

Contributing

Contributions and collaboration is welcome. Please contact me in advance for features you would like or would like to add.

Author

Kevin Crouse. Copyright, 2021.

License

Apache 2.0

Metadata

Release files for datetimeparse 0.5.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for datetimeparse 0.5.6
File Size Uploaded Provenance Actions
datetimeparse-0.5.6.tar.gz 55.1 kB Without provenance Details for datetimeparse-0.5.6.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for datetimeparse 0.5.6
File Interpreter ABI Platform Actions
datetimeparse-0.5.6-cp39-cp39-manylinux2014_x86_64.whl CPython 3.9 CPython 3.9 Linux glibc 2.17+ x86-64 Details for datetimeparse-0.5.6-cp39-cp39-manylinux2014_x86_64.whl
datetimeparse-0.5.6-cp38-cp38-manylinux2014_x86_64.whl CPython 3.8 CPython 3.8 Linux glibc 2.17+ x86-64 Details for datetimeparse-0.5.6-cp38-cp38-manylinux2014_x86_64.whl
datetimeparse-0.5.6-cp37-cp37m-manylinux2014_x86_64.whl CPython 3.7 CPython 3.7 pymalloc Linux glibc 2.17+ x86-64 Details for datetimeparse-0.5.6-cp37-cp37m-manylinux2014_x86_64.whl

Total release size: 543.4 kB

Release files / datetimeparse-0.5.6.tar.gz

Download URL datetimeparse-0.5.6.tar.gz
Size 55.1 kB
Tags Source
SHA-256 checksum
How to use checksums
4e64858d0e5b0f270606c398246b621ec68009e77a4d4f041970a6c02581c69d
BLAKE2b-256 checksum
How to use checksums
858df47eb8b27917cfbd6da6acceaa668c4d305037e383c094d5974af3802def
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.4.2 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.8.0 tqdm/4.45.0 CPython/3.8.5

Release files / datetimeparse-0.5.6-cp39-cp39-manylinux2014_x86_64.whl

Download URL datetimeparse-0.5.6-cp39-cp39-manylinux2014_x86_64.whl
Size 161.7 kB
Tags CPython 3.9 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
ea66d1cf37b584d97ad5356991745ce83af04578ab68fc287927476a77da475b
BLAKE2b-256 checksum
How to use checksums
f67c9481156888df0f4501e2ee88bdc8ce82cf942d087cca640ef8285fcfda64
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.4.2 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.8.0 tqdm/4.45.0 CPython/3.8.5

Release files / datetimeparse-0.5.6-cp38-cp38-manylinux2014_x86_64.whl

Download URL datetimeparse-0.5.6-cp38-cp38-manylinux2014_x86_64.whl
Size 168.7 kB
Tags CPython 3.8 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
b4d7b86fc51ef6eee149f464acbb486f9d1f7e084610bc65075792eb950643b3
BLAKE2b-256 checksum
How to use checksums
ca57b1941578a47285356e01852304512b1699f8b41a75e31e505a03abfaa624
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.4.2 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.8.0 tqdm/4.45.0 CPython/3.8.5

Release files / datetimeparse-0.5.6-cp37-cp37m-manylinux2014_x86_64.whl

Download URL datetimeparse-0.5.6-cp37-cp37m-manylinux2014_x86_64.whl
Size 157.9 kB
Tags CPython 3.7 CPython 3.7 pymalloc Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
0bce9415fce1466e18b61a09358a02d91544ab3eecb21d20b965430de3c8c1c5
BLAKE2b-256 checksum
How to use checksums
f526d3b843d9e214060eb5420de4684f0419cb1956046a3c5abca55bec4ad325
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.4.2 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.8.0 tqdm/4.45.0 CPython/3.8.5

Security for datetimeparse 0.5.6

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page