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)
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.
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…
I played with Date.strptime but it seems to ignore the time stamps.
Clearly, as Date is about dates.
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)
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.
(I wish I could specify format for the Time parsing like I can with
Date parsing)
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
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.