duckling/Duckling/Duration/Helpers.hs
Julien Odent bef7a44fa8 Remove redundant brackets and language pragmas
Summary: .

Reviewed By: JonCoens

Differential Revision: D6838082

fbshipit-source-id: 94757bdb80c6d3c29a7a6554429940a1b7403108
2018-01-29 16:45:28 -08:00

53 lines
1.7 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 GADTs #-}
module Duckling.Duration.Helpers
( duration
, isGrain
, isNatural
, minutesFromHourMixedFraction
, timesOneAndAHalf
) where
import Prelude
import Duckling.Dimensions.Types
import Duckling.Duration.Types (DurationData (DurationData))
import Duckling.Numeral.Helpers (isNatural)
import Duckling.Types
import qualified Duckling.Duration.Types as TDuration
import qualified Duckling.TimeGrain.Types as TG
-- -----------------------------------------------------------------
-- Patterns
isGrain :: TG.Grain -> Predicate
isGrain value (Token TimeGrain grain) = grain == value
isGrain _ _ = False
-- -----------------------------------------------------------------
-- Production
duration :: TG.Grain -> Int -> DurationData
duration grain n = DurationData {TDuration.grain = grain, TDuration.value = n}
minutesFromHourMixedFraction :: Integer -> Integer -> Integer -> DurationData
minutesFromHourMixedFraction h n d =
duration TG.Minute $ fromIntegral $ 60 * h + quot (n * 60) d
timesOneAndAHalf :: TG.Grain -> Int -> Maybe DurationData
timesOneAndAHalf grain = case grain of
TG.Minute -> Just . duration TG.Second . (30+) . (60*)
TG.Hour -> Just . duration TG.Minute . (30+) . (60*)
TG.Day -> Just . duration TG.Hour . (12+) . (24*)
TG.Month -> Just . duration TG.Day . (15+) . (30*)
TG.Year -> Just . duration TG.Month . (6+) . (12*)
_ -> const Nothing