From: John Benediktsson Date: Sat, 27 Aug 2022 16:21:10 +0000 (-0700) Subject: formatting: fix strftime %U and %W X-Git-Tag: 0.99~1104 X-Git-Url: https://gitweb.factorcode.org/gitweb.cgi?p=factor.git;a=commitdiff_plain;h=1198695223596ff2306a3204a097c00fad1f1cdc formatting: fix strftime %U and %W --- diff --git a/basis/formatting/formatting-tests.factor b/basis/formatting/formatting-tests.factor index b8a95c7888..8b4bf16b12 100644 --- a/basis/formatting/formatting-tests.factor +++ b/basis/formatting/formatting-tests.factor @@ -178,3 +178,19 @@ ${ os windows? "1.000000e+00" "1e+00" ? } [ 1.0 "%.0e" sprintf ] unit-test { "44" } [ 2020 11 6 "%U" strftime ] unit-test { "44" } [ 2020 11 6 "%W" strftime ] unit-test + +{ "00" } [ 2022 1 1 "%U" strftime ] unit-test +{ "01" } [ 2022 1 2 "%U" strftime ] unit-test +{ "01" } [ 2022 1 3 "%U" strftime ] unit-test + +{ "00" } [ 2022 1 1 "%W" strftime ] unit-test +{ "00" } [ 2022 1 2 "%W" strftime ] unit-test +{ "01" } [ 2022 1 3 "%W" strftime ] unit-test + +{ "34" } [ 2022 8 27 "%U" strftime ] unit-test +{ "35" } [ 2022 8 28 "%U" strftime ] unit-test +{ "35" } [ 2022 8 29 "%U" strftime ] unit-test + +{ "34" } [ 2022 8 27 "%W" strftime ] unit-test +{ "34" } [ 2022 8 28 "%W" strftime ] unit-test +{ "35" } [ 2022 8 29 "%W" strftime ] unit-test diff --git a/basis/formatting/formatting.factor b/basis/formatting/formatting.factor index 535133e7b6..1f5663c959 100644 --- a/basis/formatting/formatting.factor +++ b/basis/formatting/formatting.factor @@ -1,8 +1,8 @@ ! Copyright (C) 2008 John Benediktsson ! See http://factorcode.org/license.txt for BSD license USING: accessors arrays assocs calendar calendar.english -combinators combinators.smart generalizations io -io.streams.string kernel math math.functions +calendar.private combinators combinators.smart generalizations +io io.streams.string kernel math math.functions math.functions.integer-logs math.parser multiline namespaces peg.ebnf present prettyprint quotations sequences sequences.generalizations splitting strings unicode ; @@ -204,8 +204,9 @@ MACRO: sprintf ( format-string -- quot ) ] output>array join-words ; inline : week-of-year ( timestamp day -- n ) - [ dup clone 1 >>month 1 >>day day-of-week dup ] dip > [ 7 swap - ] when - [ day-of-year ] dip 2dup < [ 0 2nip ] [ - 7 / 1 + >fixnum ] if ; + [ dup clone first-day-of-year dup clone ] + [ day-this-week ] bi* swap '[ _ time- duration>days ] bi@ + dup 0 < [ 7 + - ] [ drop ] if 7 + 7 /i ; : week-of-year-sunday ( timestamp -- n ) 0 week-of-year ; inline