danielrichman / strict-rfc3339

Strict, simple, lightweight RFC3339 functions
GNU General Public License v3.0
31 stars 13 forks source link

Strict, simple, lightweight RFC3339 functions

Goals

Caveats

In both cases, see 'Notes' below.

Rationale

Usage

Validation:

>>> strict_rfc3339.validate_rfc3339("some rubbish")
False
>>> strict_rfc3339.validate_rfc3339("2013-03-25T12:42:31+00:32")
True

Indeed, we can then:

>>> strict_rfc3339.rfc3339_to_timestamp("2013-03-25T12:42:31+00:32")
1364213431
>>> tuple(time.gmtime(1364213431))[:6]
(2013, 3, 25, 12, 10, 31)

No need for two function calls:

>>> strict_rfc3339.rfc3339_to_timestamp("some rubbish")
Traceback [...]
strict_rfc3339.InvalidRFC3339Error

Producing strings (for this example TZ=America/New_York):

>>> strict_rfc3339.timestamp_to_rfc3339_utcoffset(1364213431)
'2013-03-25T12:10:31Z'
>>> strict_rfc3339.timestamp_to_rfc3339_localoffset(1364213431)
'2013-03-25T08:10:31-04:00'

And with TZ=Europe/London:

>>> strict_rfc3339.timestamp_to_rfc3339_localoffset(1364213431)
'2013-03-25T12:10:31+00:00'

Convenience functions:

>>> strict_rfc3339.now_to_rfc3339_utcoffset()
'2013-03-25T21:39:35Z'
>>> strict_rfc3339.now_to_rfc3339_localoffset()
'2013-03-25T17:39:39-04:00'

Floats:

>>> strict_rfc3339.now_to_rfc3339_utcoffset(integer=True) # The default
'2013-03-25T22:04:01Z'
>>> strict_rfc3339.now_to_rfc3339_utcoffset(integer=False)
'2013-03-25T22:04:01.04399Z'
>>> strict_rfc3339.rfc3339_to_timestamp("2013-03-25T22:04:10.04399Z")
1364249050.0439899

Behind the scenes

These functions are essentially string formatting and arithmetic only. A very small number of functions do the heavy lifting. These come from two modules: time and calendar.

time is a thin wrapper around the C time functions. I'm working on the assumption that these are usually of high quality and are correct. From the time module, strict_rfc3339 uses:

Based on the assumption that they are correct, we can use the difference between the values returned by gmtime and localtime to find the local offset. As clunky as it sounds, it's far easier than using a fully fledged timezone library.

calendar is implemented in python. From calendar, strict_rfc3339 uses:

Notes