on 00a36f5 help sign in
rel8/src/Rel8/Expr/Time.hs
155 lines 3.5 kB history raw
1module Rel8.Expr.Time
2 ( -- * Working with @Day@
3 today
4 , toDay
5 , fromDay
6 , addDays
7 , diffDays
8 , subtractDays
10 -- * Working with @UTCTime@
11 , now
12 , addTime
13 , diffTime
14 , subtractTime
16 -- Working with @DiffTime@
17 , scaleInterval
18 , second, seconds
19 , minute, minutes
20 , hour, hours
21 , day, days
22 , month, months
23 , year, years
24 ) where
26-- base
27import Data.Int ( Int32 )
28import Prelude
30-- rel8
31import Rel8.Expr ( Expr )
32import Rel8.Expr.Function ( binaryOperator, nullaryFunction )
33import Rel8.Expr.Opaleye ( unsafeCastExpr, unsafeLiteral )
35-- time
36import Data.Time.Calendar ( Day )
37import Data.Time.Clock ( DiffTime, UTCTime )
40-- | Corresponds to @date(now())@.
41today :: Expr Day
42today = toDay now
45-- | Corresponds to calling the @date@ function with a given time.
46toDay :: Expr UTCTime -> Expr Day
47toDay = unsafeCastExpr
50-- | Corresponds to @x::timestamptz@.
51fromDay :: Expr Day -> Expr UTCTime
52fromDay = unsafeCastExpr
55-- | Move forward a given number of days from a particular day.
56addDays :: Expr Int32 -> Expr Day -> Expr Day
57addDays = flip (binaryOperator "+")
60-- | Find the number of days between two days. Corresponds to the @-@ operator.
61diffDays :: Expr Day -> Expr Day -> Expr Int32
62diffDays = binaryOperator "-"
65-- | Subtract a given number of days from a particular 'Day'.
66subtractDays :: Expr Int32 -> Expr Day -> Expr Day
67subtractDays = flip (binaryOperator "-")
70-- | Corresponds to @now()@.
71now :: Expr UTCTime
72now = nullaryFunction "now"
75-- | Add a time interval to a point in time, yielding a new point in time.
76addTime :: Expr DiffTime -> Expr UTCTime -> Expr UTCTime
77addTime = flip (binaryOperator "+")
80-- | Find the duration between two times.
81diffTime :: Expr UTCTime -> Expr UTCTime -> Expr DiffTime
82diffTime = binaryOperator "-"
85-- | Subtract a time interval from a point in time, yielding a new point in time.
86subtractTime :: Expr DiffTime -> Expr UTCTime -> Expr UTCTime
87subtractTime = flip (binaryOperator "-")
90scaleInterval :: Expr Double -> Expr DiffTime -> Expr DiffTime
91scaleInterval = binaryOperator "*"
94-- | An interval of one second.
95second :: Expr DiffTime
96second = singleton "second"
99-- | Create a literal interval from an integral number of seconds.
100seconds :: Expr Double -> Expr DiffTime
101seconds = (`scaleInterval` second)
104-- | An interval of one minute.
105minute :: Expr DiffTime
106minute = singleton "minute"
109-- | Create a literal interval from an integral number of minutes.
110minutes :: Expr Double -> Expr DiffTime
111minutes = (`scaleInterval` minute)
114-- | An interval of one hour.
115hour :: Expr DiffTime
116hour = singleton "hour"
119-- | Create a literal interval from an integral number of hours.
120hours :: Expr Double -> Expr DiffTime
121hours = (`scaleInterval` hour)
124-- | An interval of one day.
125day :: Expr DiffTime
126day = singleton "day"
129-- | Create a literal interval from a number of days.
130days :: Expr Double -> Expr DiffTime
131days = (`scaleInterval` day)
134-- | An interval of one month.
135month :: Expr DiffTime
136month = singleton "month"
139-- | Create a literal interval from a number of months.
140months :: Expr Double -> Expr DiffTime
141months = (`scaleInterval` month)
144-- | An interval of one year.
145year :: Expr DiffTime
146year = singleton "year"
149-- | Create a literal interval from a number of years.
150years :: Expr Double -> Expr DiffTime
151years = (`scaleInterval` year)
154singleton :: String -> Expr DiffTime
155singleton unit = unsafeLiteral $ "'1 " ++ unit ++ "'"