numhask-space-0.11.1.0: Numerical spaces.
Safe HaskellSafe-Inferred
LanguageGHC2021

NumHask.Space.Time

Description

data algorithms related to time (as a Space)

Synopsis

Documentation

data TimeGrain Source #

a step in time

Instances

Instances details
Generic TimeGrain Source # 
Instance details

Defined in NumHask.Space.Time

Associated Types

type Rep TimeGrain :: Type -> Type #

Show TimeGrain Source # 
Instance details

Defined in NumHask.Space.Time

Eq TimeGrain Source # 
Instance details

Defined in NumHask.Space.Time

type Rep TimeGrain Source # 
Instance details

Defined in NumHask.Space.Time

floorGrain :: TimeGrain -> UTCTime -> UTCTime Source #

compute the floor UTCTime based on the timegrain

>>> floorGrain (Years 5) (UTCTime (fromGregorian 1999 1 1) (toDiffTime 0))
1995-12-31 00:00:00 UTC
>>> floorGrain (Months 3) (UTCTime (fromGregorian 2016 12 30) (toDiffTime 0))
2016-09-30 00:00:00 UTC
>>> floorGrain (Days 5) (UTCTime (fromGregorian 2016 12 30) (toDiffTime 1))
2016-12-30 00:00:00 UTC
>>> floorGrain (Minutes 15) (UTCTime (fromGregorian 2016 12 30) (toDiffTime $ 15*60+1))
2016-12-30 00:15:00 UTC
>>> floorGrain (Seconds 0.1) (UTCTime (fromGregorian 2016 12 30) ((toDiffTime 0.12)))
2016-12-30 00:00:00.1 UTC

ceilingGrain :: TimeGrain -> UTCTime -> UTCTime Source #

compute the ceiling UTCTime based on the timegrain

>>> ceilingGrain (Years 5) (UTCTime (fromGregorian 1999 1 1) (toDiffTime 0))
2000-12-31 00:00:00 UTC
>>> ceilingGrain (Months 3) (UTCTime (fromGregorian 2016 12 30) (toDiffTime 0))
2016-12-31 00:00:00 UTC
>>> ceilingGrain (Days 5) (UTCTime (fromGregorian 2016 12 30) (toDiffTime 1))
2016-12-31 00:00:00 UTC
>>> ceilingGrain (Minutes 15) (UTCTime (fromGregorian 2016 12 30) (toDiffTime $ 15*60+1))
2016-12-30 00:30:00 UTC
>>> ceilingGrain (Seconds 0.1) (UTCTime (fromGregorian 2016 12 30) (toDiffTime 0.12))
2016-12-30 00:00:00.2 UTC

addGrain :: TimeGrain -> Int -> UTCTime -> UTCTime Source #

add a TimeGrain to a UTCTime

>>> addGrain (Years 1) 5 (UTCTime (fromGregorian 2015 2 28) (toDiffTime 0))
2020-02-29 00:00:00 UTC
>>> addGrain (Months 1) 1 (UTCTime (fromGregorian 2015 2 28) (toDiffTime 0))
2015-03-31 00:00:00 UTC
>>> addGrain (Hours 6) 5 (UTCTime (fromGregorian 2015 2 28) (toDiffTime 0))
2015-03-01 06:00:00 UTC
>>> addGrain (Seconds 0.001) (60*1000+1) (UTCTime (fromGregorian 2015 2 28) (toDiffTime 0))
2015-02-28 00:01:00.001 UTC

sensibleTimeGrid :: Pos -> Int -> Range UTCTime -> (TimeGrain, [UTCTime]) Source #

compute a sensible TimeGrain and list of UTCTimes

>>> sensibleTimeGrid InnerPos 2 (Range (UTCTime (fromGregorian 2016 12 31) (toDiffTime 0)) (UTCTime (fromGregorian 2017 12 31) (toDiffTime 0)))
(Months 6,[2016-12-31 00:00:00 UTC,2017-06-30 00:00:00 UTC,2017-12-31 00:00:00 UTC])
>>> sensibleTimeGrid InnerPos 2 (Range (UTCTime (fromGregorian 2017 1 1) (toDiffTime 0)) (UTCTime (fromGregorian 2017 12 30) (toDiffTime 0)))
(Months 6,[2017-06-30 00:00:00 UTC])
>>> sensibleTimeGrid UpperPos 2 (Range (UTCTime (fromGregorian 2017 1 1) (toDiffTime 0)) (UTCTime (fromGregorian 2017 12 30) (toDiffTime 0)))
(Months 6,[2017-06-30 00:00:00 UTC,2017-12-31 00:00:00 UTC])
>>> sensibleTimeGrid LowerPos 2 (Range (UTCTime (fromGregorian 2017 1 1) (toDiffTime 0)) (UTCTime (fromGregorian 2017 12 30) (toDiffTime 0)))
(Months 6,[2016-12-31 00:00:00 UTC,2017-06-30 00:00:00 UTC])

data PosDiscontinuous Source #

whether to include lower and upper times

placedTimeLabelDiscontinuous :: PosDiscontinuous -> Maybe Text -> Int -> [UTCTime] -> ([(Int, Text)], [UTCTime]) Source #

Dates used for time series analysis or attached to charts are often discontinuous, but we want to smooth that reality over and show a continuous range on the axis.

The assumption with getSensibleTimeGrid is that there is a list of discountinuous UTCTimes rather than a continuous range. Output is a list of index points for the original [UTCTime] and label tuples, and a list of unused list elements.

>>> placedTimeLabelDiscontinuous PosIncludeBoundaries (Just (pack "%d %b")) 2 [UTCTime (fromGregorian 2017 12 6) (toDiffTime 0), UTCTime (fromGregorian 2017 12 29) (toDiffTime 0), UTCTime (fromGregorian 2018 1 31) (toDiffTime 0), UTCTime (fromGregorian 2018 3 3) (toDiffTime 0)]
([(0,"06 Dec"),(1,"31 Dec"),(2,"28 Feb"),(3,"03 Mar")],[])

placedTimeLabelContinuous :: PosDiscontinuous -> Maybe Text -> Int -> Range UTCTime -> [(Double, Text)] Source #

A sensible time grid between two dates, projected onto (0,1) with no attempt to get finnicky.

>>> placedTimeLabelContinuous PosIncludeBoundaries (Just (pack "%d %b")) 2 (Range (UTCTime (fromGregorian 2017 12 6) (toDiffTime 0)) (UTCTime (fromGregorian 2017 12 29) (toDiffTime 0)))
[(0.0,"06 Dec"),(0.4347826086956521,"16 Dec"),(0.8695652173913042,"26 Dec"),(0.9999999999999999,"29 Dec")]

fromNominalDiffTime :: NominalDiffTime -> Double Source #

convenience conversion to Double

toNominalDiffTime :: Double -> NominalDiffTime Source #

convenience conversion from Double

fromDiffTime :: DiffTime -> Double Source #

Convert from DiffTime to seconds (as a Double)

>>> fromDiffTime $ toDiffTime 1
1.0

toDiffTime :: Double -> DiffTime Source #

Convert from seconds (as a Double) to DiffTime >>> toDiffTime 1 1s