packages feed

timezone-detect 0.2.1.0 → 0.2.2.0

raw patch · 5 files changed

+55/−12 lines, 5 files

Files

ChangeLog.md view
@@ -1,8 +1,16 @@ # Changelog for timezone-detect -## v0.2.0.0 (2020-08-30)+## v0.2.2.0 (2020-08-30) -* Depend on `base >= 4.9` to ensure `MonadFail` and `lifIO` are included.+* Explicitly import `MonadFail` and `fail`; hide the `fail` from `Prelude`.+* Introduces `timeInTimeZoneToUTC`, for when the timezone name is already available.+* Minor updates to help build with older Haskell versions.+* Update github actions to build on said older haskells!+* `TimeZoneName` is now an alias for `FilePath`.++## v0.2.1.0 (2020-08-30)++* Depend on `base >= 4.9` to ensure `MonadFail` and `liftIO` are included.  ## v0.2.0.0 (2020-08-30) 
README.md view
@@ -29,5 +29,14 @@  >>> localSummer <- parseTimeM True defaultTimeLocale "%Y-%-m-%-d %T" "2019-07-25 00:30:00" >>> utcTime <- timeAtPointToUTC "./test/tz_db/timezone21.bin" 40.7831 (-73.9712) localWinter-2019-12-25 04:30:00 UTC+2019-07-25 04:30:00 UTC+```++You can also opt to obtain the timezone name separately (if you wanted to isolate that as a failure scenario,)+and, once in possession of it, use `timeInTimeZoneToUTC`:++```haskell+>>> localSummer <- parseTimeM True defaultTimeLocale "%Y-%-m-%-d %T" "2019-07-25 00:30:00"+>>> utcTime <- timeInTimeZoneToUTC "America/New_York" localSummer+2019-07-25 04:30:00 UTC ```
src/Data/Time/LocalTime/TimeZone/Detect.hs view
@@ -17,6 +17,7 @@ module Data.Time.LocalTime.TimeZone.Detect      ( lookupTimeZoneName     , timeAtPointToUTC+    , timeInTimeZoneToUTC     , getTimeZoneSeriesFromOlsonFileUNIX     , TimeZoneName ) where@@ -29,12 +30,15 @@ import Data.Time.LocalTime.TimeZone.Series import System.IO.Unsafe (unsafePerformIO) import Control.Monad.IO.Class (MonadIO(liftIO))-+import Control.Monad.Fail (MonadFail, fail)+import Prelude hiding (fail) --- | Alias for clarity, timezones are short strings that follow the IANA conventions+-- | Alias for clarity, timezones are path-like strings that follow the IANA conventions -- documented here: -- <https://data.iana.org/time-zones/tz-link.html>-type TimeZoneName = String+-- and here:+-- <https://en.wikipedia.org/wiki/Tz_database#Names_of_time_zones>+type TimeZoneName = FilePath  -- | Gets timezone info from the standard location in UNIX systems. -- The name should be one of the standard tz database names, as returned@@ -42,7 +46,7 @@ -- See: <https://en.wikipedia.org/wiki/List_of_tz_database_time_zones> getTimeZoneSeriesFromOlsonFileUNIX :: TimeZoneName -> IO TimeZoneSeries getTimeZoneSeriesFromOlsonFileUNIX tzName =-    getTimeZoneSeriesFromOlsonFile $ "/usr/share/zoneinfo/" <> tzName+    getTimeZoneSeriesFromOlsonFile $ "/usr/share/zoneinfo/" ++ tzName  -- | Given a timezone database, latitude and longitude, try to determine the -- timezone name. Follow the instructions in the C library's repository@@ -58,7 +62,7 @@     unsafePerformIO $ do         zdPtr <- withCAString databaseLocation $ \dbl -> c_ZDOpenDatabase dbl         if zdPtr == nullPtr then-            pure $ fail (databaseLocation <> " is not a valid timezone database.")+            pure $ fail (databaseLocation ++ " is not a valid timezone database.")         else do             tzName <- c_ZDHelperSimpleLookupString zdPtr                                                    (realToFrac lat)@@ -69,6 +73,13 @@                 peekCAString tzName >>= (pure . return)  +-- | Given a timezone name (presumably obtained via `lookupTimeZoneName`,)+-- and a reference time in `LocalTime`, find the UTC equivalent.+timeInTimeZoneToUTC :: TimeZoneName -> LocalTime -> IO UTCTime+timeInTimeZoneToUTC tzName referenceTime = do+    tzSeries <- getTimeZoneSeriesFromOlsonFileUNIX tzName+    return $ localTimeToUTC' tzSeries referenceTime+ -- | Given a timezone database, latitude, longitude and a local reference time, find the UTC Time -- equivalent of that reference time in the given timezone. The reference time helps determine -- which offset was in effect, since daylight savings, historical circumstances, political revisions@@ -77,5 +88,4 @@ timeAtPointToUTC :: FilePath -> Double -> Double -> LocalTime -> IO UTCTime timeAtPointToUTC databaseLocation lat lng referenceTime = do     tzName <- lookupTimeZoneName databaseLocation lat lng-    tzSeries <- liftIO $ getTimeZoneSeriesFromOlsonFileUNIX tzName-    return $ localTimeToUTC' tzSeries referenceTime+    timeInTimeZoneToUTC tzName referenceTime
test/TimezoneDetectSpec.hs view
@@ -62,3 +62,19 @@              atPointWinter `shouldBe` utcWinter             atPointSummer `shouldBe` utcSummer++    describe "timeInTimeZoneToUTC" $ do+        it "calculates a UTC instant given a timezone name" $ do+            localWinter <- localTimeFromString "2019-12-25 00:30:00"+            localSummer <- localTimeFromString "2019-08-25 00:30:00"+            utcWinter <- utcFromString "2019-12-25 05:30:00"+            utcSummer <- utcFromString "2019-08-25 04:30:00"+            alwaysSummer  <- utcFromString "2019-08-25 06:30:00"++            atTZWinter <- timeInTimeZoneToUTC "America/New_York" localWinter+            atTZSummer <- timeInTimeZoneToUTC "America/New_York" localSummer+            atTZNoDST  <- timeInTimeZoneToUTC "America/Tegucigalpa" localSummer++            atTZWinter `shouldBe` utcWinter+            atTZSummer `shouldBe` utcSummer+            atTZNoDST  `shouldBe` alwaysSummer
timezone-detect.cabal view
@@ -4,10 +4,10 @@ -- -- see: https://github.com/sol/hpack ----- hash: ecaa29fb79f73d37d1fd1e23cbb86d67f3f2f8eb18d3f054334aed03d230c054+-- hash: 03ea7e2e9760969e8b6c4e2a5f8cb5a17b448db1b6b3d5749e18cf3f7336553a  name:           timezone-detect-version:        0.2.1.0+version:        0.2.2.0 synopsis:       Haskell bindings for the zone-detect C library; plus tz-aware utils. description:    Please see the README on GitHub at <https://github.com/lfborjas/timezone-detect#readme> category:       Data, Foreign, Time