hodatime
Copyright(C) 2016 Jason Johnson
LicenseBSD-style (see the file LICENSE)
MaintainerJason Johnson <jason.johnson.081@gmail.com>
Stabilityexperimental
PortabilityPOSIX, Windows
Safe HaskellSafe-Inferred
LanguageHaskell2010

Data.HodaTime.Offset

Description

An Offset is a period of time offset from UTC time. This module contains constructors and functions for working with Offsets.

Clamping

An offset must be between 18 hours and -18 hours (inclusive). If you go outside this range the functions will clamp to the nearest value.

Technical discussion: why the components are read-only functions, not lenses

hours, minutes and seconds are plain functions, not lenses. In this library a lens exists to modify a value, and there is no honest way to modify a single component of an Offset in isolation, because an Offset is a single signed count of seconds: the sign belongs to the whole value, not to any one component.

The subtle part is that additive modification would actually work. Under an increment the component's current value cancels out, so over minutes (+45) reduces to adding 45 minutes to the whole offset — it turns -01:30 into -00:45, exactly as you would hope. But that also makes it identical to addClamped o (fromMinutes 45): as a lens it would buy nothing over the arithmetic that is already here.

What a lens would add over those functions is precisely the part that is not well defined for a signed value:

  • Reading a component in isolation (view minutes of -01:30): is the minutes part 30 or -30? We answer that for reads by making the accessors sign-consistent — each component carries the offset's sign, so -01:30 gives -1 hours and -30 minutes and hours*3600 + minutes*60 + seconds always reconstructs the total. That is a getter, hence a function.
  • An absolute set (set minutes 45) or a non-additive change (over minutes (*2)): here the old value does not cancel, and the minutes slot of a negative offset has no canonical meaning, so there is no honest implementation to offer.

We also considered a single lens over the whole value (its total seconds). It is coherent, but redundant: an Offset is already just a signed scalar, so fromSeconds, fromMinutes and fromHours construct one and addClamped / minusClamped do the arithmetic; modifying an Offset embedded in a larger structure is done by using those inside that structure's own modify, so a value lens would not compose any better. It would only save constructing a throwaway Offset for a bit of math — not enough to justify a second way to do the same thing.

So: read a component with the functions here; build or adjust an Offset as a whole with the constructors and addClamped / minusClamped. (Display is the job of Data.HodaTime.Pattern, not of these accessors.)

Synopsis

Types

data Offset Source #

An Offset from UTC in seconds.

Instances

Instances details
Show Offset Source # 
Instance details

Defined in Data.HodaTime.Offset.Internal

NFData Offset Source # 
Instance details

Defined in Data.HodaTime.Offset.Internal

Methods

rnf :: Offset -> () #

Eq Offset Source # 
Instance details

Defined in Data.HodaTime.Offset.Internal

Methods

(==) :: Offset -> Offset -> Bool #

(/=) :: Offset -> Offset -> Bool #

Ord Offset Source # 
Instance details

Defined in Data.HodaTime.Offset.Internal

Hashable Offset Source # 
Instance details

Defined in Data.HodaTime.Offset.Internal

Methods

hashWithSalt :: Int -> Offset -> Int #

hash :: Offset -> Int #

Constructors

empty :: Offset Source #

An Offset with an offset of 0. This is equivalent to UTC

fromSeconds :: Integral a => a -> Offset Source #

Create an Offset of (clamped) s seconds.

fromMinutes :: Integral a => a -> Offset Source #

Create an Offset of (clamped) m minutes.

fromHours :: Integral a => a -> Offset Source #

Create an Offset of (clamped) h hours.

Accessors

Read-only functions (not lenses); see the Technical discussion in the module header for why.

seconds :: Offset -> Int Source #

The seconds component of the Offset (carries the sign; e.g. -1 for a -00:00:01 offset).

minutes :: Offset -> Int Source #

The minutes component of the Offset (carries the sign; e.g. -30 for a -01:30 offset).

hours :: Offset -> Int Source #

The hours component of the Offset (carries the sign; e.g. -1 for a -01:30 offset).

Math

addClamped :: Offset -> Offset -> Offset Source #

Add one Offset to another NOTE: if the result of the addition is outside the accepted range it will be clamped

minusClamped :: Offset -> Offset -> Offset Source #

Subtract one Offset to another. NOTE: See add above