Date/Time conversion problems from Rails to Flex? - ruby-on-rails

I am getting the following error:
TypeError: Error #1034: Type Coercion failed: cannot convert "2010-01-02 23:28:17 UTC" to Date.
I am using WebORB to transfer data between Rails and Flex. The data coming from Rails is of type: 'ActiveSupport::TimeWithZone'. I am trying to assign it to a Flex 'Date' data type.
What am I missing?

From the documentation: http://www.adobe.com/livedocs/flex/3/langref/Date.html#Date()
If you pass a string to the Date class constructor, the date can be in a variety of formats, but must at least include the month, date, and year. For example, Feb 1 2005 is valid, but Feb 2005 is not. The following list indicates some of the valid formats:
Day Month Date Hours:Minutes:Seconds GMT Year (for instance, "Tue Feb 1 00:00:00 GMT-0800 2005", which matches toString())
I think in Rails, what you need to do is calling strftime to format the date output that's going to be sent to Flex
time_with_zone.strftime("%a %b %d %H:%M:%S %z %Y") # => "Sun Jan 03 20:58:16 +0700 2010"

Thanks for the help but, oddly, that wasn't it. Your help, Sikachu, did put me on the right track. I couldn't simply assign the returned result--I had to feed it into the constructor. So, instead of doing this, which didn't work:
var flexDate:Date = server_result.date;
I did this, which works:
var flexDate:Date = new Date(server_result.date);

Related

Comparing dates in 2 different formats to match or not with Ruby on Rails 6

I'm trying to compare date that comes from ActionMailbox mail.date with a date field in my PostgreSQL DB Table to check if a post for the same date already exists. The dates comes in different format I guess, how canI format them in same way to compare? The time section is irrelevant.
Date format that comes from email as below I guess. Looking at the Logs on server
Date: Wed, 24 Mar 2021 09:57:57 +0000
Date format I have in the DB is as below. Output of Post.last in rails c
date: "2021-03-24 09:57:57.000000000 +0000
I need to check if dates matches or not?
Btw the interesting thing is, I can just save mail.date to db without any particular formatting, I guess it is formatting itself before saving.
Date format I have in the DB is as below.
Databases don't store timestamps nor dates as strings, they're stored as numbers. The string format is just for humans. Unless you're storing the date as a string.
I'm trying to compare date that comes from ActionMailbox mail.date with a date field in my PostgreSQL DB Table to check if a post for the same date already exists.
Those are standard date formats, RFC 2822 and ISO 8601. So long as your date column has a date type you don't need to convert them. Rails or Postgres will take care of the conversion.
Thing.where(date: mail.date)
However, your "date" field is storing a timestamp. It might be misnamed, or it might be mistyped. If you only want to store the date, use t.date in your migration.
If you did, you'd parse them into Time objects, then compare.
t1 = Time.zone.parse("Wed, 24 Mar 2021 09:57:57 +0000")
t2 = Time.zone.parse("2021-03-24 09:57:57.000000000 +0000")
p t1 == t2
Btw the interesting thing is, I can just save mail.date to db without any particular formatting, I guess it is formatting itself before saving.
Rails type conversion is parsing the String into an ActiveSupport::TimeWithZone object.
> thing = Thing.new
> thing.created_at = "Wed, 24 Mar 2021 09:57:57 +0000"
> thing.created_at
=> Wed, 24 Mar 2021 04:57:57.000000000 CDT -05:00
> thing.created_at.class
=> ActiveSupport::TimeWithZone

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.

Convert Rails Datetime to YYYY-MM-DDThh:mm:ssZ

I have ann app that has a start_date and an end_date that is currently in the format Thu, 23 Apr 2015 17:00:00 UTC +00:00 and the field type is datetime.
I am needing to convert it into this YYYY-MM-DDThh:mm:ssZ for the Eventbrite API but I am having little luck.
I have tried iso = Time.iso8601(start_date) but I get the following error TypeError: no implicit conversion of ActiveSupport::TimeWithZone into String
Anyone able to point me in the right direction it would be very much appreciated.
try following:
start_date.iso8601
output
=> "2015-05-06T15:53:51+05:00"

Validate Date String

We have a new field being added to our app where the client wants to be able to put in Sept 22. The input will be part of an import with 100 or so records. I know there are many libraries for parsing it but we want to be able to validate it. In case someone were to make a typo. Any thoughts or libraries to do this?
DateTime.parse will parse "Sept 22" with current year.
you can just make a dateTime with specified year as
date = DateTime.parse("Sept 22")
date_time_with_year = DateTime.new(year, date.month, date.day)
Check out Chronic
You can do things like
Chronic.parse('may 27th', :now => Time.local(2000, 1, 1))
#=> Sat May 27 12:00:00 PDT 2000
It will attempt to guess what the string was trying to convey, by default (ie: "Sept 27" will actually parse to something like 2013-09-27 12:00:00 -0500

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.

Resources