Skip to content

Commit 733a359

Browse files
committed
Parse time ranges from ics files
1 parent 7fb04fb commit 733a359

File tree

1 file changed

+42
-10
lines changed

1 file changed

+42
-10
lines changed

Caligraph/ICalendar/Backend.hs

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,45 @@ reloadFile = do
111111
eitherResult <- ICal.parseICalendarFile def fp
112112
return eitherResult
113113

114+
vevent2incarnation :: ICal.VEvent -> ICal.DTStart -> a -> CB.Incarnation a
115+
vevent2incarnation event start itemId =
116+
CB.Incarnation day time duration summary itemId
117+
where
118+
projHM :: TimeOfDay -> (Int,Int)
119+
projHM tod = (todHour tod, todMin tod)
120+
difftime2HM :: DiffTime -> (Int,Int)
121+
difftime2HM difft =
122+
let picosec = diffTimeToPicoseconds difft in
123+
let tenExp12 = 1000000000000 in
124+
let mindiff = fromIntegral ((picosec `div` tenExp12) `div` 60) in
125+
(mindiff `div` 60, mindiff `mod` 60)
126+
127+
datetime2tuple :: ICal.DateTime -> (Day, Maybe (Int,Int))
128+
datetime2tuple d =
129+
case d of
130+
ICal.FloatingDateTime (LocalTime d t) -> (d, Just $ projHM t)
131+
ICal.UTCDateTime (UTCTime d t) -> (d, Just $ difftime2HM t)
132+
ICal.ZonedDateTime (LocalTime d t) zone -> (d, Just $ projHM t)
133+
(day, time) = case start of
134+
ICal.DTStartDateTime d _ -> datetime2tuple d
135+
ICal.DTStartDate d _ -> (ICal.dateValue d, Nothing)
136+
duration = case ICal.veDTEndDuration event of
137+
Nothing -> Nothing
138+
Just (Left (ICal.DTEndDate _ _)) -> Nothing
139+
Just (Left (ICal.DTEndDateTime endDateTime _)) ->
140+
do -- in Maybe
141+
has_start_time <- time
142+
has_end_time <- snd $ datetime2tuple endDateTime
143+
return $ has_end_time `CU.diffTime` has_start_time
144+
145+
Just (Right ical_duration) ->
146+
case (ICal.durationValue ical_duration) of
147+
ICal.DurationDate sign d h m sec -> Just (h, m)
148+
ICal.DurationTime sign h m sec -> Just (h, m)
149+
ICal.DurationWeek _ _ -> Nothing
150+
summary = fromMaybe "" (unpack <$> ICal.summaryValue <$> ICal.veSummary event)
151+
152+
114153
cachedIncarnations :: St -> (Day,Day) -> CB.Incarnations'
115154
cachedIncarnations st (from,to) =
116155
A.accumArray (flip (:)) [] (from,to)
@@ -123,21 +162,14 @@ cachedIncarnations st (from,to) =
123162
Just x -> (x:l)
124163
Nothing -> l
125164

126-
extractDayFromDateTime :: ICal.DateTime -> Day
127-
extractDayFromDateTime (ICal.FloatingDateTime (LocalTime d t)) = d
128-
extractDayFromDateTime (ICal.UTCDateTime (UTCTime d _)) = d
129-
extractDayFromDateTime (ICal.ZonedDateTime (LocalTime d t) zone) = d
130-
131165
extractEvent :: VEventKey -> ICal.VEvent -> Maybe (Day,CB.Incarnation')
132166
extractEvent key event = do
133167
start <- ICal.veDTStart event
134-
let day = case start of
135-
ICal.DTStartDateTime d _ -> extractDayFromDateTime d
136-
ICal.DTStartDate d _ -> ICal.dateValue d
137-
summary <- unpack <$> ICal.summaryValue <$> ICal.veSummary event
138168
let itemId = PS.lookupUnsafe (_idStore st) key
169+
let incarn = vevent2incarnation event start itemId
170+
let day = CB.day incarn
139171
if from <= day && day <= to
140-
then return (day, CB.Incarnation day Nothing Nothing summary itemId)
172+
then return (day, incarn)
141173
else Nothing
142174

143175
backend :: CB.Backend St Event

0 commit comments

Comments
 (0)