Ruby 1.9 Time parse question

Discussion in 'Ruby' started by Paul, Jun 7, 2011.

  1. Paul

    Paul Guest

    Hi there!

    I have a ruby program that works as expected with 1.8.6 and 1.8.7. I
    just discovered some failures with some functions when i ran it in
    1.9.2.

    The line that is breaking is:
    the data is an AoA and looks like:
    data_array = [ ['1', '04/14/2011', '10:00 am', 'foo'],
    ['2', '04/18/2011', '03:30 pm', 'bar'],
    ['3', '04/18/2011', '11:15 am', 'baz']]

    => so I expect the elements to resort as: 1, 3, 2.

    I want to use code that will work in both 1.8 and 1.9 using the
    standard Ruby libraries for maximum portability.

    In 1.8, the Time.parse method works with the 'mm/dd/yyyy' format,
    while in 1.9 it is looking for 'dd/mm/yyyy'.

    Is there a nice clean way to sort this array by date and time that
    will work in both 1.8 and 1.9?

    Paul.
     
    Paul, Jun 7, 2011
    #1
    1. Advertisements

  2. You should give the format explicitly:

    16:22:52 ~$ allruby -r date -e 'puts Date.strptime("04/14/2011", "%m/%d/%Y"=
    )'
    CYGWIN_NT-5.1 padrklemme2 1.7.9(0.237/5/3) 2011-03-29 10:10 i686 Cygwin
    =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
    =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
    ruby 1.8.7 (2008-08-11 patchlevel 72) [i386-cygwin]
    2011-04-14
    =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
    =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
    ruby 1.9.2p180 (2011-02-18 revision 30909) [i386-cygwin]
    2011-04-14
    =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
    =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
    jruby 1.6.1 (ruby-1.8.7-p330) (2011-04-12 85838f6) (Java HotSpot(TM)
    Client VM 1.6.0_25) [Windows XP-x86-java]
    2011-04-14
    =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
    =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
    jruby 1.6.1 (ruby-1.9.2-p136) (2011-04-12 85838f6) (Java HotSpot(TM)
    Client VM 1.6.0_25) [Windows XP-x86-java]
    2011-04-14
    =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
    =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D

    You can as well do the parsing yourself:

    16:25:00 ~$ allruby -r date -e 'm,d,y=3D"04/14/2011".scan(/\d+/).map
    {|x|x.to_i};puts Date.new(y,m,d)'
    CYGWIN_NT-5.1 padrklemme2 1.7.9(0.237/5/3) 2011-03-29 10:10 i686 Cygwin
    =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
    =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
    ruby 1.8.7 (2008-08-11 patchlevel 72) [i386-cygwin]
    2011-04-14
    =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
    =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
    ruby 1.9.2p180 (2011-02-18 revision 30909) [i386-cygwin]
    2011-04-14
    =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
    =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
    jruby 1.6.1 (ruby-1.8.7-p330) (2011-04-12 85838f6) (Java HotSpot(TM)
    Client VM 1.6.0_25) [Windows XP-x86-java]
    2011-04-14
    =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
    =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
    jruby 1.6.1 (ruby-1.9.2-p136) (2011-04-12 85838f6) (Java HotSpot(TM)
    Client VM 1.6.0_25) [Windows XP-x86-java]
    2011-04-14
    =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
    =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D


    Kind regards

    robert

    --=20
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
     
    Robert Klemme, Jun 7, 2011
    #2
    1. Advertisements

  3. Paul

    Paul Guest

    Hello Robert, thank you for the reply.

    I played with Date.strptime but it seems to ignore the time stamps.

    I need to use the time stamps to sort the records correctly. My
    current code joins the date and time and parses them together using
    the Time library so that it can sort it based on date+time stamp.

    Is there a way I can use the Date parser so that it factors in the date
    +time?
    (if there is, I haven't figured it out yet)

    Thanks. Paul.

    (I wish I could specify format for the Time parsing like I can with
    Date parsing)


     
    Paul, Jun 7, 2011
    #3
  4. Please do not top post.

    Clearly, as Date is about _dates_. :)
    My bad. I overlooked that you want time as well. Then use class Time
    or DateTime instead. They all have method strptime. Legal format
    patterns are in the docs.
    You can.

    Btw, I'd rather convert strings on insertion into the Array. It's
    usually best to convert input into specific types when one sees it.
    Then you can use all the specific Date / Time / DateTime functionality
    in your application and only convert back to string when you output
    results. Also, often specific data needs less memory than data left
    in strings. Also, converting only once is more efficient than doing
    it over and over again. In your case you could as well use
    Array#sort_by to convert each timestamp only once.

    Kind regards

    robert
     
    Robert Klemme, Jun 7, 2011
    #4
  5. Paul

    Paul Guest

    Hello Robert, I was thinking the same thing. The biggest difference is
    between re-writing two sort routines and re-writing a third of the
    entire program.

    Looks like I have some refactoring to do this week..

    Cheers! Paul.
     
    Paul, Jun 7, 2011
    #5
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.