duckling/Duckling/TimeGrain/Types.hs
igor-drozdov 29d776dee5 Added TimeGrain and Duration Dimensions to Russian language
Summary:
- Added Duration dimension to Russian language
- Added TimeGrain dimension to Russian language
- Refactored isNatural and isNaturalWith out of Duration helpers into Numeral helpers
- Implemented <integer> and a half rule for Russian Numeral
- Changed the type of inSeconds to polymorphic one
Closes https://github.com/facebook/duckling/pull/105

Reviewed By: blandinw

Differential Revision: D6312604

Pulled By: patapizza

fbshipit-source-id: 9ae237b4beb6915ff8da013230457937d8e56733
2017-11-15 10:45:24 -08:00

81 lines
2.5 KiB
Haskell

-- Copyright (c) 2016-present, Facebook, Inc.
-- All rights reserved.
--
-- This source code is licensed under the BSD-style license found in the
-- LICENSE file in the root directory of this source tree. An additional grant
-- of patent rights can be found in the PATENTS file in the same directory.
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE NoRebindableSyntax #-}
{-# LANGUAGE TypeFamilies #-}
module Duckling.TimeGrain.Types
( Grain(..)
, add
, inSeconds
, lower
)
where
import Control.DeepSeq
import Data.Aeson
import Data.Hashable
import Data.Text.Lazy.Builder (fromText)
import GHC.Generics
import Prelude
import TextShow
import qualified Data.Text as Text
import qualified Data.Time as Time
import Duckling.Resolve (Resolve(..))
data Grain
-- NoGrain is helpful to define "now"
= NoGrain | Second | Minute | Hour | Day | Week | Month | Quarter | Year
deriving (Eq, Generic, Hashable, Ord, Bounded, Enum, Show, NFData)
instance Resolve Grain where
type ResolvedValue Grain = Grain
resolve _ _ = Nothing
instance TextShow Grain where
showb = fromText . Text.toLower . Text.pack . show
instance ToJSON Grain where
toJSON = String . showt
updateUTCDay :: Time.UTCTime -> (Time.Day -> Time.Day) -> Time.UTCTime
updateUTCDay (Time.UTCTime day diffTime) f = Time.UTCTime (f day) diffTime
add :: Time.UTCTime -> Grain -> Integer -> Time.UTCTime
add utcTime NoGrain n = Time.addUTCTime (realToFrac n) utcTime
add utcTime Second n = Time.addUTCTime (realToFrac n) utcTime
add utcTime Minute n = Time.addUTCTime (realToFrac $ 60 * n) utcTime
add utcTime Hour n = Time.addUTCTime (realToFrac $ 3600 * n) utcTime
add utcTime Day n = updateUTCDay utcTime $ Time.addDays n
add utcTime Week n = updateUTCDay utcTime . Time.addDays $ 7 * n
add utcTime Month n = updateUTCDay utcTime $ Time.addGregorianMonthsClip n
add utcTime Quarter n =
updateUTCDay utcTime . Time.addGregorianMonthsClip $ 3 * n
add utcTime Year n = updateUTCDay utcTime $ Time.addGregorianYearsClip n
inSeconds :: Num a => Grain -> a -> a
inSeconds NoGrain n = n
inSeconds Second n = n
inSeconds Minute n = n * 60
inSeconds Hour n = n * inSeconds Minute 60
inSeconds Day n = n * inSeconds Hour 24
inSeconds Week n = n * inSeconds Day 7
inSeconds Month n = n * inSeconds Day 30
inSeconds Quarter n = n * inSeconds Month 3
inSeconds Year n = n * inSeconds Day 365
lower :: Grain -> Grain
lower NoGrain = Second
lower Second = Second
lower Year = Month
lower Month = Day
lower x = pred x