Date#parse assume a EU format - can it assume a US format?

Discussion in 'Ruby' started by Josh Sharpe, Sep 21, 2010.

  1. Josh Sharpe

    Josh Sharpe Guest

    Before you jump to an answer here, please don't suggest that I use
    _strptime. Going that route forces my users to give me the date and/or
    time in the format that I specify. Which is really not optimal.

    So the problem is that Date#parse assumes that the format is
    day/month/year. I would like to make it assume that it is in the US
    format, month/day/year.

    After digging into format.rb I tried this:

    class Date
    def _parse_eu(str, e)
    _parse_us(str,e)
    end
    end

    ... in an attempt to skip the parse_eu method.... that solution doesn't
    exactly work:

    ruby-1.9.2-p0 > Date.parse("10/04/2010")
    => Sat, 10 Apr 2010

    So, it's still using the EU format. Any other suggestions??

    Thanks!
    --
    Posted via http://www.ruby-forum.com/.
     
    Josh Sharpe, Sep 21, 2010
    #1
    1. Advertising

  2. Josh Sharpe wrote:
    > Before you jump to an answer here, please don't suggest that I use
    > _strptime. Going that route forces my users to give me the date and/or
    > time in the format that I specify. Which is really not optimal.
    >
    > So the problem is that Date#parse assumes that the format is
    > day/month/year.


    It is U.S. middle-endian format in 1.8.7. But try this monkey-patch for
    1.9.2:

    require 'date'
    def Date._parse_sla(str, e) # :nodoc:
    if str.sub!(%r|('?-?\d+)/\s*('?\d+)(?:\D\s*('?-?\d+))?|, ' ') # '
    s3e(e, $2, $1, $3)
    true
    end
    end

    >> Date.parse("10/04/2010")

    => #<Date: 2010-10-04 (4910947/2,0,2299161)>
    --
    Posted via http://www.ruby-forum.com/.
     
    Brian Candler, Sep 21, 2010
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brian Candy
    Replies:
    2
    Views:
    1,172
    Janaka
    Feb 18, 2004
  2. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,278
    Michael Borgwardt
    May 30, 2004
  3. linq936
    Replies:
    21
    Views:
    698
    Pete Becker
    Sep 19, 2007
  4. Francis Moreau
    Replies:
    17
    Views:
    516
    Tim Rentsch
    Jan 4, 2011
  5. Matteo
    Replies:
    4
    Views:
    156
    Dr John Stockton
    Jun 15, 2004
Loading...

Share This Page