I'm pulling date-time strings from a large CSV file, which look like this:
"11/19/2008 21:56"
I'd like to extract the hour only, so I can build a histogram of all the hours to find the most frequent ones. Similarly, I'd like to extract days of the week (names) from the dates and build a histogram of most frequent days.
I'm new to Ruby, looked up the information, for starters tried various forms of the following, but no luck:
require 'date'
puts DateTime.strptime("11/19/2008 21:56", '%I')
Can you please advise a simple (and clear) way to accomplish the above? Also, any suggestions how to represent the results would be great. I'm thinking one hash array for the hours (24 entries) and one for the days (7 entries)? What would be the neatest algorithm to load them up as I iterate through the date-time strings, and then maybe re-sorting them with most frequent on top? Thanks!!
Let’s do this! You can use the Time class in Ruby to represent a time & date. This information is stored by the Time class as the number of seconds since the Epoch, also known as Unix time. You can get an object that represents the current time using Time.now You can create a Time object using an Unix timestamp & the at method
Using the as_hms () function, on the other hand, will extract time, into a time object. In this short R tutorial, you have learned how to extract time from datetime using as.POSIXct (), format, and lubridate (i.e., dmy_hms ()).
These methods are not available in pure Ruby, they are added by the ActiveSupport component of Rails. Here you can find some examples, notice how these methods don’t return Time or Date objects, but a custom ActiveSupport class. You can do time math with these & get things like tomorrow’s date:
For the current day number, out of 365 days in a year: Now that you know how to display time on screen, you can begin to manipulate it. Ruby allows you to format time as you please. First, let’s take a simple example on how to manipulate time:
This is the starting point:
dt = "11/19/2008 21:56"
require 'date'
DateTime.strptime(dt, '%m/%d/%Y %H:%M') # => #<DateTime: 2008-11-19T21:56:00+00:00 ((2454790j,78960s,0n),+0s,2299161j)>
Date formats like "11/19/2008" present a problem when parsing because the default is to use this format:
'%d/%m/%Y'
Date blows up when it sees a month value of 19
. '%m/%d/%Y'
is not as popular around the world as '%d/%m/%Y'
, which is why Ruby defaults to it.
Once you have the timestamp parsed, you can easily extract parts from it:
datetime = DateTime.strptime(dt, '%m/%d/%Y %H:%M')
datetime.hour # => 21
datetime.wday # => 3
Notice that wday
returns values from 0..6
, not 1..7
, where 0 = Sunday
:
%w[Sunday Monday Tuesday Wednesday Thursday Friday Saturday][datetime.wday]
# => "Wednesday"
Rails' ActiveSupport has a lot of useful methods as part of its Date, DateTime and Time support. Using them is easy, and it's easy to cherry-pick which you want if you decide to add them to plain-ol' Ruby code.
"11/19/2008 21:56".split[1]
=> "21:56"
If can be in other formats, but always the only part with a ":" and two digits on each side, you can use
"11/19/2008 21:56"[/\d{2}:\d{2}/]
=> "21:56"
And for day, something similar
"11/19/2008 21:56"[/\d{2}\/\d{2}\/\d{4}/]
=> "11/19/2008"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With