| Copyright | (C) 2016 Jason Johnson |
|---|---|
| License | BSD-style (see the file LICENSE) |
| Maintainer | Jason Johnson <jason.johnson.081@gmail.com> |
| Stability | experimental |
| Portability | POSIX, Windows |
| Safe Haskell | Safe-Inferred |
| Language | Haskell2010 |
Data.HodaTime.Offset
Contents
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 : as a lens it would buy nothing over the arithmetic that is already here.addClamped o (fromMinutes
45)
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 minutesof-01:30): is the minutes part30or-30? We answer that for reads by making the accessors sign-consistent — each component carries the offset's sign, so-01:30gives-1hours and-30minutes andhours*3600 + minutes*60 + secondsalways 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
An Offset from UTC in seconds.
Constructors
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).