Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "week of year" expression #173

Merged
merged 5 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading