PaperKraftMike
PaperKraftMike

Reputation: 99

Ruby strptime method error

So I am working through a tutorial building an event manager program and I'm a bit stuck. I want to build a method that will take registration data from a .csv file and then extract the hour times when people registered. However I'm having trouble getting it to work. Here's what I have so far:

def reg_hour(regtime)
    regtime = DateTime.new
    DateTime.strptime(regtime, "%H")

end

When I run the code though I get this error:

 `block in _strptime_i': undefined method `sub!' for #<DateTime: -4712-01-01T00:00:00+00:00 (-1/2,0,2299161)> (NoMethodError)

I am quite confused and any help would be greatly appreciated.

Here's the link to the tutorial if anyone is interested. http://tutorials.jumpstartlab.com/projects/eventmanager.html

Upvotes: 0

Views: 539

Answers (2)

varnie
varnie

Reputation: 2595

I am not quite sure I fully understand your intentions, but here it is rewritten:

require 'time'

def reg_hour(regtime)
    DateTime.strptime(regtime, "%H")
end

d = reg_hour("21/03/2011 14:39:11.642")
puts d.year

Is this something you're trying to do?

Upvotes: 1

drusolis
drusolis

Reputation: 862

Not sure what you're doing with the DateTime.new and overriding the regtime variable (I'm new to ruby myself). If the regtime is coming out of a csv file, it's probably coming out as a string. Perhaps you could use a regular expression as long as the regdate format is consistent.

If regdate is: "11/12/08 10:47"

Then using:

regdate.scan(/\s\d+:/)

Would return [" 10:"]. Perhaps then you could store that in a array variable and clean it up by removing white space and the colon. There's probably a more elegant solution, but that's my newbie brute force way.

Upvotes: 1

Related Questions