Allow the user to set time zone

Fix timezone handling for periodic tasks
This commit is contained in:
eikek
2022-03-01 21:40:23 +01:00
parent 6f7eafcebc
commit 9545431d59
21 changed files with 186 additions and 72 deletions

View File

@ -12,6 +12,7 @@ module Data.CalEvent exposing
, makeEvent
)
import Data.TimeZone exposing (TimeZone)
import Util.Maybe
@ -22,12 +23,13 @@ type alias CalEvent =
, day : String
, hour : String
, minute : String
, timeZone : TimeZone
}
everyMonth : CalEvent
everyMonth =
CalEvent Nothing "*" "*" "01" "00" "00"
CalEvent Nothing "*" "*" "01" "00" "00" Data.TimeZone.utc
makeEvent : CalEvent -> String
@ -43,6 +45,8 @@ makeEvent ev =
++ ev.hour
++ ":"
++ ev.minute
++ " "
++ Data.TimeZone.toName ev.timeZone
in
case ev.weekday of
Just wd ->
@ -60,18 +64,40 @@ fromEvent str =
parts =
String.split " " str
foldChanges : List (CalEvent -> Maybe CalEvent) -> Maybe CalEvent
foldChanges list =
List.foldl (\fmc -> \c -> Maybe.andThen fmc c) (Just init) list
in
case parts of
wd :: date :: time :: [] ->
Maybe.andThen
(fromDate date)
(fromTime time init)
|> Maybe.map (withWeekday wd)
wd :: date :: time :: tz :: [] ->
foldChanges
[ fromWeekDays wd
, fromDate date
, fromTime time
, fromTimeZone tz
]
a :: b :: c :: [] ->
if startsWithWeekday a then
foldChanges
[ fromWeekDays a
, fromDate b
, fromTime c
]
else
foldChanges
[ fromDate a
, fromTime b
, fromTimeZone c
]
date :: time :: [] ->
Maybe.andThen
(fromDate date)
(fromTime time init)
foldChanges
[ fromDate date
, fromTime time
]
_ ->
Nothing
@ -109,6 +135,31 @@ fromTime time ev =
Nothing
fromTimeZone : String -> CalEvent -> Maybe CalEvent
fromTimeZone tzStr ev =
Data.TimeZone.get tzStr
|> Maybe.map (\tz -> { ev | timeZone = tz })
fromWeekDays : String -> CalEvent -> Maybe CalEvent
fromWeekDays str ce =
if startsWithWeekday str then
Just (withWeekday str ce)
else
Nothing
withWeekday : String -> CalEvent -> CalEvent
withWeekday wd ev =
{ ev | weekday = Util.Maybe.fromString wd }
weekDays : List String
weekDays =
[ "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" ]
startsWithWeekday : String -> Bool
startsWithWeekday str =
List.any (\a -> String.startsWith a str) weekDays