time

package module
v0.0.0-...-23f96bd Latest Latest
Warning

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

Go to latest
Published: Jan 27, 2018 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Nanosecond  Duration = 1
	Microsecond          = 1000 * Nanosecond
	Millisecond          = 1000 * Microsecond
	Second               = 1000 * Millisecond
	Minute               = 60 * Second
	Hour                 = 60 * Minute
)

Common durations. There is no definition for units of Day or larger to avoid confusion across daylight savings time zone transitions.

View Source
const LocalDateFormat = "2006-01-02" // yyyy-mm-dd

LocalDateFormat is used to render LocalDate as string

Variables

View Source
var CurrentTimeKey = TimeCtxKey("current_time")

TimeKey is key to keep current time in context

View Source
var ErrPeriodInvalidFromAfterTo = errors.New(`Invalid time period - "from" is after "to"`)

ErrPeriodInvalidFromAfterTo is returned when Period creating is called with invalid parameters: from > to

View Source
var ErrPeriodInvalidParamNull = errors.New(`Invalid time period - date is null`)

ErrPeriodInvalidParamNull is returned when Period creating is called with invalid parameters: from or to is NullLocalDate

View Source
var Midnight = MustCreateNewLocalTime(0, 0)
View Source
var NullLocalDate = LocalDate{/* contains filtered or unexported fields */}

NullLocalDate is used to represent missing date value

View Source
var NullLocalDateTime = LocalDateTime{/* contains filtered or unexported fields */}

NullLocalDateTime is used to represent missing LocalDateTime value

View Source
var NullLocalTime = LocalTime{/* contains filtered or unexported fields */}

NullLocalTime is used to represent missing date value

Functions

This section is empty.

Types

type Clock

type Clock interface {
	Today(ctx context.Context) LocalDate
	Now(ctx context.Context) LocalDateTime
	GoTime(ctx context.Context) time.Time
	ToUnixTimestamp(ctx context.Context, localDateTime LocalDateTime) UnixTimeStamp
}

Clock provides information about current time

func NewClock

func NewClock() Clock

NewClock creates new Clock

type DateTimeSpan

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

DateTimeSpan represents time span between two moments

func MustNewOpenDateTimeSpanFrom

func MustNewOpenDateTimeSpanFrom(from LocalDateTime) DateTimeSpan

func NewDateTimeSpan

func NewDateTimeSpan(from LocalDateTime, to LocalDateTime) DateTimeSpan

NewDateTimeSpan creates DateTimeSpan instance

func NewOpenDateTimeSpanFrom

func NewOpenDateTimeSpanFrom(from LocalDateTime) (DateTimeSpan, error)

NewOpenDateTimeSpanFrom creates DateTimeSpan that represents a set of days between "from" (inclusive) and eternity

func (DateTimeSpan) Contains

func (ts DateTimeSpan) Contains(other DateTimeSpan) bool

Contains checks if DateTimeSpan fully covers other DateTimeSpan

func (DateTimeSpan) Empty

func (ts DateTimeSpan) Empty() bool

Empty checks if DateTimeStamp has zero duration

func (DateTimeSpan) From

func (ts DateTimeSpan) From() LocalDateTime

From returns start of DateTimeSpan

func (DateTimeSpan) MarshalJSON

func (ts DateTimeSpan) MarshalJSON() ([]byte, error)

func (DateTimeSpan) NullableTo

func (ts DateTimeSpan) NullableTo() NullableLocalDateTime

NullableTo returns last day of a period or null

func (DateTimeSpan) Overlaps

func (ts DateTimeSpan) Overlaps(other DateTimeSpan) bool

Overlaps checks if DateTimeSpan overlaps other DateTimeSpan

func (DateTimeSpan) Subtract

func (ts DateTimeSpan) Subtract(span DateTimeSpan) []DateTimeSpan

func (DateTimeSpan) TimeSpan

func (ts DateTimeSpan) TimeSpan() LocalTimeSpan

TimeSpan creates LocalTimeSpan based on this object start and stop time of a day. Start and stop must be on the same day and start can't be after end.

func (DateTimeSpan) To

func (ts DateTimeSpan) To() LocalDateTime

To returns end of DateTimeSpan

type Duration

type Duration int64

A Duration represents the elapsed time between two instants as an int64 nanosecond count. The representation limits the largest representable duration to approximately 290 years.

func CalculateDuration

func CalculateDuration(from LocalDateTime, to LocalDateTime) Duration

CalculateDuration returns time duration between two DateTimes

func (Duration) String

func (d Duration) String() string

type LocalDate

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

LocalDate represents a date without taking into account a timezone

func MustParseLocalDate

func MustParseLocalDate(date string) LocalDate

MustParseLocalDate parses string into date using LocalDateFormat and panics if it is impossible

func NewLocalDate

func NewLocalDate(year int, month time.Month, day int) LocalDate

NewLocalDate creates instances of LocalDate

func ParseLocalDate

func ParseLocalDate(date string) (LocalDate, error)

ParseLocalDate parses string into date using LocalDateFormat

func ToLocalDate

func ToLocalDate(date time.Time) LocalDate

ToLocalDate converts date from go's time.Time to LocalDate.

func (LocalDate) AddDate

func (d LocalDate) AddDate(years, months, days int) LocalDate

AddDate returns the date corresponding to adding the given number of years, months and days to d.

func (LocalDate) After

func (d LocalDate) After(u LocalDate) bool

After reports whether date d is after u.

func (LocalDate) BeforeOrEqual

func (d LocalDate) BeforeOrEqual(u LocalDate) bool

BeforeOrEqual reports wheter the date d is before or equal to u.

func (LocalDate) Date

func (d LocalDate) Date() (year int, month time.Month, day int)

Date returns the year, month, and day in which d occurs.

func (LocalDate) End

func (d LocalDate) End() LocalDateTime

func (LocalDate) Equal

func (d LocalDate) Equal(u LocalDate) bool

Equal reports wheter the date d is equal to u.

func (LocalDate) GetStartOfDayUTC

func (d LocalDate) GetStartOfDayUTC() time.Time

GetStartOfDayUTC returns Time value that represent beginning of a day (00:00 AM) at UTC timezone

func (LocalDate) IsNull

func (d LocalDate) IsNull() bool

IsNull checks whether this variable represents missing value

func (LocalDate) MarshalJSON

func (d LocalDate) MarshalJSON() ([]byte, error)

MarshalJSON marshals date to JSON

func (LocalDate) MarshalText

func (d LocalDate) MarshalText() ([]byte, error)

MarshalText serializes this date type to string

func (LocalDate) Next

func (d LocalDate) Next() LocalDate

Next returns the next day

func (*LocalDate) Scan

func (d *LocalDate) Scan(value interface{}) error

Scan implements the Scanner interface.

func (LocalDate) Start

func (d LocalDate) Start() LocalDateTime

func (LocalDate) String

func (d LocalDate) String() string

func (*LocalDate) UnmarshalJSON

func (d *LocalDate) UnmarshalJSON(data []byte) error

UnmarshalJSON parses JSON string into date using LocalDateFormat

func (*LocalDate) UnmarshalText

func (d *LocalDate) UnmarshalText(text []byte) error

UnmarshalText parses string into date using LocalDateFormat

func (LocalDate) Value

func (d LocalDate) Value() (driver.Value, error)

Value implements the sql driver Valuer interface.

func (LocalDate) Weekday

func (d LocalDate) Weekday() Weekday

Weekday returns weekday of a given date

func (LocalDate) WithTime

func (d LocalDate) WithTime(localTime LocalTime) LocalDateTime

WithTime creates time.Time instance when combined with LocalTime

func (LocalDate) Year

func (d LocalDate) Year() int

type LocalDateTime

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

LocalDateTime represents a date and time without taking into account a timezone

func Earlier

func Earlier(dateTime1, dateTime2 LocalDateTime) LocalDateTime

func MustParseLocalDateTime

func MustParseLocalDateTime(datetime string) LocalDateTime

MustParseLocalDateTime parses string into LocalDateTime

func NewLocalDateTime

func NewLocalDateTime(t time.Time) LocalDateTime

NewLocalDateTime creates instances of LocalDateTime

func ParseLocalDateTime

func ParseLocalDateTime(datetime string) (LocalDateTime, error)

ParseLocalDateTime parses string into LocalDateTime

func (LocalDateTime) Add

Add returns the date-time equal to ldt+d.

func (LocalDateTime) AddDate

func (ldt LocalDateTime) AddDate(years, months, days int) LocalDateTime

AddDate returns the date-time increased by the given number of years, months and days.

func (LocalDateTime) After

func (ldt LocalDateTime) After(other LocalDateTime) bool

After reports whether this object if after method argument

func (LocalDateTime) Before

func (ldt LocalDateTime) Before(other LocalDateTime) bool

Before reports whether this object if before method argument

func (LocalDateTime) Date

func (ldt LocalDateTime) Date() LocalDate

Date returns the date component from LocalDateTime

func (LocalDateTime) Earlier

func (ldt LocalDateTime) Earlier(other LocalDateTime) LocalDateTime

func (LocalDateTime) GoTime

func (ldt LocalDateTime) GoTime(location *time.Location) time.Time

GoTime returns converts LocalDateTime to time.Time, keeping same numeric date and time values and sets timezone

func (LocalDateTime) IsNull

func (ldt LocalDateTime) IsNull() bool

IsNull checks whether this variable represents missing value

func (LocalDateTime) MarshalJSON

func (ldt LocalDateTime) MarshalJSON() ([]byte, error)

MarshalJSON marshals date to JSON

func (*LocalDateTime) Scan

func (ldt *LocalDateTime) Scan(value interface{}) error

Scan implements the Scanner interface.

func (LocalDateTime) String

func (ldt LocalDateTime) String() string

func (LocalDateTime) Sub

Sub returns the duration ldt-u. If the result exceeds the maximum (or minimum) value that can be stored in a Duration, the maximum (or minimum) duration will be returned. To compute ldt-d for a duration d, use Add(-d).

func (LocalDateTime) Time

func (ldt LocalDateTime) Time() LocalTime

Time returns the time component from LocalDateTime

func (*LocalDateTime) UnmarshalJSON

func (ldt *LocalDateTime) UnmarshalJSON(data []byte) error

UnmarshalJSON parses JSON string into LocalDateTime

func (LocalDateTime) Value

func (ldt LocalDateTime) Value() (driver.Value, error)

Value implements the sql driver Valuer interface.

func (LocalDateTime) Weekday

func (ldt LocalDateTime) Weekday() Weekday

Weekday returns weekday of a given date

type LocalTime

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

LocalTime represents time of a day without taking into account a timezone

func MustCreateNewLocalTime

func MustCreateNewLocalTime(hour, minute int) LocalTime

MustCreateNewLocalTime is like NewLocalTime but panics on error

func MustParseLocalTime

func MustParseLocalTime(value string) LocalTime

MustParseLocalTime parses string in form of "15:04"

func NewLocalTime

func NewLocalTime(hour, minute int) (LocalTime, error)

NewLocalTime creates instances of LocalTime

func ParseLocalTime

func ParseLocalTime(date string) (LocalTime, error)

ParseLocalTime parses string into LocalTime

func ToLocalTime

func ToLocalTime(t time.Time) LocalTime

ToLocalTime converts time from go's time.Time to LocalTime.

func (LocalTime) After

func (t LocalTime) After(other LocalTime) bool

After checks if given local time is after method argument

func (LocalTime) Before

func (t LocalTime) Before(other LocalTime) bool

Before checks if given local time is before method argument

func (LocalTime) Hour

func (t LocalTime) Hour() int

Hour returns hour part of time

func (LocalTime) MarshalJSON

func (t LocalTime) MarshalJSON() ([]byte, error)

MarshalJSON marshals locat time to JSON

func (LocalTime) Minutes

func (t LocalTime) Minutes() int

Minutes returns minutes part of time

func (*LocalTime) Scan

func (t *LocalTime) Scan(value interface{}) error

Scan implements the Scanner interface.

func (LocalTime) String

func (t LocalTime) String() string

func (*LocalTime) UnmarshalJSON

func (t *LocalTime) UnmarshalJSON(data []byte) error

UnmarshalJSON parses JSON string into LocalTime

func (LocalTime) Value

func (t LocalTime) Value() (driver.Value, error)

Value implements the sql driver Valuer interface.

type LocalTimeSpan

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

LocalTimeSpan represents a span between two LocalTime instances

func MustParseTimeSpan

func MustParseTimeSpan(value string) LocalTimeSpan

MustParseTimeSpan parses string in form of "15:04-15:04" into LocalTimeSpan

func NewTimeSpan

func NewTimeSpan(from LocalTime, to LocalTime) LocalTimeSpan

NewTimeSpan creates new LocalTimeSpan

func (LocalTimeSpan) Contains

func (ts LocalTimeSpan) Contains(dateTimeSpan DateTimeSpan) bool

Contains check if LocalTimeSpan contains DateTimeSpan

func (LocalTimeSpan) DateTimeSpanWithinOneDay

func (ts LocalTimeSpan) DateTimeSpanWithinOneDay(date LocalDate) DateTimeSpan

DateTimeSpanWithinOneDay creates DateTimeSpan with start and end on the given day and with time corresponding to LocalTimeSpan start and end.

func (LocalTimeSpan) Duration

func (ts LocalTimeSpan) Duration() Duration

func (LocalTimeSpan) From

func (ts LocalTimeSpan) From() LocalTime

From returns start of LocalTimeSpan

func (LocalTimeSpan) Overlaps

func (ts LocalTimeSpan) Overlaps(other LocalTimeSpan) bool

Overlaps checks if LocalTimeSpan overlaps other LocalTimeSpan

func (LocalTimeSpan) OverlapsDateTimeSpan

func (ts LocalTimeSpan) OverlapsDateTimeSpan(dateTimeSpan DateTimeSpan) bool

OverlapsDateTimeSpan checks if LocalTimeSpan overlaps DateTimeSpan

func (LocalTimeSpan) To

func (ts LocalTimeSpan) To() LocalTime

To returns end of LocalTimeSpan

func (LocalTimeSpan) Valid

func (ts LocalTimeSpan) Valid() bool

type NullableLocalDate

type NullableLocalDate struct {
	Date  LocalDate
	Valid bool
}

NullableLocalDate represents a LocalDate that may be null. It implements the sql.Scanner interface so it can be used as a scan destination, similar to sql.NullString.

func (*NullableLocalDate) Scan

func (d *NullableLocalDate) Scan(value interface{}) error

Scan implements the Scanner interface.

func (NullableLocalDate) Value

func (d NullableLocalDate) Value() (driver.Value, error)

Value implements the sql driver Valuer interface.

type NullableLocalDateTime

type NullableLocalDateTime struct {
	DateTime LocalDateTime
	Valid    bool
}

NullableLocalDateTime represents a LocalDateTime that may be null. It implements the sql.Scanner interface so it can be used as a scan destination, similar to sql.NullString.

func (*NullableLocalDateTime) Scan

func (d *NullableLocalDateTime) Scan(value interface{}) error

Scan implements the Scanner interface.

func (NullableLocalDateTime) Value

func (d NullableLocalDateTime) Value() (driver.Value, error)

Value implements the sql driver Valuer interface.

type Period

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

Period represents a set of days between two dates (inclusive)

func MustNewOneDayPeriod

func MustNewOneDayPeriod(date LocalDate) Period

func MustNewOpenPeriodFrom

func MustNewOpenPeriodFrom(from LocalDate) Period

func MustNewPeriod

func MustNewPeriod(from LocalDate, to LocalDate) Period

func NewOneDayPeriod

func NewOneDayPeriod(date LocalDate) (Period, error)

NewOneDayPeriod creates Period that represents a single day

func NewOpenPeriodFrom

func NewOpenPeriodFrom(from LocalDate) (Period, error)

NewOpenPeriodFrom creates Period that represents a set of days between "from" (inclusive) and eternity

func NewPeriod

func NewPeriod(from LocalDate, to LocalDate) (Period, error)

NewPeriod creates Period that represents a set of days between two dates: "from" (inclusive) and "to" (inclusive)

func (Period) Contains

func (p Period) Contains(date LocalDate) bool

Contains tells if period contains given date

func (Period) Days

func (p Period) Days() []LocalDate

Days returns a slice of all days in a period in order

func (Period) From

func (p Period) From() LocalDate

From returns first day of a period

func (Period) IsOpen

func (p Period) IsOpen() bool

func (Period) NullableTo

func (p Period) NullableTo() NullableLocalDate

NullableTo returns last day of a period or null

func (Period) String

func (p Period) String() string

func (Period) To

func (p Period) To() LocalDate

To returns last day of a period

func (Period) ToDateTimeSpan

func (p Period) ToDateTimeSpan() DateTimeSpan

type Source

type Source interface {
	Now(ctx context.Context) (*time.Time, error)
}

func NewRESTSource

func NewRESTSource(url string) Source

func NewSystemSource

func NewSystemSource() Source

type TimeCtxKey

type TimeCtxKey string

UserTypeCtxKey is type to keep time in context

type UnixTimeStamp

type UnixTimeStamp int64

UnixTimeStamp represents number of seconds elapsed since January 1, 1970 UTC.

func (*UnixTimeStamp) Scan

func (uts *UnixTimeStamp) Scan(value interface{}) error

Scan implements the Scanner interface.

func (UnixTimeStamp) SecondsTo

func (uts UnixTimeStamp) SecondsTo(t time.Time) int64

func (UnixTimeStamp) Value

func (uts UnixTimeStamp) Value() (driver.Value, error)

Value implements the sql driver Valuer interface.

type Weekday

type Weekday time.Weekday

Weekday represents day of a week

const (
	NotAWeekday Weekday = -1 + iota
	Sunday
	Monday
	Tuesday
	Wednesday
	Thursday
	Friday
	Saturday
)

Constants representing days of a week

func ParseWeekday

func ParseWeekday(weekdayString string) (Weekday, error)

ParseWeekday parses string into Weekday

func (Weekday) MarshalJSON

func (w Weekday) MarshalJSON() ([]byte, error)

MarshalJSON marshals date to JSON

func (Weekday) MarshalText

func (w Weekday) MarshalText() ([]byte, error)

MarshalText serializes this date type to string

func (Weekday) String

func (w Weekday) String() string

func (*Weekday) UnmarshalText

func (w *Weekday) UnmarshalText(text []byte) error

UnmarshalText parses string into weekday

func (Weekday) Value

func (w Weekday) Value() (driver.Value, error)

Value implements the sql driver Valuer interface.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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