Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby: How to extract an hour (or day) from a date-time string

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!!

like image 596
pete Avatar asked May 02 '13 01:05

pete


People also ask

How to get the current time & date in Ruby?

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

How to extract time from datetime in R?

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 ()).

Is it possible to do time Math in pure Ruby?

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:

How many days are there in a year in Ruby?

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:


2 Answers

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.

like image 105
the Tin Man Avatar answered Nov 16 '22 00:11

the Tin Man


"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"
like image 29
fotanus Avatar answered Nov 16 '22 01:11

fotanus