aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Types/Reminder.hs
blob: 3c8892c69f536bf353a76a6236b231dea99ac392 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
{-

    srem --- Timed reminders as notifications

    Copyright (C) 2015  Sean Whitton

    This file is part of srem.

    srem is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    srem is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with srem.  If not, see <http://www.gnu.org/licenses/>.

-}

module Types.Reminder ( Reminder
                      , makeReminder
                      , makeReminder'
                      , getReminderHour
                      , getReminderMinute
                      , getReminderText
                      ) where

type Hour   = Int
type Minute = Int

data Reminder = Reminder { getReminderHour :: Hour
                         , getReminderMinute :: Minute
                         , getReminderText :: String}
              deriving (Eq, Ord)

instance Show Reminder where
    show (Reminder h m s) =
        "reminder at " ++ niceTime h m ++ ": " ++ s

-- | Maybe makes a reminder for an event, prepending a human-readable
--   time to the event description.
makeReminder       :: Hour           -- ^ Clock hour-hand time of event
                   -> Minute         -- ^ Clock minute-hand time of the event
                   -> String         -- ^ Event description
                   -> Maybe Reminder -- ^ Maybe a reminder
makeReminder h m s = makeReminder' h m (niceTime h m ++ " " ++ s)

-- | Maybe makes a reminder for an event.  Unlike 'makeReminder',
--   doesn't prepend anything to the event description.
makeReminder'       :: Hour           -- ^ Clock hour-hand time of event
                    -> Minute         -- ^ Clock minute-hand time of the event
                    -> String         -- ^ Event description
                    -> Maybe Reminder -- ^ Maybe a reminder
makeReminder' h m s = if   h `elem` [0..23] && m `elem` [0..59]
                      then return $ Reminder h m s
                      else fail "invalid reminder"

niceTime :: Hour -> Minute -> String
niceTime h m = (show . to12Hour) h
               ++ ":" ++ (zeroPadTime . show) m
               ++ amPm h

zeroPadTime   :: String -> String
zeroPadTime t = if   length t < 2
                then '0' : t
                else t

to12Hour :: Int -> Int
to12Hour h = if   h > 12
             then h - 12
             else h

amPm :: Int -> String
amPm h = if   h > 12
         then "pm"
         else "am"