Date format bug when date is in Portuguese? - ruby-on-rails

I'm seeing some odd behavior in our application coming from a user working in what I believe to be Portuguese.
I realized that Rails seems to be mis-interpreting the dates she is submitting. Here's some examples:
Date.parse("ter, 30 abr 2013 07:00:00 GMT-07:00")
=> Thu, 30 May 2013
Date.parse("dom, 5 mai 2013 07:00:00 GMT-07:00 -07:00")
ArgumentError: invalid date
Is there something extra I need to do correctly identify dates being submitted in other languages?

Date are parsed with Ruby Date class with the following documentation
http://www.ruby-doc.org/stdlib-1.9.3/libdoc/date/rdoc/Date.html#method-c-parse
I'm not sure it's a great idea to allow your users to type in the full date by themselves - they can mistype or assume different format. I suggest to use datepicker that with localized presentation based on the users location? One of the the many date pickers that allow this is http://docs.jquery.com/UI/Datepicker/Localization

Related

Change Rails date type format

I am saving dates with the followings formats 1985-01-04 and 19850104, then I need to show the format Year of this way 1985 January 4.
but, i got this got: 04 January 1985.
could I change this format? I'm using rails 6.
I have wanted to change this option default because I am implement testing with this format: 1985 January 4.
Use 'strftime' to format a date for your needs:
Time.now.strftime("%Y %B %d")
See this cheat sheet for many other options of date formatting with strftime:
https://www.shortcutfoo.com/app/dojos/ruby-date-format-strftime/cheatsheet

Rails cant save correct date into database

I have a date format like this "18/12/15" as input (it is a string)
18 => day
12 => month
15 => year (2015)
I am trying to store it in my db in a date column but it doesnt save the proper date (for the previous exemple I get Sat, 15 Dec 2018 whereas I should get something like day, 18 Dec 2015.)
How can I fix that
You could use strptime:
require 'date'
Date.strptime('18/12/15', '%d/%m/%y')
#<Date: 2015-12-18 ((2457375j,0s,0n),+0s,2299161j)>
You want to define a date with the order of day, month, year, while, due to the locale your intention is interpreted as year, month, day.
You can do one of the following:
pass the parameters in inverted order
implement a method which gets the parameters in the correct order and inverts them
set the locale to the one you intend to use
use strptime, as spickermann described

Rails - how to throw explicit error when setting / persisting invalid date [duplicate]

This question already has answers here:
How do I validate a date in rails?
(10 answers)
Closed 7 years ago.
Okay so September has 30 days right.
This is what rails does when you set an invalid date like 31 September:
s = Student.first
s.date_of_birth = "2015-09-31"
s.save!
> true
s.date_of_birth
> Thu, 01 Oct 2015 00:00:00 UTC +00:00
It sets the date to 1st of October.
What I want is something like this
s.save!
(0.4ms) BEGIN
(0.5ms) ROLLBACK
ActiveRecord::RecordInvalid: Validation failed: Date is not a valid date
Is there no way to force Rails to explicitly error out when setting such a crap date? The standard rails date_select does not dynamically change the number of days in the days dropdown when you change to a different month. So you can select 31 September with the dropdowns, and the date will be saved as 1st of October. But I want to display an error message.
I don't want people to accidentally put the wrong dates into my system.
I think one of the solutions would be to change the datatype of date_of_birth from datetime to date. This way Rails would validate the date and would not allow the situation, you've shown.
It is also a good practice to store dates in date type, because it leads to significant decrease in potential issues, when working with those dates.
Try adding this to your model:
def date_of_birth=(d)
date = Date.strptime(d.to_s)
send(:write_attribute, :date_of_birth, date)
end
Now if you try setting an invalid date, it should raise an exception:
s.date_of_birth = "2015-09-31" # raises an exception
However, I don't think that doing this is a good idea. It's probably a better idea to do as Andrey Deineko said and store it in a date type.

Ruby string to Date object

I have a Mongoid field that is of type Date. I'm having all sorts of trouble searching for documents against this specific field. I receive dates as a string in this format: 10/20/2013. I thought something like Date.parse("10/20/2013") or "10/20/2013".to_date would be good enough to let me do something like MyModel.find_by(datefield: date_result) but this is giving me a ton of ArgumentError out of range type issues.
What's the easiest way to turn "10/20/2013" into a simple Date object that I can use to query against databases?
You get this:
Date.parse("10/20/2013")
ArgumentError: invalid date
The problem is 10/20. Ruby is an international language, and the values 10 and 20 are somewhat ambiguous. In the U.S. the "standard" date format is "MMDDYYYY", or %m%d%Y in date parsing terms. The majority of the world uses a different standard though, "DDMMYYYY" or %d%m%Y. Ruby uses the second format, with day first.
Looking at the difference, it's easy to see why Date.parse would be confused and complain. 10 is a sensible day, but 20 is nonsense as far as a month, so Ruby rejects it.
You can fix this by forcing the pattern used for parsing:
Date.strptime('10/20/2013', '%m/%d/%Y')
# => #<Date: 2013-10-20 ((2456586j,0s,0n),+0s,2299161j)>
You can use strptime:
Date.strptime('10/20/2013', '%m/%d/%Y')
=> <Date: 2013-10-20 ((2456586j,0s,0n),+0s,2299161j)>
Read this a list of possible formats
Date.parse("10/20/2013")
=> ArgumentError: invalid date
to
Date.parse("20/10/2013")
=> Sun, 20 Oct 2013

Date/Time conversion problems from Rails to Flex?

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

Resources