format everything with hindent
[packages/time.git] / lib / Data / Time / LocalTime / Internal / LocalTime.hs
1 {-# OPTIONS -fno-warn-orphans #-}
2
3 module Data.Time.LocalTime.Internal.LocalTime
4 (
5 -- * Local Time
6 LocalTime(..)
7 , addLocalTime
8 , diffLocalTime
9 -- converting UTC and UT1 times to LocalTime
10 , utcToLocalTime
11 , localTimeToUTC
12 , ut1ToLocalTime
13 , localTimeToUT1
14 ) where
15
16 import Control.DeepSeq
17 import Data.Data
18 import Data.Time.Calendar.Days
19 import Data.Time.Calendar.Gregorian
20 import Data.Time.Clock.Internal.NominalDiffTime
21 import Data.Time.Clock.Internal.UTCDiff
22 import Data.Time.Clock.Internal.UTCTime
23 import Data.Time.Clock.Internal.UniversalTime
24 import Data.Time.LocalTime.Internal.TimeOfDay
25 import Data.Time.LocalTime.Internal.TimeZone
26
27 -- | A simple day and time aggregate, where the day is of the specified parameter,
28 -- and the time is a TimeOfDay.
29 -- Conversion of this (as local civil time) to UTC depends on the time zone.
30 -- Conversion of this (as local mean time) to UT1 depends on the longitude.
31 data LocalTime = LocalTime
32 { localDay :: Day
33 , localTimeOfDay :: TimeOfDay
34 } deriving (Eq, Ord, Data, Typeable)
35
36 instance NFData LocalTime where
37 rnf (LocalTime d t) = rnf d `seq` rnf t `seq` ()
38
39 instance Show LocalTime where
40 show (LocalTime d t) = (showGregorian d) ++ " " ++ (show t)
41
42 -- | addLocalTime a b = a + b
43 addLocalTime :: NominalDiffTime -> LocalTime -> LocalTime
44 addLocalTime x = utcToLocalTime utc . addUTCTime x . localTimeToUTC utc
45
46 -- | diffLocalTime a b = a - b
47 diffLocalTime :: LocalTime -> LocalTime -> NominalDiffTime
48 diffLocalTime a b = diffUTCTime (localTimeToUTC utc a) (localTimeToUTC utc b)
49
50 -- | Get the local time of a UTC time in a time zone.
51 utcToLocalTime :: TimeZone -> UTCTime -> LocalTime
52 utcToLocalTime tz (UTCTime day dt) = LocalTime (addDays i day) tod
53 where
54 (i, tod) = utcToLocalTimeOfDay tz (timeToTimeOfDay dt)
55
56 -- | Get the UTC time of a local time in a time zone.
57 localTimeToUTC :: TimeZone -> LocalTime -> UTCTime
58 localTimeToUTC tz (LocalTime day tod) = UTCTime (addDays i day) (timeOfDayToTime todUTC)
59 where
60 (i, todUTC) = localToUTCTimeOfDay tz tod
61
62 -- | Get the local time of a UT1 time on a particular meridian (in degrees, positive is East).
63 ut1ToLocalTime :: Rational -> UniversalTime -> LocalTime
64 ut1ToLocalTime long (ModJulianDate date) =
65 LocalTime (ModifiedJulianDay localMJD) (dayFractionToTimeOfDay localToDOffset)
66 where
67 localTime = date + long / 360 :: Rational
68 localMJD = floor localTime
69 localToDOffset = localTime - (fromIntegral localMJD)
70
71 -- | Get the UT1 time of a local time on a particular meridian (in degrees, positive is East).
72 localTimeToUT1 :: Rational -> LocalTime -> UniversalTime
73 localTimeToUT1 long (LocalTime (ModifiedJulianDay localMJD) tod) =
74 ModJulianDate ((fromIntegral localMJD) + (timeOfDayToDayFraction tod) - (long / 360))
75
76 -- orphan instance
77 instance Show UniversalTime where
78 show t = show (ut1ToLocalTime 0 t)