无法使用方法 Date.commercial 找到第 53 周的开始

Not able to find start of 53rd week using method Date.commercial

Date.commercial 查找周开始时间(星期一)的方法在第 53 周无效

Date.commercial(2018, 1, 1)
#=> 2018-01-01
Date.commercial(2018, 52, 1)
#=> 2018-12-24

当我试图在有 53 周的年份中查找第 53 周的星期一时出现错误。

Date.commercial(2018, 53, 1)
# ArgumentError: invalid date

另外,它的定义我也不清楚。就像这两者的行为如此不同:

Date.commercial(2015, 1, 1)
#=> 2014-12-29

Date.commercial(2016, 1, 1)
#=> 2016-01-04

我认为是因为 Date.commercial() 将周数转换为日期的定义与 Date.strftime("%W") 的定义不同。有没有什么方法可以将年的周数转换成Date.strftime("%W")一致的星期几,即Date.strftime("%W")的逆函数?

来自 API 文档:

the first calendar week of the year is the one that includes the first Thursday of that year. In the Gregorian calendar, this is equivalent to the week which includes January 4.

因此,2018 年没有 53 周。 2018年最后一周的最后一天是:

Date.commercial(2018, 52, 7)
# => #<Date: 2018-12-30 ((2458483j,0s,0n),+0s,2299161j)> 

接下来是 2019 年的第一周:

Date.commercial(2019, 1, 1)
# => #<Date: 2018-12-31 ((2458484j,0s,0n),+0s,2299161j)> 

你好像是在2018-12-31是2018年的前提下工作的,不是,不在Commercial date。如果您要使用商业年而不是日历年,请使用 %G,而不是 %Y 来获取商业年,并使用 %V 来表示周而不是 [=16] =].

date2018_12_31 = Date.commercial(2019, 1, 1)
date2018_12_31.strftime("%Y %W")                  # WRONG
# => "2018 53"
date2018_12_31.strftime("%G %V")                  # CORRECT
# => "2019 01"

Is there any method which can convert year's week number to Monday date of the week consistent with Date.strftime("%W"), i.e. reverse function of Date.strftime("%W")?

您可以使用 strptime:

Date.strptime('2018-W53', '%Y-W%W')
#=> #<Date: 2018-12-31 ((2458484j,0s,0n),+0s,2299161j)>

请注意 %W%V(商业周)的定义不同并产生不同的结果。

%W 定义为:

The week 1 of YYYY starts with a Monday. The days in the year before the first week are in week 0.

%V 定义为:

The week 1 of YYYY starts with a Monday and includes YYYY-01-04. The days in the year before the first week are in the last week of the previous year.

一些例子:

Date.new(2018,  1,  1).strftime('%Y-W%W / %G-CW%V') #=> "2018-W01 / 2018-CW01"
Date.new(2018, 12, 31).strftime('%Y-W%W / %G-CW%V') #=> "2018-W53 / 2019-CW01"

Date.new(2019,  1,  1).strftime('%Y-W%W / %G-CW%V') #=> "2019-W00 / 2019-CW01"
Date.new(2019, 12, 31).strftime('%Y-W%W / %G-CW%V') #=> "2019-W52 / 2020-CW01"

Date.new(2020,  1,  1).strftime('%Y-W%W / %G-CW%V') #=> "2020-W00 / 2020-CW01"
Date.new(2020, 12, 31).strftime('%Y-W%W / %G-CW%V') #=> "2020-W52 / 2020-CW53"

Date.new(2021,  1,  1).strftime('%Y-W%W / %G-CW%V') #=> "2021-W00 / 2020-CW53"
Date.new(2021, 12, 31).strftime('%Y-W%W / %G-CW%V') #=> "2021-W52 / 2021-CW52"