Parse a date string from a twitter post with timezone info included - dart

I have created a tweet using Twitters REST API. The returned JSON from Twitter from the created Tweet is in this format
'Tue Jan 31 11:15:15 +0000 2017'
Darts
DateTime.parse('Tue Jan 31 11:15:15 +0000 2017');
won't parse this format. I turned to the intl package holds some hope in dealing with this. I try to use DateFormat to solve this thus
DateFormat format = new DateFormat("EEE MMM dd HH:mm:ss ZZZZ yyyy");
format.parse('Tue Jan 31 11:15:15 +0000 2017');
It fails. The only way I can get it to work is to remove the timezone part of the twitter date string
'Tue Jan 31 11:15:15 2017'
and do the following
var date = 'Tue Jan 31 11:15:15 +0000 2017'.split(' ');
date.removeAt(4);
DateFormat format = new DateFormat("EEE MMM dd HH:mm:ss yyyy");
format.parse(date.join(' '));
How can I parse a Twitter datetime string without removing the timezone info part?

It looks like package:intl doesn't support timezones yet: https://github.com/dart-lang/intl/issues/128
You could add your use case to the issue (it shouldn't be much work or require extra data to support the 'ZZZZ' case, since you just need to parse a number), or even better, submit a Pull Request!

Dart DateTime doesn't support timezones, so it's not that useful to parse them. Also, Intl is probably not the best way to parse a string like this. The point of Intl date parsing is that it varies by locale. If you want to parse a fixed format it can be done more easily and more efficiently.
Since DateTime doesn't support time zones, if you want to use that information, it seems like your best bet is to remove it, get the information, and then add that to the time after the fact.
If you're running in the browser, you could also call out to the JS Date.parse(), which does handle that format.

Related

Incorrect locale from dateFormatter

I have problem with my DateFormatter.
My iOS app communicates with server and uses If-Modified-Since header with date created with following formatter:
modifiedSinceDateFormatter = DateFormatter()
modifiedSinceDateFormatter!.dateFormat = "EEE, dd MMM yyyy HH:mm:ss zzz"
modifiedSinceDateFormatter!.locale = Locale(identifier: "en_US")
modifiedSinceDateFormatter!.timeZone = TimeZone(abbreviation: "GMT")
It works as expected - returning date in following format: Fri, 08 Sep 2017 07:02:20 GMT.
But I was looking through the server logs and found that once request was made with following date format sob., 26 sie 2017 10:17:01 CEST (that's correct Polish locale and timezone - I expect my users to use Polish locale).
So my question is:
How is it possible that this formatter returned date in the wrong locale? Are there some options that user can activate to override this locale (like Accessibility options)? Can it be some jailbroken device?
EDIT: And it happened again: wt., 17 kwi 2018 08:40:02 CEST. Interesting that there was few requests (at same moment from single device) and only one of them failed - with wrong date).
I found the following explanation on how to ensure you get your date properly parsed using English names for months and days
Unless you specifically need month and/or weekday names to appear in the user's language, you should always use the special locale of en_US_POSIX. This will ensure your fixed format is actually fully honored and no user settings override your format. This also ensures month and weekday names appear in English. Without using this special locale, you may get 24-hour format even if you specify 12-hour (or visa-versa). And dates sent to a server almost always need to be in English.
I found the quote on this page

How to work with raw date format in Google Sheets

I'm using Zapier to input timestamps of emails to Google Sheets. The format I'm seeing in Google Sheets is:
Wed, 28 Jun 2017 21:02:51 +0000 (UTC)
Is there a way to either change this (in Google Sheets) to 2017-06-28 21:02:51 or to help date-related functions to understand this format?
You should be able to extract date from string:
=DATEVALUE(REGEXEXTRACT("Wed, 28 Jun 2017 21:02:51 +0000 (UTC)","\d+ [A-Za-z]{3} \d{4}"))
REGEXEXTRACT part extracts "28 Jun 2017". Then format the result as date.
Edit:
Getting closer :) Error DATEVALUE parameter '28 Jun 2017' cannot be
parsed to date/time.
I think this is bacause of your regional settings.
Option: writing script, my solution wont work.
Option: Change File → Regional Settings to US.
Option: modify the formula so it replaces english names of monthes into their numbers. This would produce ugly big formula.

preserving datetime values in rails while saving in utc(default)

I have a a checkin:datetime field in rails and is using the default utc.
But the issue is that when the user submits the form the checkin date is coming with his local timezone info. So rails will automatically convert this to utc and depending on the difference with his timezone and utc there might be an off of one day.
So how can I change the date to utc without changing values?
Update
This is the only code I use for saving to database.(the utc conversion is done by activerecord(i think) if the passed in value is not utc)
reservation=current_user.reservations.create(reservation_params)
reservation.save
I have found a way to do that. It is not direct conversion as I expected but it works. I changed the form input field to sent a string with just the date and time(without utc).
eg: 2016-07-13 00:00:00
Then in my controller before saving I used below code to parse it to utc.
reservation.check_in= Time.zone.parse(value_from_view)
example:
reservation.check_in= Time.zone.parse('2016-07-13 00:00:00')
This returns Wed, 13 Jul 2016 00:00:00 UTC +00:00 as expected.

date format dilemma?

I have this date from twitter, this represents the exact date the tweet is published,
Sun, 01 Jul 2012 19:05:54 +0000
what I want is to make its format into MM/DD HH:MM, tried to look for php date formats but couldn't find a way to make it look exactly the way I want it to be. Can someone please help? Thanks.
print date('m/d h:i',strtotime('Sun, 01 Jul 2012 19:05:54 +0000'));
The date function for php is a good place to find all sorts of information on this.
This would be pretty simple to search and figure it out. You are looking for a date... ahh, date, that is a php function. When you look that up you will see that it takes some params, a format and a time stamp. Well... You do not have a time stamp you have a string. how do i convert a string to time? Wait, there is a strtotime function in php. There you have it... run the date function in php with the way you want the date to look and then convert the string to timestamp with strtotime
<?php
date_default_timezone_set("UTC");
$string = "Sun, 01 Jul 2012 19:05:54 +0000";
$timestamp = strtotime($string);
print "Date is " . date("m/d H:i", $timestamp) . "\n";
?>
You may have to change timezone, and/or add/subtract seconds or use local time functions to convert between TZ's.

how to change: Sun Jan 01 00:00:00 BRST 2006 to 2006-01-01 00:00:00.0

Hey guys, how can I tranform the format of datePicker that came to my action params?
My actual Date attribute doesn`t accept this"Sun Jan 01 00:00:00 BRST 2006" type, only "2006-01-01 00:00:00.0 " for example.
How can I deal with it?
It's hard to investigate in questions that don't provide required information to reproduce the issue. In your case, that would be the error type, and message, and the related code snippets, at least. -
Normally, your datePicker result "Sun Jan 01 00:00:00 BRST 2006" should be perfectly fine with standard code like that:
Controller action method:
def index = {
Date date = params.datePicker
[date: params.datePicker ?: new Date()]
}
GSP:
<g:datePicker name="datePicker" value="${date}" />
Now let's go on to wild guesses:
In case you're trying to parse that date string using the SimpleDateFormat class, the corresponding conversion pattern would be:
Date date = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy")
.parse("Sun Jan 01 00:00:00 BRST 2006")
resp.,
Date date = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy")
.parse(params.datePicker)
It's also possible to register a CustomDateEditor, but there shouldn't be a need to do with a datePicker.
After all, the datePicker result should be converted to a Date, automatically, as depicted in the first sample. - If it still does not, please clarify your issue.
Why you don't use Joda-time instead of Date that has many deprecate methods, and is less powerful.
In the gsp file then you just need to use: .toString("format")

Resources