Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Going from twitter date to Python datetime date

I am receiving twitter messages that are sent at a certain date in the following format from twitter:

Tue Mar 29 08:11:25 +0000 2011

I want to store these dates in 'timestamp with time zone' field in postgresql with djangos DateTimeField field. When I store that string however I get this error:

ValidationError: [u'Enter a valid date/time in YYYY-MM-DD HH:MM[:ss[.uuuuuu]] format.']

can I automatically transform the twitter datetype to a python datetime time (that does work elsewhere in my app for saving dates).

like image 909
Javaaaa Avatar asked Oct 09 '11 13:10

Javaaaa


People also ask

What date format does Twitter use?

Kristin Briney on Twitter: "There is one correct way to write a date and it's YYYY-MM-DD.

Is Python datetime in UTC?

You can use the datetime module to convert a datetime to a UTC timestamp in Python. If you already have the datetime object in UTC, you can the timestamp() to get a UTC timestamp. This function returns the time since epoch for that datetime object.

How do I grab a date in Python?

today() method to get the current local date. By the way, date. today() returns a date object, which is assigned to the today variable in the above program. Now, you can use the strftime() method to create a string representing date in different formats.


4 Answers

Writing something like this should convert a twitter date to a timestamp.

import time

ts = time.strftime('%Y-%m-%d %H:%M:%S', time.strptime(tweet['created_at'],'%a %b %d %H:%M:%S +0000 %Y'))

UPDATE

For Python 3, as per 2020, you can do it in this way:

from datetime import datetime

# dtime = tweet['created_at']
dtime = 'Fri Oct 09 10:01:41 +0000 2015'
new_datetime = datetime.strftime(datetime.strptime(dtime,'%a %b %d %H:%M:%S +0000 %Y'), '%Y-%m-%d %H:%M:%S')
print((new_datetime))
like image 123
Chris Herring Avatar answered Oct 21 '22 05:10

Chris Herring


Give this a go. It assumes the date format from twitter is RFC822 compliant (see the question linked to by @Adrien).

A naive datetime object is constructed (i.e. no timezone info). It is adjusted according to the timezone offset to UTC. Unless you have a need to keep the original timezone, I'd store the date time as UTC and format to local time when you display it.

from datetime import datetime, timedelta
from email.utils import parsedate_tz

s = 'Tue Mar 29 08:11:25 +0000 2011'

def to_datetime(datestring):
    time_tuple = parsedate_tz(datestring.strip())
    dt = datetime(*time_tuple[:6])
    return dt - timedelta(seconds=time_tuple[-1])
like image 29
Rob Cowie Avatar answered Oct 21 '22 06:10

Rob Cowie


A little bit old but using parse really help me with this issue

from datetime import datetime
from dateutil.parser import parse

date = 'Fri May 10 00:44:04 +0000 2019' 
dt = parse(date)

print(dt) 
# 2019-05-10 00:44:04+00:00
like image 8
strider Avatar answered Oct 21 '22 05:10

strider


To get datetime with timezone you can simple use datetime.strptime as follow:

from datetime import datetime
s = 'Wed Jun 05 05:34:02 +0000 2019'
created_at = datetime.strptime(s, '%a %b %d %H:%M:%S %z %Y')
print(created_at)
#2019-06-05 05:34:02+00:00
like image 3
elopezp Avatar answered Oct 21 '22 05:10

elopezp