1

感谢您阅读我的问题。我正在学习 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
4

1 回答 1

0

看起来 1 是 AD8 中日期的星期几。

在格式字符串中尝试小写 y:

def peak_days(day)
  DateTime.strptime(day, '%m/%d/%y %H:%M').wday
end
于 2013-08-20T22:19:09.327 回答