on 11bcbbb help sign in
rel8/src/Rel8/Expr/Time.hs
166 lines 3.7 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 , week, weeks
23 , month, months
24 , year, years
25 ) where
27-- base
28import Data.Int ( Int32 )
29import Prelude
31-- rel8
32import Rel8.Expr ( Expr )
33import Rel8.Expr.Function ( binaryOperator, nullaryFunction )
34import Rel8.Expr.Opaleye ( unsafeCastExpr, unsafeLiteral )
36-- time
37import Data.Time.Calendar ( Day )
38import Data.Time.Clock ( DiffTime, UTCTime )
41-- | Corresponds to @date(now())@.
42today :: Expr Day
43today = toDay now
46-- | Corresponds to calling the @date@ function with a given time.
47toDay :: Expr UTCTime -> Expr Day
48toDay = unsafeCastExpr
51-- | Corresponds to @x::timestamptz@.
52fromDay :: Expr Day -> Expr UTCTime
53fromDay = unsafeCastExpr
56-- | Move forward a given number of days from a particular day.
57addDays :: Expr Int32 -> Expr Day -> Expr Day
58addDays = flip (binaryOperator "+")
61-- | Find the number of days between two days. Corresponds to the @-@ operator.
62diffDays :: Expr Day -> Expr Day -> Expr Int32
63diffDays = binaryOperator "-"
66-- | Subtract a given number of days from a particular 'Day'.
67subtractDays :: Expr Int32 -> Expr Day -> Expr Day
68subtractDays = flip (binaryOperator "-")
71-- | Corresponds to @now()@.
72now :: Expr UTCTime
73now = nullaryFunction "now"
76-- | Add a time interval to a point in time, yielding a new point in time.
77addTime :: Expr DiffTime -> Expr UTCTime -> Expr UTCTime
78addTime = flip (binaryOperator "+")
81-- | Find the duration between two times.
82diffTime :: Expr UTCTime -> Expr UTCTime -> Expr DiffTime
83diffTime = binaryOperator "-"
86-- | Subtract a time interval from a point in time, yielding a new point in time.
87subtractTime :: Expr DiffTime -> Expr UTCTime -> Expr UTCTime
88subtractTime = flip (binaryOperator "-")
91scaleInterval :: Expr Double -> Expr DiffTime -> Expr DiffTime
92scaleInterval = binaryOperator "*"
95-- | An interval of one second.
96second :: Expr DiffTime
97second = singleton "second"
100-- | Create a literal interval from a number of seconds.
101seconds :: Expr Double -> Expr DiffTime
102seconds = (`scaleInterval` second)
105-- | An interval of one minute.
106minute :: Expr DiffTime
107minute = singleton "minute"
110-- | Create a literal interval from a number of minutes.
111minutes :: Expr Double -> Expr DiffTime
112minutes = (`scaleInterval` minute)
115-- | An interval of one hour.
116hour :: Expr DiffTime
117hour = singleton "hour"
120-- | Create a literal interval from a number of hours.
121hours :: Expr Double -> Expr DiffTime
122hours = (`scaleInterval` hour)
125-- | An interval of one day.
126day :: Expr DiffTime
127day = singleton "day"
130-- | Create a literal interval from a number of days.
131days :: Expr Double -> Expr DiffTime
132days = (`scaleInterval` day)
135-- | An interval of one week.
136week :: Expr DiffTime
137week = singleton "week"
140-- | Create a literal interval from a number of weeks.
141weeks :: Expr Double -> Expr DiffTime
142weeks = (`scaleInterval` week)
145-- | An interval of one month.
146month :: Expr DiffTime
147month = singleton "month"
150-- | Create a literal interval from a number of months.
151months :: Expr Double -> Expr DiffTime
152months = (`scaleInterval` month)
155-- | An interval of one year.
156year :: Expr DiffTime
157year = singleton "year"
160-- | Create a literal interval from a number of years.
161years :: Expr Double -> Expr DiffTime
162years = (`scaleInterval` year)
165singleton :: String -> Expr DiffTime
166singleton unit = unsafeLiteral $ "'1 " ++ unit ++ "'"