我正在尝试创建一个年周列表(相当于Rails中mySQL的YEARWEEK(date,1))
位于两个日期值之间。如果开始日期和结束日期位于同一年,则列表将完美生成。以下是我的代码:
campaign_start_date = "2013-08-02 06:59:00"
campaing_end_date = "2013-09-01 06:59:00"
start_year = DateTime.parse(campaign_start_date).cwyear
start_week = "%04d%02d" % [start_year, DateTime.parse(campaign_start_date).cweek]
end_year = DateTime.parse(campaing_end_date).cwyear
end_week = "%04d%02d" % [end_year, DateTime.parse(campaing_end_date).cweek]
if start_year == end_year
(start_week..end_week).each{ |i| result << i }
else
# need to build a suitable logic here. to handle the case when duration spans over multiple years. for example started in 01-Nov-14 and ended in 01-May-15
end
return result
上面的日期值不会有问题,它会落在if的情况下,我会得到的结果是:
[
"201331",
"201332",
"201332",
"201333",
"201334",
"201335"
]
这也是我想要的。但是,如果我的开始日期和结束日期值是例如:
campaign_start_date = "2014-07-23 06:59:00"
campaing_end_date = "2015-03-01 06:59:00"
意味着落在不同的年份,那么它需要与我在if
条件下的逻辑不同,因为对于这些日期值(start_week=201430
和end_week=201509
),if
条件在这里不合适,因为它会生成80
值,这是错误的,因为这些日期之间的周数不是80
。需要帮助开发else
案例的逻辑。这可能很容易,但现在我只是厌倦了再深入挖掘。
特别注意:解决方案应关注商业年度和商业周(参见铁轨的.cwyer和.cweek函数)。例如,2016-01-01
的年度周将是201553
,而不是201601
在这方面的任何帮助都将不胜感激。
感谢那些回复t的人。我终于解决了这样的问题:
campaign_weeks = []
campaign_start_date = "2014-07-23 06:59:00" # or any date
campaing_end_date = "2015-03-01 06:59:00" # or any date
start_year = DateTime.parse(campaign_start_date).cwyear
start_cweek_of_the_campaign = "%04d%02d" % [start_year, DateTime.parse(campaign_start_date).cweek]
end_year = DateTime.parse(campaing_end_date).cwyear
end_cweek_of_the_campaign = "%04d%02d" % [end_year, DateTime.parse(campaing_end_date).cweek]
if start_year == end_year
(start_cweek_of_the_campaign..end_cweek_of_the_campaign).each do |w|
campaign_weeks << ("%04d%02d" % [start_year, w])
end
else
(start_year..end_year).each do |y|
first_cweek_number_of_the_year = (y == start_year) ? start_cweek_of_the_campaign : 1
last_cweek_number_of_the_year = (y == end_year) ? end_cweek_of_the_campaign : DateTime.new(y, 12, 28).cweek
(first_cweek_number_of_the_year .. last_cweek_number_of_the_year).each do |w|
campaign_weeks << ("%04d%02d" % [y, w])
end
end
end
return campaign_weeks
注:12月28日总是在一年中的最后一周。一年中最后一个ISO周是52或53。
参考:http://en.wikipedia.org/wiki/ISO_week_date#Last_week
从这个答案中得到了一些提示:使用Ruby 计算一年中的周数
瓶颈是(start_week..end_week)
范围。它显然超过了一百(因为我们是小数):
2014xx ⇒ 201452 ⇒ 201453 ⇒ ... ⇒ 201499 ⇒ 201500 ⇒ ...
你可能应该过滤你的范围,比如:
r = (start_week..end_week)
r.to_a.reject { |e| e[-2..-1].to_i > 52 }
根据您计算周数的方式(=基于-或基于1),201500
可能也会被过滤:/
r.to_a.select { |e| e[-2..-1].to_i.between? 1, 52 }
试试这个;无论年份是否相同,它都适用于任何一组日期:
campaign_start_date = "2014-07-23 06:59:00"
campaign_end_date = "2015-03-01 06:59:00"
start_date = DateTime.parse(campaign_start_date)
end_date = DateTime.parse(campaign_end_date)
while start_date < end_date
puts "%04d%02d" % [start_date.cw_year, start_date.cweek]
start_date = start_date + 7.days
end
讨论有点晚,但以下是我过去得到的两个日期之间的商业周数:
def cweek_diff(start_date, end_date)
return if end_date < start_date
cweek_diff = (end_date.cweek - start_date.cweek) + 1
cwyear_diff = end_date.cwyear - start_date.cwyear
cyear_diff * 53 + cweek_diff - cwyear_diff
end
它在我的情况下非常有效。希望它能有所帮助;)