3
The function `rfc3339` formats dates according to the :RFC:`3339`. `rfc3339`
4
tries to have as much as possible sensible defaults.
7
__author__ = 'Henry Precheur <henry@precheur.org>'
8
__license__ = "Public Domain"
14
def _timezone(utcoffset):
16
Return a string reprenseting the timezone offset.
23
hours = abs(utcoffset) // 3600
24
minutes = abs(utcoffset) % 3600
26
return '+%02d:%02d' % (hours, minutes)
28
return '-%02d:%02d' % (hours, minutes)
30
def _utc_offset(date, use_system_timezone):
32
Return the UTC offset of `date`. If `date` does not have any `tzinfo`, use
33
the timezone informations stored locally on the system.
36
... system_timezone = -time.altzone
38
... system_timezone = -time.timezone
39
>>> _utc_offset(datetime.datetime.now(), True) == system_timezone
41
>>> _utc_offset(datetime.datetime.now(), False)
44
if date.utcoffset() is not None:
45
return date.utcoffset()
46
elif use_system_timezone:
55
return d.strftime('%Y-%m-%dT%H:%M:%SZ')
57
def rfc3339(date, utc=False, use_system_timezone=True):
59
Return a string formatted according to the :RFC:`3339`. If called with
60
`utc=True`, it normalizes `date` to the UTC date. If `date` does not have
61
any timezone information, uses the local timezone::
63
>>> date = datetime.datetime(2008, 4, 2, 20)
64
>>> rfc3339(date, utc=True, use_system_timezone=False)
65
'2008-04-02T20:00:00Z'
66
>>> rfc3339(date) # doctest: +ELLIPSIS
67
'2008-04-02T20:00:00...'
69
If called with `user_system_time=False` don't use the local timezone and
70
consider the offset to UTC to be zero::
72
>>> rfc3339(date, use_system_timezone=False)
73
'2008-04-02T20:00:00+00:00'
75
`date` must be a a `datetime.datetime`, `datetime.date` or a timestamp as
76
returned by `time.time()`::
78
>>> rfc3339(0, utc=True, use_system_timezone=False)
79
'1970-01-01T00:00:00Z'
80
>>> rfc3339(datetime.date(2008, 9, 6), use_system_timezone=False)
81
'2008-09-06T00:00:00+00:00'
82
>>> rfc3339('foo bar')
83
Traceback (most recent call last):
85
TypeError: excepted datetime, got str instead
87
# Check if `date` is a timestamp.
90
return _utc_string(datetime.datetime.utcfromtimestamp(date))
92
date = datetime.datetime.fromtimestamp(date)
95
if isinstance(date, datetime.date):
96
# If `date` is a `datetime.date` convert it to a `datetime.datetime`.
97
if not isinstance(date, datetime.datetime):
98
date = datetime.datetime(*date.timetuple()[:3])
99
utcoffset = _utc_offset(date, use_system_timezone)
101
return _utc_string(date + datetime.timedelta(seconds=utcoffset))
103
return date.strftime('%Y-%m-%dT%H:%M:%S') + _timezone(utcoffset)
105
raise TypeError('excepted %s, got %s instead' %
106
(datetime.datetime.__name__, date.__class__.__name__))
108
if __name__ == '__main__':