M: integer days-in-year ( year -- n ) leap-year? 366 365 ? ;
M: timestamp days-in-year ( timestamp -- n ) year>> days-in-year ;
-GENERIC: days-in-month ( obj -- n )
+: (days-in-month) ( year month -- n )
+ dup 2 = [ drop leap-year? 29 28 ? ] [ nip day-counts nth ] if ;
-M: array days-in-month ( obj -- n )
- first2 dup 2 = [
- drop leap-year? 29 28 ?
- ] [
- nip day-counts nth
- ] if ;
-
-M: timestamp days-in-month ( timestamp -- n )
- >date< drop 2array days-in-month ;
-
-GENERIC: day-of-week ( obj -- n )
+: days-in-month ( timestamp -- n )
+ >date< drop (days-in-month) ;
-M: timestamp day-of-week ( timestamp -- n )
+: day-of-week ( timestamp -- n )
>date< zeller-congruence ;
-M: array day-of-week ( array -- n )
- first3 zeller-congruence ;
-
-GENERIC: day-of-year ( obj -- n )
-
-M: array day-of-year ( array -- n )
- first3
- 3dup day-counts rot head-slice sum +
- swap leap-year? [
- -roll
- pick 3 1 <date> >r <date> r>
+:: (day-of-year) ( year month day -- n )
+ day-counts month head-slice sum day +
+ year leap-year? [
+ year month day <date>
+ year 3 1 <date>
after=? [ 1+ ] when
- ] [
- >r 3drop r>
- ] if ;
+ ] when ;
-M: timestamp day-of-year ( timestamp -- n )
- >date< 3array day-of-year ;
+: day-of-year ( timestamp -- n )
+ >date< (day-of-year) ;
: day-offset ( timestamp m -- timestamp n )
over day-of-week - ; inline
\r
M: array month. ( pair -- )\r
first2\r
- [ month-names nth write bl number>string print ] 2keep\r
- [ 1 zeller-congruence ] 2keep\r
- 2array days-in-month day-abbreviations2 " " join print\r
+ [ month-names nth write bl number>string print ]\r
+ [ 1 zeller-congruence ]\r
+ [ (days-in-month) day-abbreviations2 " " join print ] 2tri\r
over " " <repetition> concat write\r
[\r
[ 1+ day. ] keep\r