module Data.Thyme.Time.Core
( module Data.Thyme
, module Data.Thyme.Time.Core
) where
import Prelude
import Control.Lens
import Data.AffineSpace
import Data.Int
import Data.Thyme.Internal.Micro
import Data.Ratio
import Data.Thyme
import Data.Thyme.Calendar.OrdinalDate
import Data.Thyme.Calendar.MonthDay
import Data.Thyme.Calendar.WeekDate
import Data.Thyme.Clock.Internal
import Data.Thyme.Clock.POSIX
import Data.Thyme.Clock.TAI
import qualified Data.Time.Calendar as T
import qualified Data.Time.Clock as T
import qualified Data.Time.Clock.TAI as T
import qualified Data.Time.LocalTime as T
import Unsafe.Coerce
class Thyme a b | b -> a where
thyme :: Iso' a b
instance Thyme T.Day Day where
thyme = iso
(ModifiedJulianDay . fromInteger . T.toModifiedJulianDay)
(T.ModifiedJulianDay . toInteger . toModifiedJulianDay)
instance Thyme T.UniversalTime UniversalTime where
thyme = iso T.getModJulianDate T.ModJulianDate . from modJulianDate
instance Thyme T.DiffTime DiffTime where
thyme = iso unsafeCoerce unsafeCoerce . from picoseconds
instance Thyme T.NominalDiffTime NominalDiffTime where
thyme = iso unsafeCoerce unsafeCoerce . from picoseconds
instance Thyme T.UTCTime UTCView where
thyme = iso
(\ (T.UTCTime d t) -> UTCTime (d ^. thyme) (t ^. thyme))
(\ (UTCTime d t) -> T.UTCTime (thyme # d) (thyme # t))
instance Thyme T.UTCTime UTCTime where
thyme = thyme . from utcTime
instance Thyme T.AbsoluteTime AbsoluteTime where
thyme = iso (`T.diffAbsoluteTime` T.taiEpoch)
(`T.addAbsoluteTime` T.taiEpoch)
. thyme . iso (taiEpoch .+^) (.-. taiEpoch)
instance Thyme T.TimeZone TimeZone where
thyme = iso (\ T.TimeZone {..} -> TimeZone {..})
(\ TimeZone {..} -> T.TimeZone {..})
instance Thyme T.TimeOfDay TimeOfDay where
thyme = iso ( \ (T.TimeOfDay h m s) -> TimeOfDay h m $
microseconds # round (s * 1000000) )
( \ (TimeOfDay h m s) -> T.TimeOfDay h m . fromRational $
toInteger (s ^. microseconds) % 1000000 )
instance Thyme T.LocalTime LocalTime where
thyme = iso
(\ (T.LocalTime d t) -> LocalTime (d ^. thyme) (t ^. thyme))
(\ (LocalTime d t) -> T.LocalTime (thyme # d) (thyme # t))
instance Thyme T.ZonedTime ZonedTime where
thyme = iso
(\ (T.ZonedTime t z) -> ZonedTime (t ^. thyme) (z ^. thyme))
(\ (ZonedTime t z) -> T.ZonedTime (thyme # t) (thyme # z))
toThyme :: (Thyme a b) => a -> b
toThyme = view thyme
fromThyme :: (Thyme a b) => b -> a
fromThyme = review thyme
addDays :: Days -> Day -> Day
addDays = flip (.+^)
diffDays :: Day -> Day -> Days
diffDays = (.-.)
toGregorian :: Day -> (Year, Month, DayOfMonth)
toGregorian (view gregorian -> YearMonthDay y m d) = (y, m, d)
fromGregorian :: Year -> Month -> DayOfMonth -> Day
fromGregorian y m d = gregorian # YearMonthDay y m d
fromGregorianValid :: Year -> Month -> DayOfMonth -> Maybe Day
fromGregorianValid y m d = gregorianValid (YearMonthDay y m d)
addGregorianMonthsClip :: Months -> Day -> Day
addGregorianMonthsClip n = review gregorian
. gregorianMonthsClip n . view gregorian
addGregorianMonthsRollover :: Months -> Day -> Day
addGregorianMonthsRollover n = review gregorian
. gregorianMonthsRollover n . view gregorian
addGregorianYearsClip :: Years -> Day -> Day
addGregorianYearsClip n = review gregorian
. gregorianYearsClip n . view gregorian
addGregorianYearsRollover :: Years -> Day -> Day
addGregorianYearsRollover n = review gregorian
. gregorianYearsRollover n . view gregorian
dayOfYearToMonthAndDay :: Bool -> DayOfYear -> (Month, DayOfMonth)
dayOfYearToMonthAndDay leap (view (monthDay leap) -> MonthDay m d) = (m, d)
monthAndDayToDayOfYear :: Bool -> Month -> DayOfMonth -> DayOfYear
monthAndDayToDayOfYear leap m d = monthDay leap # MonthDay m d
monthAndDayToDayOfYearValid :: Bool -> Month -> DayOfMonth -> Maybe DayOfYear
monthAndDayToDayOfYearValid leap m d = monthDayValid leap (MonthDay m d)
toOrdinalDate :: Day -> (Year, DayOfYear)
toOrdinalDate (view ordinalDate -> OrdinalDate y d) = (y, d)
fromOrdinalDate :: Year -> DayOfYear -> Day
fromOrdinalDate y d = ordinalDate # OrdinalDate y d
fromOrdinalDateValid :: Year -> DayOfYear -> Maybe Day
fromOrdinalDateValid y d = ordinalDateValid (OrdinalDate y d)
sundayStartWeek :: Day -> (Year, WeekOfYear, DayOfWeek)
sundayStartWeek (view sundayWeek -> SundayWeek y w d) = (y, w, d)
fromSundayStartWeek :: Year -> WeekOfYear -> DayOfWeek -> Day
fromSundayStartWeek y w d = sundayWeek # SundayWeek y w d
fromSundayStartWeekValid :: Year -> WeekOfYear -> DayOfWeek -> Maybe Day
fromSundayStartWeekValid y w d = sundayWeekValid (SundayWeek y w d)
mondayStartWeek :: Day -> (Year, WeekOfYear, DayOfWeek)
mondayStartWeek (view mondayWeek -> MondayWeek y w d) = (y, w, d)
fromMondayStartWeek :: Year -> WeekOfYear -> DayOfWeek -> Day
fromMondayStartWeek y w d = mondayWeek # MondayWeek y w d
fromMondayStartWeekValid :: Year -> WeekOfYear -> DayOfWeek -> Maybe Day
fromMondayStartWeekValid y w d = mondayWeekValid (MondayWeek y w d)
toWeekDate :: Day -> (Year, WeekOfYear, DayOfWeek)
toWeekDate (view weekDate -> WeekDate y w d) = (y, w, d)
fromWeekDate :: Year -> WeekOfYear -> DayOfWeek -> Day
fromWeekDate y w d = weekDate # WeekDate y w d
fromWeekDateValid :: Year -> WeekOfYear -> DayOfWeek -> Maybe Day
fromWeekDateValid y w d = weekDateValid (WeekDate y w d)
getModJulianDate :: UniversalTime -> Rational
getModJulianDate = view modJulianDate
mkModJulianDate :: Rational -> UniversalTime
mkModJulianDate = review modJulianDate
secondsToDiffTime :: Int64 -> DiffTime
secondsToDiffTime a = DiffTime (Micro $ a * 1000000)
picosecondsToDiffTime :: Int64 -> DiffTime
picosecondsToDiffTime a = DiffTime . Micro $
quot (a + signum a * 500000) 1000000
mkUTCTime :: Day -> DiffTime -> UTCTime
mkUTCTime d t = utcTime # UTCTime d t
unUTCTime :: UTCTime -> UTCView
unUTCTime = view utcTime
addUTCTime :: NominalDiffTime -> UTCTime -> UTCTime
addUTCTime = flip (.+^)
diffUTCTime :: UTCTime -> UTCTime -> NominalDiffTime
diffUTCTime = (.-.)
toMicroseconds :: (TimeDiff t) => t -> Int64
toMicroseconds = view microseconds
fromMicroseconds :: (TimeDiff t) => Int64 -> t
fromMicroseconds = review microseconds
posixSecondsToUTCTime :: POSIXTime -> UTCTime
posixSecondsToUTCTime = review posixTime
utcTimeToPOSIXSeconds :: UTCTime -> POSIXTime
utcTimeToPOSIXSeconds = view posixTime
addAbsoluteTime :: DiffTime -> AbsoluteTime -> AbsoluteTime
addAbsoluteTime = flip (.+^)
diffAbsoluteTime :: AbsoluteTime -> AbsoluteTime -> DiffTime
diffAbsoluteTime = (.-.)
utcToTAITime :: LeapSecondTable -> UTCTime -> AbsoluteTime
utcToTAITime = view . absoluteTime
taiToUTCTime :: LeapSecondTable -> AbsoluteTime -> UTCTime
taiToUTCTime = review . absoluteTime
utcToLocalTimeOfDay :: TimeZone -> TimeOfDay -> (Days, TimeOfDay)
utcToLocalTimeOfDay = addMinutes . timeZoneMinutes
localToUTCTimeOfDay :: TimeZone -> TimeOfDay -> (Days, TimeOfDay)
localToUTCTimeOfDay = addMinutes . negate . timeZoneMinutes
timeToTimeOfDay :: DiffTime -> TimeOfDay
timeToTimeOfDay = view timeOfDay
timeOfDayToTime :: TimeOfDay -> DiffTime
timeOfDayToTime = review timeOfDay
dayFractionToTimeOfDay :: Rational -> TimeOfDay
dayFractionToTimeOfDay = review dayFraction
timeOfDayToDayFraction :: TimeOfDay -> Rational
timeOfDayToDayFraction = view dayFraction
utcToLocalTime :: TimeZone -> UTCTime -> LocalTime
utcToLocalTime = view . utcLocalTime
localTimeToUTC :: TimeZone -> LocalTime -> UTCTime
localTimeToUTC = review . utcLocalTime
ut1ToLocalTime :: Rational -> UniversalTime -> LocalTime
ut1ToLocalTime = view . ut1LocalTime
localTimeToUT1 :: Rational -> LocalTime -> UniversalTime
localTimeToUT1 = review . ut1LocalTime
utcToZonedTime :: TimeZone -> UTCTime -> ZonedTime
utcToZonedTime z t = view zonedTime (z, t)
zonedTimeToUTC :: ZonedTime -> UTCTime
zonedTimeToUTC = snd . review zonedTime