Ruby 1.9.2 Date issues

K

keldog

Hello all...

Wondering if someone can help me out with a date issue.

My environment:
ruby: 1.9.2p0
rails: 3.0.3
mac os x

My issue:

From what I've been reading, the date format has been changed in
1.9.2. So, saving dates in the "mm/dd/yyy" format has been an issue
since 1.9.2 is saving as "dd/mm/yyyy"

I have a jQuery plugin that uses the date picker and displays in the
format of "mm/dd/yyyy" However, sometimes the date saves and other
times it doesn't because of ruby expecting the changed format (mm/dd/
yyyy). For example:

3/1/2011 saves as 1/3/2011
3/13/2011 doesn't save (returns "")

I have the following code in my model to inspect the date (in my case,
start_date of a course):

Code:
before_filter :fix_dates

private

def fix_dates
raise self.start_date.to_s.inspect
end

Tried the following:
1) In the /config/initializers directory, created a date_format.rb to
change the date. However, that's for display purposes only (I
believe).

2) In the fix_dates function above, I added:
self.start_date = Date.strptime(self.start_date.to_s,'%m/%d/%Y').to_s
But this isn't valid since ruby will set the date to an empty string
before executing this code.

I think there is a really simple solution for this work around, but
I've failed to find one thus far. Any help is greatly appreciated!

-K
 
R

Robert Klemme

Hello all...

Wondering if someone can help me out with a date issue.

My environment:
ruby: 1.9.2p0
rails: 3.0.3
mac os x

My issue:

From what I've been reading, the date format has been changed in
1.9.2. =A0So, saving dates in the "mm/dd/yyy" format has been an issue
since 1.9.2 is saving as "dd/mm/yyyy"

What exactly do you mean by "saving"? Both "mm/dd/yyy" and
"dd/mm/yyyy" are just textual representations of a Date. If you want
to be sure what conversion is used you can use Date#strftime:

09:44:35 ~$ irb19 -r date
Ruby version 1.9.2
irb(main):001:0> d =3D Date.today
=3D> #<Date: 2011-01-11 (4911145/2,0,2299161)>
irb(main):002:0> d.to_s
=3D> "2011-01-11"
irb(main):003:0> d.strftime '%m/%d/%Y'
=3D> "01/11/2011"
irb(main):004:0> d.strftime '%d/%m/%Y'
=3D> "11/01/2011"
irb(main):005:0>

If you save via Marshal there should be no issues. Note also that
there are some built in formats:

irb(main):012:0> d.rfc3339
=3D> "2011-01-11"
irb(main):013:0> d.rfc2822
=3D> "Tue, 11 Jan 2011 00:00:00 +0000"
irb(main):014:0> d.rfc822
=3D> "Tue, 11 Jan 2011 00:00:00 +0000"
I have a jQuery plugin that uses the date picker and displays in the
format of "mm/dd/yyyy" =A0However, sometimes the date saves and other
times it doesn't because of ruby expecting the changed format (mm/dd/
yyyy). =A0For example:

3/1/2011 saves as 1/3/2011
3/13/2011 doesn't save (returns "")

I would expect the date picker to return a Date and not a String.
What am I missing?
I have the following code in my model to inspect the date (in my case,
start_date of a course):

Code:
=A0before_filter :fix_dates

=A0private

=A0def fix_dates
=A0 =A0raise self.start_date.to_s.inspect
=A0end

Tried the following:
1) In the /config/initializers directory, created a date_format.rb to
change the date. =A0However, that's for display purposes only (I
believe).

2) In the fix_dates function above, I added:
self.start_date =3D Date.strptime(self.start_date.to_s,'%m/%d/%Y').to_s
But this isn't valid since ruby will set the date to an empty string
before executing this code.

I think there is a really simple solution for this work around, but
I've failed to find one thus far. =A0Any help is greatly appreciated!

Maybe you changed your environment and now a different default format
for your locale is chosen.

Kind regards

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
R

Rick DeNatale

What exactly do you mean by "saving"? =A0Both "mm/dd/yyy" and
"dd/mm/yyyy" are just textual representations of a Date. =A0If you want
to be sure what conversion is used you can use Date#strftime:

I'm pretty sure he's talking about how Date.parse interprets a string
of the form "1/2/2011"

This form is ambiguous of course, in the US it's normal meaning would
be January 1, 2011, where in most of the civilized world it would be
February 1, 2011

Date.parse in ruby 1.8.6 tries to use heuristics to decide how to
resolve the ambiguity. Ruby 1.9 (and IIRC 1.8.7 as well) did away with
this and simply interprets such an input string as February 1.
09:44:35 ~$ irb19 -r date
Ruby version 1.9.2
irb(main):001:0> d =3D Date.today
=3D> #<Date: 2011-01-11 (4911145/2,0,2299161)>
irb(main):002:0> d.to_s
=3D> "2011-01-11"
irb(main):003:0> d.strftime '%m/%d/%Y'
=3D> "01/11/2011"
irb(main):004:0> d.strftime '%d/%m/%Y'
=3D> "11/01/2011"
irb(main):005:0>

If you save via Marshal there should be no issues. =A0Note also that
there are some built in formats:

irb(main):012:0> d.rfc3339
=3D> "2011-01-11"
irb(main):013:0> d.rfc2822
=3D> "Tue, 11 Jan 2011 00:00:00 +0000"
irb(main):014:0> d.rfc822
=3D> "Tue, 11 Jan 2011 00:00:00 +0000"

True but probably mostly irrelevant if I'm interpreting the OPs
questions correctly.
I would expect the date picker to return a Date and not a String.
What am I missing?

The date picker is javascript in a browser, the 'date' needs to be
transported via a parameter in a HTTP post, which only allows strings.
So the date needs to arrive at the server in the form of a string,
which then gets parsed into a ruby date, and this is ultimately done
by Date._parse which is the method whose semantics have changed in
Ruby 1.8.7/1.9
I have the following code in my model to inspect the date (in my case,
start_date of a course):

Code:
=A0before_filter :fix_dates[/QUOTE][/QUOTE]

before_filter is a controller method, not a model method isn't it?
Sorry pure-ruby folks but the OPs question is in the context or Rails.

To fix this I think the OP wants to do the conversion from the string
in the controller, so that it's already a ruby Date or DateTime by the
time AR sees it.  Exactly how to do this depends on the controller
itself.  Asuming that the model involved is Foo, and we have a
standard FoosController and a standard form being posted, something
like this:

def create
if (params[:foo] && date_string =3D params[:foo][:start_date])
params[:start_date] =3D Date.strptime(date_string), "%m/%d/%Y")
end

[USER=55271]@foo[/USER] =3D Foo.create(params[:foo])
if @foo.save
#handle case when foo is valid
else
#handle validation errors
end
end

HTH

--=20
Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Github: http://github.com/rubyredrick
Twitter: @RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top