}
} ;
+HELP: week-number
+{ $values { "timestamp" timestamp } { "[1,53]" integer } }
+{ $description "Calculates the ISO 8601 week number from 1 to 53 (leap years). See " { $snippet "https://en.wikipedia.org/wiki/ISO_week_date" } }
+{ $examples
+ "Last day of 2018 is already in the first week of 2019."
+ { $example "USING: calendar prettyprint ;"
+ "2018 12 31 <date> week-number ."
+ "1"
+ }
+ "2020 is a leap year with 53 weeks, and January 1st, 2021 is still in week 53 of 2020."
+ { $example "USING: calendar prettyprint ;"
+ "2021 1 1 <date> week-number ."
+ "53"
+ }
+} ;
+
HELP: sunday
{ $values { "timestamp" timestamp } { "new-timestamp" timestamp } }
{ $description "Returns the Sunday from the current week, which starts on a Sunday." } ;
days-in-month
day-of-year
day-of-week
+ week-number
}
"Calculating a Julian day number:"
{ $subsections julian-day-number }
! pm
[ now 30 pm ] [ not-in-interval? ] must-fail-with
+
+{ 1 } [ 2018 12 31 <date> week-number ] unit-test
+
+{ 16 } [ 2019 4 17 <date> week-number ] unit-test
+
+{ 53 } [ 2021 1 1 <date> week-number ] unit-test
: unix-time>timestamp ( seconds -- timestamp )
[ unix-1970 ] dip +second ; inline
+: (week-number) ( timestamp -- [0,53] )
+ [ day-of-year ] [ day-of-week [ 7 ] when-zero ] bi - 10 + 7 /i ;
+
+: week-number ( timestamp -- [1,53] )
+ dup (week-number) {
+ { 0 [ year>> 1 - end-of-year (week-number) ] }
+ { 53 [ year>> 1 + <year> (week-number) 1 = 1 53 ? ] }
+ [ nip ]
+ } case ;
+
{
{ [ os unix? ] [ "calendar.unix" ] }
{ [ os windows? ] [ "calendar.windows" ] }