感谢您阅读我的问题。我正在完成EventManager教程(http://tutorials.jumpstartlab.com/projects/eventmanager.html#iteration:-day-of-the-week-targeting)。
我的日期输出有问题。这是我正在使用的方法。
def peak_days(day)
DateTime.strptime(day, '%m/%d/%Y %H:%M').wday
end
后来我在csv文件中迭代每一行。该文件的第一个日期是“11/12/08”,我预计将在周三返回“3”。相反,它返回“1”。
day = peak_days(row[:regdate])
有人可以告诉我为什么会这样吗?这是我完整的练习代码:
require 'csv'
require 'sunlight/congress'
require 'erb'
require 'date'
Sunlight::Congress.api_key = "e179a6973728c4dd3fb1204283aaccb5"
def clean_zipcode(zipcode)
zipcode.to_s.rjust(5, "0")[0..4]
end
def clean_phone(number)
clean_number = number.gsub(/[^\d]/, "")
if clean_number.length == 10
clean_number
elsif clean_number.length == 11
if clean_number.start_with?("1")
clean_number = number[1..-1]
else
clean_number = "0000000000"
end
else
clean_number = "0000000000"
end
end
def peak_hours(time)
DateTime.strptime(time, '%m/%d/%Y %H:%M').hour
end
def peak_days(day)
DateTime.strptime(day, '%m/%d/%Y %H:%M').wday
end
def legislators_by_zipcode(zipcode)
Sunlight::Congress::Legislator.by_zipcode(zipcode)
end
def save_thank_you_letters(id,form_letter)
Dir.mkdir("output") unless Dir.exists?("output")
filename = "output/thanks_#{id}.html"
File.open(filename,'w') do |file|
file.puts form_letter
end
end
puts "EventManager initialized."
contents = CSV.open 'event_attendees.csv', headers: true, header_converters: :symbol
template_letter = File.read "form_letter.erb"
erb_template = ERB.new template_letter
contents.each do |row|
id = row[0]
name = row[:first_name]
zipcode = clean_zipcode(row[:zipcode])
phone = clean_phone(row[:homephone])
time = peak_hours(row[:regdate])
day = peak_days(row[:regdate])
legislators = legislators_by_zipcode(zipcode)
form_letter = erb_template.result(binding)
save_thank_you_letters(id,form_letter)
end
答案 0 :(得分:0)
看起来1是A.D.8中日期的日期。
在格式字符串中尝试小写y:
def peak_days(day)
DateTime.strptime(day, '%m/%d/%y %H:%M').wday
end