Skip to content

Commit

Permalink
Merge pull request #173 from isovector/master
Browse files Browse the repository at this point in the history
Add "week of year" expression
  • Loading branch information
nomeata authored Mar 20, 2024
2 parents 4bfacd2 + f96ff64 commit 1a50c0f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
4 changes: 4 additions & 0 deletions doc/arbtt.xml
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,9 @@
Expression <literal>day of week $date</literal> evaluates to an integer,
from 1 to 7, corresponding to the day of week, Monday is 1, Sunday is 7.
These expressions can be compared to integers.
Expression <literal>week of year $date</literal> evaluates to an integer,
from 0 to 53, corresponding to the week of year. January 1 falls in week 0.
These expressions can be compared to integers.
</para>

<para>
Expand Down Expand Up @@ -452,6 +455,7 @@
<rhs> <quote>$idle</quote> </rhs>
<rhs> <quote>day of week</quote> <nonterminal def="#g-date" /> </rhs>
<rhs> <quote>day of month</quote> <nonterminal def="#g-date" /> </rhs>
<rhs> <quote>week of year</quote> <nonterminal def="#g-date" /> </rhs>
<rhs> <quote>month</quote> <nonterminal def="#g-date" /> </rhs>
<rhs> <quote>year</quote> <nonterminal def="#g-date" /> </rhs>
<rhs> number literal </rhs>
Expand Down
10 changes: 10 additions & 0 deletions src/Categorize.hs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ parseCondExpr = buildExpressionParser [
[ Prefix (reservedOp lang "!" >> return checkNot) ],
[ Prefix (reserved lang "day of week" >> return evalDayOfWeek)
, Prefix (reserved lang "day of month" >> return evalDayOfMonth)
, Prefix (reserved lang "week of year" >> return evalWeekOfYear)
, Prefix (reserved lang "month" >> return evalMonth)
, Prefix (reserved lang "year" >> return evalYear)
, Prefix (reserved lang "format" >> return formatDate) ],
Expand Down Expand Up @@ -354,6 +355,15 @@ evalDayOfMonth cp = Left $ printf
"Cannot apply day of month to an expression of type %s, only to $date."
(cpType cp)

-- Week of year is an integer in [0..53].
evalWeekOfYear :: CondPrim -> Erring CondPrim
evalWeekOfYear (CondDate df) = Right $ CondInteger $ \ctx ->
let tz = zonedTimeZone (cCurrentTime ctx) in
(toInteger . snd3 . toWeekDate . localDay . utcToLocalTime tz) `fmap` df ctx
evalWeekOfYear cp = Left $ printf
"Cannot apply week of year to an expression of type %s, only to $date."
(cpType cp)

-- Month is an integer in [1..12].
evalMonth :: CondPrim -> Erring CondPrim
evalMonth (CondDate df) = Right $ CondInteger $ \ctx ->
Expand Down

0 comments on commit 1a50c0f

Please sign in to comment.