summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorGregor Kleen <aethoago@141.li>2017-02-16 12:55:00 +0100
committerGregor Kleen <aethoago@141.li>2017-02-16 12:55:00 +0100
commit333ea946916b005134e7ba249178acad4858a67d (patch)
treeab27a9dbdd45dbe542dd4a42e083864f7adee9f9 /lib
parent6cab5d804dd2f5a5bcaa74ebad2347c9581a06b8 (diff)
downloadpostdelay-333ea946916b005134e7ba249178acad4858a67d.tar
postdelay-333ea946916b005134e7ba249178acad4858a67d.tar.gz
postdelay-333ea946916b005134e7ba249178acad4858a67d.tar.bz2
postdelay-333ea946916b005134e7ba249178acad4858a67d.tar.xz
postdelay-333ea946916b005134e7ba249178acad4858a67d.zip
shiftBack
Diffstat (limited to 'lib')
-rw-r--r--lib/Postdelay/TimeSpec.hs29
1 files changed, 18 insertions, 11 deletions
diff --git a/lib/Postdelay/TimeSpec.hs b/lib/Postdelay/TimeSpec.hs
index 8244885..384de4b 100644
--- a/lib/Postdelay/TimeSpec.hs
+++ b/lib/Postdelay/TimeSpec.hs
@@ -36,6 +36,13 @@ seconds' = seconds . iso (% Second) (# Second)
36utcOffset :: Iso' TimeZone Time 36utcOffset :: Iso' TimeZone Time
37utcOffset = iso ((% Minute) . fromIntegral . timeZoneMinutes) (minutesToTimeZone . round . (# Minute)) 37utcOffset = iso ((% Minute) . fromIntegral . timeZoneMinutes) (minutesToTimeZone . round . (# Minute))
38 38
39shiftBack :: MonadState LocalTime m => Time -> m a -> m a
40shiftBack by mod = join $ shiftBack' <$> get <*> mod <*> get
41 where
42 shiftBack' prev ret new
43 | new < prev = ret <$ (flexDT.seconds' %= (^+^) by)
44 | otherwise = pure ret
45
39 46
40timeSpec :: forall s m. StringParser s m => m (Endo LocalTime) 47timeSpec :: forall s m. StringParser s m => m (Endo LocalTime)
41timeSpec = label "Relative time specification" $ 48timeSpec = label "Relative time specification" $
@@ -44,23 +51,23 @@ timeSpec = label "Relative time specification" $
44 , offsets False 51 , offsets False
45 ] 52 ]
46 where 53 where
47 specBase = foldMap toEndo <$> mkGramSepBy spaces [ absDate 54 specBase = toEndo <$> mkGramSepBy spaces [ timeOfDay
48 , timeOfDay 55 , dateSpec
49 ] 56 ]
50 57
51 toEndo :: RWS LocalTime () LocalTime a -> Endo LocalTime 58 toEndo :: [RWS LocalTime () LocalTime a] -> Endo LocalTime
52 toEndo act = Endo $ \t -> fst $ execRWS act t t 59 toEndo acts = Endo $ \t -> fst $ execRWS (sequence acts) t t
53 60
54timeOfDay, absDate :: StringParser s m => m (RWS LocalTime () LocalTime ()) 61timeOfDay, dateSpec :: StringParser s m => m (RWS LocalTime () LocalTime ())
55timeOfDay = label "Time of day" $ assign time <$> choice 62timeOfDay = label "Time of day" $ shiftBack (1 % Day) . assign time <$> choice
56 [ TimeOfDay 0 0 0 <$ string' "midnight" 63 [ TimeOfDay 0 0 0 <$ string' "midnight"
57 , TimeOfDay 12 0 0 <$ string' "noon" 64 , TimeOfDay 12 0 0 <$ string' "noon"
58 , TimeOfDay 16 0 0 <$ string' "teatime" 65 , TimeOfDay 16 0 0 <$ string' "teatime"
59 ] 66 ]
60absDate = label "Date" . (fmap . (=<<) . assign $ date) $ choice 67dateSpec = label "Date" $ (date <~) <$> choice
61 [ view date <$ string' "today" 68 [ view date <$ string' "today"
62 , views date succ <$ string' "tomorrow" 69 , views date succ <$ string' "tomorrow"
63 ] 70 ]
64 71
65offsets :: forall s m. StringParser s m 72offsets :: forall s m. StringParser s m
66 => Bool -- ^ Require sign on first offset? 73 => Bool -- ^ Require sign on first offset?