算法计算一个星期#从一个日期与星期的自定义启动? (为iCal中)自定义、一个星期、算法、星期

2023-09-11 07:13:39 作者:相思化成泪

我只能找算法获得ISO 8601周(一周开始于星期一)。

不过,的iCal 规范说

  

一个周被定义为经历七天的时间,开始上的的天   周定义为周开始(见WKST)。周数的所述一个   历年是包含至少四(4)天的第一周   在该日历年。

因此​​,它比ISO 8601更复杂,因为周开始可以是每周的任何一天。

有没有一种算法来确定什么是日期的周数,每周有一个自定义开始日期?

或...有一个函数 iCal4j 这样做吗?从日期确定weekno?

谢谢!

P.S。限量版:我用一个JVM语言不能扩展的Java类,但我可以调用Java方法或者实例化的Java类

解决方案

daysInFirstWeek 是天数上都在一月份的一年的第一周。周开始于一个 WKST 天。 (例如,如果1月1日是一个 WKST 一天,回到7)

设置 DAYOFYEAR 来输入日期的年的第n个工作日(例如,2月1日= 32)

怎么能把计算机右下角同时能显示出日期 时间 星期几

如果 DAYOFYEAR 小于或等于 daysInFirstWeek

3.1。如果 daysInFirstWeek 大于或等于4, weekNo 1,跳到第5步。

3.2。让 daysInFirstWeekOfLastYear 是天在previous今年是在1月第一周数。周开始于一个 WKST 的日子。

3.3。如果 daysInFirstWeekOfLastYear 4或去年是闰年和 daysInFirstWeekOfLastYear 5, weekNo 53,否则 weekNo 52,跳到第5步。

设置 weekNo 上限((DAYOFYEAR - daysInFirstWeek)/ 7)

4.1。如果 daysInFirstWeek 大于或等于4,增加 weekNo 1

4.2。如果 daysInFirstWeek 上的第一个星期等于53天和数月的一年中(从 WKST 启动) inputDate 的年份+ 1大于或等于4,设置 weekNo 1

返回 weekNo

I can only find algorithm for getting ISO 8601 week (week starts on a Monday).

However, the iCal spec says

A week is defined as a seven day period, starting on the day of the week defined to be the week start (see WKST). Week number one of the calendar year is the first week that contains at least four (4) days in that calendar year.

Therefore, it is more complex than ISO 8601 since the start of week can be any day of the week.

Is there an algorithm to determine what is the week number of a date, with a custom start day of week?

or... is there a function in iCal4j that does this? Determine a weekno from a date?

Thanks!

p.s. Limitation: I'm using a JVM language that cannot extend a Java class, but I can invoke Java methods or instantiate Java classes.

解决方案

Let daysInFirstWeek be the number of days on the first week of the year that are in January. Week starts on a WKST day. (e.g. if Jan 1st is a WKST day, return 7)

Set dayOfYear to the n-th days of the input date's year (e.g. Feb 1st = 32)

If dayOfYear is less than or equal to daysInFirstWeek

3.1. if daysInFirstWeek is greater than or equal to 4, weekNo is 1, skip to step 5.

3.2. Let daysInFirstWeekOfLastYear be the number of days on the first week of the previous year that are in January. Week starts on a WKST day.

3.3. if daysInFirstWeekOfLastYear is 4 or last year is Leap year and daysInFirstWeekOfLastYear is 5, weekNo is 53, otherwise weekNo is 52, skip to step 5.

Set weekNo to ceiling((dayOfYear - daysInFirstWeek) / 7)

4.1. if daysInFirstWeek greater than or equal to 4, increment weekNo by 1

4.2. if daysInFirstWeek equal 53 and count of days on the first week (starting from WKST) of January in the year of inputDate's year + 1 is greater than or equal to 4, set weekNo to 1

return weekNo