Parsing Twitter API Datestamp

Parsing Twitter API Datestamp

strtotime("dateString"); gets it into the native PHP date format, then you can work with the date() function to get it printed out how you'd like it.

Trying to convert timestamp from twitter api into DateTime in flutter

You can use substring method of string and cut each and every part then pass all that in datetime.

I hope Following code solve your issue.

  int month;
String datetime = "Thu Mar 26 11:51:30 +0000 2020";
String monthInString = datetime.substring(4, 7);
int day = int.parse(datetime.substring(8, 10));
int hours = int.parse(datetime.substring(11, 13));
int min = int.parse(datetime.substring(14, 16));
int second = int.parse(datetime.substring(17, 19));
int year = int.parse(datetime.substring(26, 30));
DateTime _dateTime;
switch (monthInString) {
// add all months
case 'Jan':
month = 1;
break;
case 'Mar':
month = 3;
break;
}
_dateTime = DateTime.utc(year, month, day, hours, min, second);
print(_dateTime);

Twitter Api returns Inacurate 24 hour time? Parsing in swift 4

The proper way to deal with that date string is to use a DateFormatter to first convert the string into a Date. Then use another DateFormatter to convert into a new string in a desired style.

let twitterTimestamp = "Thu May 31 16:43:37 +0000 2018"
let tdf = DateFormatter()
tdf.dateFormat = "EEE MMM dd HH:mm:ss Z yyyy"
tdf.locale = Locale(identifier: "en_US_POSIX")
if let date = tdf.date(from: twitterTimestamp) {
let df = DateFormatter()
df.dateStyle = .medium
df.timeStyle = .medium
let result = df.string(from: date)
} else {
// Unexpected date string
}

The result will be in the user's local time.

Set the dateStyle and timeStyle properties to suit your specific needs.

How to parse twitter date time string in pyspark?

I couldn't get any of the solution provided working. Apparently the to_timestamp() function on pyspark.sql reads the input in a certain format. All attempts of provide the format yielded no results. Hence using UDF (user defined function) was inevitable. But one has to note that the function cannot return python datetime. So now it has to be a two step process.

  1. UDF for parsing the date string, convert the format and return a string compatible with to_timestamp()
  2. Using to_timestamp() to convert the datatype in spark dataframe

from datetime import datetime
import pytz
from pyspark.sql.functions import udf, to_date, to_utc_timestamp

## Converting date string format
def getDate(x):
if x is not None:
return str(datetime.strptime(x,'%a %b %d %H:%M:%S +0000 %Y').replace(tzinfo=pytz.UTC).strftime("%Y-%m-%d %H:%M:%S"))
else:
return None

## UDF declaration
date_fn = udf(getDate, StringType())

## Converting datatype in spark dataframe
df = df.withColumn("created_at", to_utc_timestamp(date_fn("created_at"),"UTC"))

Twitter API date created_at and conversion to local time doesn't add up

Mystery solved, unless you are logged into Twitter, the default timezone must be Pacific. Once logged in tweets are timestamped with what ever timezone you have set. I am now logged in and the tweet shows 12pm.

Trouble parsing Twitter timestamps using to_timestamp function

This seems to work, if all times are +0000:

test=# select to_timestamp('Wed Oct 09 10:31:05 +0000 2013', 'DY Mon DD HH24:MI:SS +0000 YYYY');
to_timestamp
------------------------
2013-10-09 10:31:05+02
(1 row)

If not, it doesn't seem to matter much:

test=# select to_timestamp('Wed Oct 09 10:31:05 +0200 2013', 'DY Mon DD HH24:MI:SS +0000 YYYY');
to_timestamp
------------------------
2013-10-09 10:31:05+02
(1 row)

test=# select to_timestamp('Wed Oct 09 10:31:05 +0200 2013', 'DY Mon DD HH24:MI:SS xxxx YYYY')::timestamp without time zone;
to_timestamp
---------------------
2013-10-09 10:31:05
(1 row)

Going from twitter date to Python datetime date

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))


Related Topics



Leave a reply



Submit