Skip to content

Instantly share code, notes, and snippets.

@rcook
Last active March 30, 2020 05:47
Show Gist options
  • Save rcook/1ed485ee5530af7ac2f89c1e4ac559f1 to your computer and use it in GitHub Desktop.
Save rcook/1ed485ee5530af7ac2f89c1e4ac559f1 to your computer and use it in GitHub Desktop.
What was the time difference between Bothell, Washington and Haworth, West Yorkshire over time?
The MIT License (MIT)
Copyright (c) 2018 Richard Cook
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
{-# LANGUAGE TemplateHaskell #-}
module Main (main) where
import Data.Foldable (for_)
import Data.Time (TimeZone, UTCTime(..), fromGregorian)
import Data.Time.Zones (diffForPOSIX, timeZoneForPOSIX)
import Data.Time.Zones.Internal (utcTimeToInt64)
import Data.Time.Zones.TH (includeTZFromDB)
import Data.Time.Zones.Types (TZ)
import Text.Printf (printf)
newtype Minutes = Minutes Int
instance Show Minutes where
show (Minutes n) = printf "%d:%02d" (n `div` 60) (n `mod` 60)
tzHaworth :: TZ
tzHaworth = $(includeTZFromDB "Europe/London")
tzBothell :: TZ
tzBothell = $(includeTZFromDB "America/Los_Angeles")
tzOffsetInfo :: TZ -> UTCTime -> (Minutes, TimeZone)
tzOffsetInfo tz utcTime =
let posixTime = utcTimeToInt64 utcTime
tzInEffect = timeZoneForPOSIX tz posixTime
offset = Minutes $ (diffForPOSIX tz posixTime) `div` 60
in (offset, tzInEffect)
minutesDiff :: Minutes -> Minutes -> Minutes
minutesDiff (Minutes a) (Minutes b) = Minutes (a - b)
main :: IO ()
main = do
let startDay = fromGregorian 2018 1 1
for_ (take 365 [startDay..]) $ \day -> do
let time = UTCTime day 0
(offsetBothell, tzInEffectBothell) = tzOffsetInfo tzBothell time
(offsetHaworth, tzInEffectHaworth) = tzOffsetInfo tzHaworth time
diff = offsetBothell `minutesDiff` offsetHaworth
putStrLn $
printf
"%s: %s vs %s %s"
(show day)
(show tzInEffectBothell)
(show tzInEffectHaworth)
(show diff)
let startDay = fromGregorian 2018 1 1
map (flip addDays startDay) [0..]
map (\i -> addDays i startDay) [0..]
[startDay..]
tzOffsetInfo :: TZ -> UTCTime -> (Minutes, TimeZone)
tzOffsetInfo tz utcTime =
let posixTime = utcTimeToInt64 utcTime
tzInEffect = timeZoneForPOSIX tz posixTime
offset = Minutes $ (diffForPOSIX tz posixTime) `div` 60
in (offset, tzInEffect)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment