2017-03-08 21:33:55 +03:00
|
|
|
-- 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 NoRebindableSyntax #-}
|
|
|
|
|
|
|
|
-- | Everything needed to run Duckling.
|
|
|
|
|
|
|
|
module Duckling.Core
|
|
|
|
( Context(..)
|
|
|
|
, Dimension(..)
|
2017-03-15 20:15:42 +03:00
|
|
|
, fromName
|
2017-03-08 21:33:55 +03:00
|
|
|
, Entity(..)
|
|
|
|
, Lang(..)
|
|
|
|
, Some(..)
|
2017-03-22 17:59:39 +03:00
|
|
|
, toName
|
2017-03-08 21:33:55 +03:00
|
|
|
|
|
|
|
-- Duckling API
|
|
|
|
, parse
|
|
|
|
, supportedDimensions
|
|
|
|
|
|
|
|
-- Reference time builders
|
|
|
|
, currentReftime
|
2017-06-02 20:52:52 +03:00
|
|
|
, fromZonedTime
|
2017-03-08 21:33:55 +03:00
|
|
|
, makeReftime
|
|
|
|
) where
|
|
|
|
|
|
|
|
import Data.HashMap.Strict (HashMap)
|
2017-04-24 17:19:34 +03:00
|
|
|
import Data.Maybe
|
2017-03-08 21:33:55 +03:00
|
|
|
import Data.Text (Text)
|
|
|
|
import Data.Time
|
|
|
|
import Data.Time.LocalTime.TimeZone.Series
|
|
|
|
import Prelude
|
2017-06-02 20:52:52 +03:00
|
|
|
import qualified Data.HashMap.Strict as HashMap
|
2017-03-08 21:33:55 +03:00
|
|
|
|
|
|
|
import Duckling.Api
|
|
|
|
import Duckling.Dimensions.Types
|
|
|
|
import Duckling.Lang
|
|
|
|
import Duckling.Resolve
|
|
|
|
import Duckling.Types
|
|
|
|
|
|
|
|
-- | Builds a `DucklingTime` for timezone `tz` at `utcTime`.
|
|
|
|
-- If no `series` found for `tz`, uses UTC.
|
|
|
|
makeReftime :: HashMap Text TimeZoneSeries -> Text -> UTCTime -> DucklingTime
|
|
|
|
makeReftime series tz utcTime = DucklingTime $ ZoneSeriesTime ducklingTime tzs
|
|
|
|
where
|
2017-03-15 17:57:36 +03:00
|
|
|
tzs = HashMap.lookupDefault (TimeZoneSeries utc []) tz series
|
2017-03-08 21:33:55 +03:00
|
|
|
ducklingTime = toUTC $ utcToLocalTime' tzs utcTime
|
|
|
|
|
|
|
|
-- | Builds a `DucklingTime` for timezone `tz` at current time.
|
|
|
|
-- If no `series` found for `tz`, uses UTC.
|
|
|
|
currentReftime :: HashMap Text TimeZoneSeries -> Text -> IO DucklingTime
|
|
|
|
currentReftime series tz = do
|
|
|
|
utcNow <- getCurrentTime
|
|
|
|
return $ makeReftime series tz utcNow
|
2017-06-02 20:52:52 +03:00
|
|
|
|
|
|
|
-- | Builds a `DucklingTime` from a `ZonedTime`.
|
|
|
|
fromZonedTime :: ZonedTime -> DucklingTime
|
|
|
|
fromZonedTime (ZonedTime localTime timeZone) = DucklingTime $
|
|
|
|
ZoneSeriesTime (toUTC localTime) (TimeZoneSeries timeZone [])
|