Convert an RFC 3339 time to a standard Python timestamp
No builtin, afaik.
feed.date.rfc3339
This is a Python library module with functions for converting timestamp strings in RFC 3339 format to Python time float values, and vice versa. RFC 3339 is the timestamp format used by the Atom feed syndication format.
It is BSD-licensed.
http://home.blarg.net/~steveha/pyfeed.html
(Edited so it's clear I didn't write it. :-)
Getting the RFC 3339 timestamp for a day ago
You can get one day previous to x
with:
x = x + datetime.timedelta(days = -1)
The following transcript shows this in action:pax> python
Python 2.7.3 (default, Jan 2 2013, 16:53:07)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import datetime
>>> d = datetime.datetime.utcnow()
>>> d
datetime.datetime(2014, 2, 26, 1, 11, 1, 536396)
>>> print d.isoformat("T") + "Z"
2014-02-26T01:11:01.536396Z
>>> d = d + datetime.timedelta(days=-1)
>>> d
datetime.datetime(2014, 2, 25, 1, 11, 1, 536396)
>>> print d.isoformat("T") + "Z"
2014-02-25T01:11:01.536396Z
Python and RFC 3339 timestamps
This was based on the examples on page 10 of the RFC. The only difference is that I am showing a microseconds value of six digits, conformant to Google Drive's timestamps.
from math import floor
def build_rfc3339_phrase(datetime_obj):
datetime_phrase = datetime_obj.strftime('%Y-%m-%dT%H:%M:%S')
us = datetime_obj.strftime('%f')
seconds = datetime_obj.utcoffset().total_seconds()
if seconds is None:
datetime_phrase += 'Z'
else:
# Append: decimal, 6-digit uS, -/+, hours, minutes
datetime_phrase += ('.%.6s%s%02d:%02d' % (
us,
('-' if seconds < 0 else '+'),
abs(int(floor(seconds / 3600))),
abs(seconds % 3600)
))
return datetime_phrase
convert list of string to ISO 8601/RFC 3339 format
Based on that error message, you've probably done import rfc3339
, not from rfc3339 import rfc3339
. You can't call the module (as it's telling you).
That said, if all your strings are of the format YYYY-MM-DDTHH:mm:ss
and you want YYYY-MM-DDTHH:mm:ssZ
, why not just...
z_dates = [date + "Z" for date in start_date]
Converting TZ time to string
Try this:
from dateutil import parser
from datetime import datetime
datetime_obj = parser.parse(saved_data['created_at'])
readable = datetime.strftime(datetime_obj,'%Y-%m-%d %H:%M:%S')
print (readable)
Python datetime parse timezone (RFC3339)
You could use dateutil
, however, if you are using Python 3 it only works for 3.2 or 3.3. It also supports Python 2 on 2.6 and 2.7.
The solution I would suggest:
from dateutil.parser import parse
string = "2016-01-29T20:00:00+01:00"
date = parse(string)
This will give you a datetime object like so:Out[1]: 2016-01-29 20:00:00+01:00
Out[2]: datetime.datetime(2016, 1, 29, 20, 0, tzinfo=tzoffset(None, 3600))
If you'd like to know more, check dateutil documentation.Moreover, I believe the reason strptime
doesn't work straight away here is due to that 'T' on your date string. It's not the format strptime
expects. Luckily dateutil
parse method works right out of the box to fix that string for you.
Related Topics
Scale Everything on Pygame Display Surface
Continuing in Python's Unittest When an Assertion Fails
How to Calculate the Inverse of the Normal Cumulative Distribution Function in Python
Extract Int from String in Pandas
How to Read Class Attributes in the Same Order as Declared
Disable or Lock Mouse and Keyboard in Python
Python Observer Pattern: Examples, Tips
Numpy Array Dtype Is Coming as Int32 by Default in a Windows 10 64 Bit MAChine
Changing the Options of a Optionmenu When Clicking a Button
Importerror: Cannot Import Name Numpy_Mkl
How to Find the Maximum Value in a List of Tuples
How to Ignore One Single Specific Line with Pylint
Counting Letter Frequency in a String (Python)
Appending to the Same List from Different Processes Using Multiprocessing
Passing Double Quote Shell Commands in Python to Subprocess.Popen()