timespan

package
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 31, 2017 License: BSD-3-Clause Imports: 4 Imported by: 0

Documentation

Overview

Package timespan provides spans of time (TimeSpan), and ranges of dates (DateRange). Both are half-open intervals for which the start is included and the end is excluded. This allows for empty spans and also facilitates aggregating spans together.

Index

Constants

View Source
const TimestampFormat = "2006-01-02 15:04:05"

TimestampFormat is a simple format for date & time, "2006-01-02 15:04:05".

Variables

This section is empty.

Functions

This section is empty.

Types

type DateRange

type DateRange struct {
	// contains filtered or unexported fields
}

DateRange carries a date and a number of days and describes a range between two dates.

func EmptyRange

func EmptyRange(day Date) DateRange

EmptyRange constructs an empty range. This is often a useful basis for further operations but note that the end date is undefined.

func NewDateRange

func NewDateRange(start, end Date) DateRange

NewDateRange assembles a new date range from two dates. These are half-open, so if start and end are the same, the range spans zero (not one) day. Similarly, if they are on subsequent days, the range is one date (not two).

func NewDateRangeOf

func NewDateRangeOf(start time.Time, duration time.Duration) DateRange

NewDateRangeOf assembles a new date range from a start time and a duration, discarding the precise time-of-day information. The start time includes a location, which is not necessarily UTC. The duration can be negative.

func NewMonthOf

func NewMonthOf(year int, month time.Month) DateRange

NewMonthOf constructs the range encompassing the whole month specified for a given year. It handles leap years correctly.

func NewYearOf

func NewYearOf(year int) DateRange

NewYearOf constructs the range encompassing the whole year specified.

func OneDayRange

func OneDayRange(day Date) DateRange

OneDayRange constructs a range of exactly one day. This is often a useful basis for further operations. Note that the end date is the same as the start date.

func (DateRange) Contains

func (dateRange DateRange) Contains(d Date) bool

Contains tests whether the date range contains a specified date. Empty date ranges (i.e. zero days) never contain anything.

func (DateRange) ContainsTime

func (dateRange DateRange) ContainsTime(t time.Time) bool

ContainsTime tests whether a given local time is within the date range. The time range is from midnight on the start day to one nanosecond before midnight on the day after the end date. Empty date ranges (i.e. zero days) never contain anything.

If a calculation needs to be 'half-open' (i.e. the end date is exclusive), simply use the expression 'dateRange.ExtendBy(-1).ContainsTime(t)'

func (DateRange) Days

func (dateRange DateRange) Days() PeriodOfDays

Days returns the period represented by this range. This will never be negative.

func (DateRange) Duration

func (dateRange DateRange) Duration() time.Duration

Duration computes the duration (in nanoseconds) from midnight at the start of the date range up to and including the very last nanosecond before midnight on the end day. The calculation is for UTC, which does not have daylight saving and every day has 24 hours.

If the range is greater than approximately 290 years, the result will hard-limit to the minimum or maximum possible duration (see time.Sub(t)).

func (DateRange) DurationIn

func (dateRange DateRange) DurationIn(loc *time.Location) time.Duration

DurationIn computes the duration (in nanoseconds) from midnight at the start of the date range up to and including the very last nanosecond before midnight on the end day. The calculation is for the specified location, which may have daylight saving, so not every day necessarily has 24 hours. If the date range spans the day the clocks are changed, this is taken into account.

If the range is greater than approximately 290 years, the result will hard-limit to the minimum or maximum possible duration (see time.Sub(t)).

func (DateRange) End

func (dateRange DateRange) End() Date

End returns the date following the last date of the range. End can be considered to be the exclusive end, i.e. the final value of a half-open range.

If the range is empty (i.e. has zero days), then the start date is returned, this being also the (half-open) end value in that case. This is more useful than the undefined result returned by Last() for empty ranges.

func (DateRange) EndTimeIn

func (dateRange DateRange) EndTimeIn(loc *time.Location) time.Time

EndTimeIn returns the nanosecond after the end time in a specified location. Along with StartTimeIn, this gives a 'half-open' range where the start is inclusive and the end is exclusive.

func (DateRange) EndUTC

func (dateRange DateRange) EndUTC() time.Time

EndUTC assumes that the end date is a UTC date and returns the time a nanosecond after the end time in a specified location. Along with StartUTC, this gives a 'half-open' range where the start is inclusive and the end is exclusive.

func (DateRange) ExtendBy

func (dateRange DateRange) ExtendBy(days PeriodOfDays) DateRange

ExtendBy extends (or reduces) the date range by moving the end date. A negative parameter is allowed and this may cause the range to become inverted (i.e. the mark date becomes the end date instead of the start date).

func (DateRange) ExtendByPeriod

func (dateRange DateRange) ExtendByPeriod(period period.Period) DateRange

ExtendByPeriod extends (or reduces) the date range by moving the end date. A negative parameter is allowed and this may cause the range to become inverted (i.e. the mark date becomes the end date instead of the start date).

func (DateRange) IsEmpty

func (dateRange DateRange) IsEmpty() bool

IsEmpty returns true if this has a starting date but the range is empty (zero days).

func (DateRange) IsZero

func (dateRange DateRange) IsZero() bool

IsZero returns true if this has a zero start date and the the range is empty. Usually this is because the range was created via the zero value.

func (DateRange) Last

func (dateRange DateRange) Last() Date

Last returns the last date (inclusive) represented by this range. Be careful because if the range is empty (i.e. has zero days), then the last is undefined so an empty date is returned. Therefore it is often more useful to use End() instead of Last(). See also IsEmpty().

func (DateRange) Merge

func (thisRange DateRange) Merge(thatRange DateRange) DateRange

Merge combines two date ranges by calculating a date range that just encompasses them both. There are two special cases.

Firstly, if one range is entirely contained within the other range, the larger of the two is returned. Otherwise, the result is from the start of the earlier one to the end of the later one, even if the two ranges don't overlap.

Secondly, if either range is the zero value (see IsZero), it is excluded from the merge and the other range is returned unchanged.

func (DateRange) Normalise

func (dateRange DateRange) Normalise() DateRange

Normalise ensures that the number of days is zero or positive. The normalised date range is returned; in this value, the mark date is the same as the start date.

func (DateRange) ShiftBy

func (dateRange DateRange) ShiftBy(days PeriodOfDays) DateRange

ShiftBy moves the date range by moving both the start and end dates similarly. A negative parameter is allowed.

func (DateRange) ShiftByPeriod

func (dateRange DateRange) ShiftByPeriod(period period.Period) DateRange

ShiftByPeriod moves the date range by moving both the start and end dates similarly. A negative parameter is allowed.

func (DateRange) Start

func (dateRange DateRange) Start() Date

Start returns the earliest date represented by this range.

func (DateRange) StartTimeIn

func (dateRange DateRange) StartTimeIn(loc *time.Location) time.Time

StartTimeIn returns the start time in a specified location.

func (DateRange) StartUTC

func (dateRange DateRange) StartUTC() time.Time

StartUTC assumes that the start date is a UTC date and gets the start time of that date, as UTC. It returns midnight on the first day of the range.

func (DateRange) String

func (dateRange DateRange) String() string

String describes the date range in human-readable form.

func (DateRange) TimeSpanIn

func (dateRange DateRange) TimeSpanIn(loc *time.Location) TimeSpan

TimeSpanIn obtains the time span corresponding to the date range in a specified location. The result is normalised.

type TimeSpan

type TimeSpan struct {
	// contains filtered or unexported fields
}

TimeSpan holds a span of time between two instants with a 1 nanosecond resolution. It is implemented using a time.Duration, therefore is limited to a maximum span of 290 years.

func NewTimeSpan

func NewTimeSpan(t1, t2 time.Time) TimeSpan

NewTimeSpan creates a new time span from two times. The start and end can be in either order; the result will be normalised. The inputs are half-open: the start is included and the end is excluded.

func ZeroTimeSpan

func ZeroTimeSpan(start time.Time) TimeSpan

ZeroTimeSpan creates a new zero-duration time span at a specified time.

func (TimeSpan) Contains

func (ts TimeSpan) Contains(t time.Time) bool

Contains tests whether a given moment of time is enclosed within the time span. The start time is inclusive; the end time is exclusive. If t has a different locality to the time-span, it is adjusted accordingly.

func (TimeSpan) DateRangeIn

func (ts TimeSpan) DateRangeIn(loc *time.Location) DateRange

DateRangeIn obtains the date range corresponding to the time span in a specified location. The result is normalised.

func (TimeSpan) Duration

func (ts TimeSpan) Duration() time.Duration

Duration gets the duration of the time span.

func (TimeSpan) End

func (ts TimeSpan) End() time.Time

End gets the end time of the time span. Strictly, this is one nanosecond after the range of time included in the time span; this implements the half-open model.

func (TimeSpan) ExtendBy

func (ts TimeSpan) ExtendBy(d time.Duration) TimeSpan

ExtendBy lengthens the time span by a specified amount. The parameter may be negative, in which case it is possible that the end of the time span will appear to be before the start. However, the result is normalised so that the resulting start is the lesser value.

func (TimeSpan) ExtendWithoutWrapping

func (ts TimeSpan) ExtendWithoutWrapping(d time.Duration) TimeSpan

ExtendWithoutWrapping lengthens the time span by a specified amount. The parameter may be negative, but if its magnitude is large than the time span's duration, it will be truncated so that the result has zero duration in that case. The start time is never altered.

func (TimeSpan) In

func (ts TimeSpan) In(loc *time.Location) TimeSpan

In returns a TimeSpan adjusted from its current location to a new location. Because location is considered to be a presentational attribute, the actual time itself is not altered by this function. This matches the behaviour of time.Time.In(loc).

func (TimeSpan) IsEmpty

func (ts TimeSpan) IsEmpty() bool

IsEmpty returns true if this is an empty time span (zero duration).

func (TimeSpan) Merge

func (ts TimeSpan) Merge(other TimeSpan) TimeSpan

Merge combines two time spans by calculating a time span that just encompasses them both. As a special case, if one span is entirely contained within the other span, the larger of the two is returned. Otherwise, the result is the start of the earlier one to the end of the later one, even if the two spans don't overlap.

func (TimeSpan) Normalise

func (ts TimeSpan) Normalise() TimeSpan

Normalise ensures that the mark time is at the start time and the duration is positive. The normalised time span is returned.

func (TimeSpan) ShiftBy

func (ts TimeSpan) ShiftBy(d time.Duration) TimeSpan

ShiftBy moves the time span by moving both the start and end times similarly. A negative parameter is allowed.

func (TimeSpan) Start

func (ts TimeSpan) Start() time.Time

Start gets the end time of the time span.

func (TimeSpan) String

func (ts TimeSpan) String() string

String produces a human-readable description of a time span.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL