算法计算一个星期#从一个日期与星期的自定义启动? (为iCal中) [英] algorithm for calculating a week # from a date with custom start of week? (for iCal)
问题描述
我只能找算法获得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
14.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 aWKST
day. (e.g. if Jan 1st is aWKST
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 todaysInFirstWeek
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 aWKST
day.3.3. if
daysInFirstWeekOfLastYear
is 4 or last year is Leap year anddaysInFirstWeekOfLastYear
is 5,weekNo
is 53, otherwiseweekNo
is 52, skip to step 5.Set
weekNo
toceiling((dayOfYear - daysInFirstWeek) / 7)
4.1. if
daysInFirstWeek
greater than or equal to 4, incrementweekNo
by 14.2. if
daysInFirstWeek
equal 53 and count of days on the first week (starting fromWKST
) of January in the year ofinputDate
's year + 1 is greater than or equal to 4, setweekNo
to 1return
weekNo
这篇关于算法计算一个星期#从一个日期与星期的自定义启动? (为iCal中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!