{-# OPTIONS -fno-warn-orphans #-}
module Data.Time.LocalTime.Internal.LocalTime
(
LocalTime(..),
addLocalTime,diffLocalTime,
utcToLocalTime,localTimeToUTC,ut1ToLocalTime,localTimeToUT1,
) where
import Control.DeepSeq
import Data.Typeable
import Data.Data
import Data.Time.Calendar.Days
import Data.Time.Calendar.Gregorian
import Data.Time.Clock.Internal.NominalDiffTime
import Data.Time.Clock.Internal.UniversalTime
import Data.Time.Clock.Internal.UTCDiff
import Data.Time.Clock.Internal.UTCTime
import Data.Time.LocalTime.Internal.TimeOfDay
import Data.Time.LocalTime.Internal.TimeZone
data LocalTime = LocalTime {
localDay :: Day,
localTimeOfDay :: TimeOfDay
} deriving (Eq,Ord,Data, Typeable)
instance NFData LocalTime where
rnf (LocalTime d t) = rnf d `seq` rnf t `seq` ()
instance Show LocalTime where
show (LocalTime d t) = (showGregorian d) ++ " " ++ (show t)
addLocalTime :: NominalDiffTime -> LocalTime -> LocalTime
addLocalTime x = utcToLocalTime utc . addUTCTime x . localTimeToUTC utc
diffLocalTime :: LocalTime -> LocalTime -> NominalDiffTime
diffLocalTime a b = diffUTCTime (localTimeToUTC utc a) (localTimeToUTC utc b)
utcToLocalTime :: TimeZone -> UTCTime -> LocalTime
utcToLocalTime tz (UTCTime day dt) = LocalTime (addDays i day) tod where
(i,tod) = utcToLocalTimeOfDay tz (timeToTimeOfDay dt)
localTimeToUTC :: TimeZone -> LocalTime -> UTCTime
localTimeToUTC tz (LocalTime day tod) = UTCTime (addDays i day) (timeOfDayToTime todUTC) where
(i,todUTC) = localToUTCTimeOfDay tz tod
ut1ToLocalTime :: Rational -> UniversalTime -> LocalTime
ut1ToLocalTime long (ModJulianDate date) = LocalTime (ModifiedJulianDay localMJD) (dayFractionToTimeOfDay localToDOffset) where
localTime = date + long / 360 :: Rational
localMJD = floor localTime
localToDOffset = localTime - (fromIntegral localMJD)
localTimeToUT1 :: Rational -> LocalTime -> UniversalTime
localTimeToUT1 long (LocalTime (ModifiedJulianDay localMJD) tod) = ModJulianDate ((fromIntegral localMJD) + (timeOfDayToDayFraction tod) - (long / 360))
instance Show UniversalTime where
show t = show (ut1ToLocalTime 0 t)