timehelper

package module
v0.0.0-...-1fb121f Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2016 License: Apache-2.0 Imports: 10 Imported by: 0

README

timehelper

Build Status Coverage Status GoDoc

GoLang "time" package helpers.

Documentation

Index

Constants

View Source
const (
	SecondPrecision      = 0
	MillisecondPrecision = 3
	MicrosecondPrecision = 6
	NanosecondPrecision  = 9
	PicosecondPrecision  = 12
	GoPrecision          = NanosecondPrecision
	PostgreSQLPrecision  = MicrosecondPrecision
)
View Source
const (
	// Base
	MonthsInYear = 12
	HoursInDay   = 24
	MinsInHour   = 60
	SecsInMin    = 60

	// Derived
	MinsInDay  = MinsInHour * HoursInDay
	SecsInDay  = SecsInMin * MinsInDay
	SecsInHour = SecsInMin * MinsInHour

	// Seconds fraction
	MillisecsInSec = 1e3
	MicrosecsInSec = 1e6
	NanosecsInSec  = 1e9
	PicosecsInSec  = 1e12

	MicrosecsInMillisec = 1e3
	NanosecsInMillisec  = 1e6
	PicosecsInMillisec  = 1e9

	NanosecsInMicrosec = 1e3
	PicosecsInMicrosec = 1e6

	PicosecsInNanosec = 1e3
)
View Source
const (
	// Approximate
	DaysInMonth = 30
)
View Source
const SimpleLayout = "2006-01-02 15:04:05"

SimpleLayout is additional predefined layout for use in Time.Format and Time.Parse. The reference time used in the layouts is the specific time: "Mon Jan 2 15:04:05 MST 2006".

Variables

This section is empty.

Functions

func UnixEpoch

func UnixEpoch() time.Time

UnixEpoch returns local Time corresponding to the beginning of UNIX epoch.

Types

type Interval

type Interval struct {
	Months      int32
	Days        int32
	SomeSeconds int64
	// contains filtered or unexported fields
}

Interval represent time interval in Postgres-compatible way. It consists of 3 public fields:

Months - number months
Days - number of days
SomeSeconds - number of seconds or some smaller units (depends on precision).

All fields are signed. Sign of one field is independent from sign of other field. Interval internally stores precision. Precision is number of digits after comma in 10-based representation of seconds. Precision can be from [0; 12] where 0 means that SomeSeconds is seconds and 12 means that SomeSeconds is picoseconds. If Interval created without calling constructor when it has 0 precision (i.e. SomeSeconds is just seconds). If Interval created with calling constructor and its documentation does not say another when it has precision = 9 (i.e. SomeSeconds is nanoseconds). This is because default Go time type has nanosecond precision. If interval is used to store PostgreSQL Interval when recommended precision is 6 (microsecond) because PostgreSQL use microsecond. This type is similar to Postgres interval data type. Value from one field is never automatically translated to value of another field, so <60*60*24 seconds> != <1 days> and so on. This is because of:

  1. compatibility with Postgres;
  2. day may have different amount of seconds and month may have different amount of days.

func Day

func Day() Interval

Day returns new Interval equal to 1 day

func Diff

func Diff(from, to time.Time) Interval

Diff calculates difference between given timestamps (time.Time) as nanoseconds and returns result as Interval (=to-from). Result always have months & days parts set to zero.

func DiffExtended

func DiffExtended(from, to time.Time) (i Interval)

DiffExtended is similar to Diff but calculates difference in months, days & nanoseconds instead of just nanoseconds (=to-from). Result may have non-zero months & days parts. DiffExtended use Location of both passed times while calculation. Most of time it is better to pass times with the same Location (UTC or not).

func FromDuration

func FromDuration(d time.Duration) Interval

FromDuration returns new Interval equivalent for given time.Duration (convert time.Duration to Interval).

func Hour

func Hour() Interval

Hour returns new Interval equal to 1 hour (3600 seconds)

func Microsecond

func Microsecond() Interval

Microsecond returns new Interval equal to 1 microsecond

func Millisecond

func Millisecond() Interval

Millisecond returns new Interval equal to 1 millisecond

func Minute

func Minute() Interval

Minute returns new Interval equal to 1 minute (60 seconds)

func Month

func Month() Interval

Month returns new Interval equal to 1 month

func Nanosecond

func Nanosecond() Interval

Nanosecond returns new Interval equal to 1 nanosecond

func NewGoInterval

func NewGoInterval() Interval

New returns zero interval with GoLang precision (= nanosecond)

func NewInterval

func NewInterval(p uint8) Interval

New returns zero interval with specified precision p

func NewPgInterval

func NewPgInterval() Interval

New returns zero interval with PostgreSQL precision (= microsecond)

func Parse

func Parse(s string, p uint8) (i Interval, err error)

Parse parses incoming string and extract interval with requested precision p. Format is postgres style specification for interval output format. Examples:

-1 year 2 mons -3 days 04:05:06.789
1 mons
2 year -34:56:78
00:00:00

func Picosecond

func Picosecond() Interval

Nanosecond returns new Interval equal to 1 picosecond. This constructor return interval with precision = 12 (picosecond).

func Second

func Second() Interval

Second returns new Interval equal to 1 second

func Since

func Since(t time.Time) Interval

Since returns elapsed time since given timestamp as Interval (=Diff(t, time.New()) Result always have months & days parts set to zero.

func SinceExtended

func SinceExtended(t time.Time) Interval

SinceExtended returns elapsed time since given timestamp as Interval (=DiffExtended(t, time.New()) Result may have non-zero months & days parts.

func Year

func Year() Interval

Year returns new Interval equal to 1 year (12 months)

func (Interval) Add

func (i Interval) Add(add Interval) Interval

Add adds given Interval to original Interval. Original Interval will be changed. TODO 'will be changed'?

func (Interval) AddTo

func (i Interval) AddTo(t time.Time) time.Time

AddTo adds original Interval to given timestamp and return result.

func (Interval) Comparable

func (i Interval) Comparable(i2 Interval) bool

Comparable returns true only if it is possible to compare Intervals. Intervals "A" and "B" can be compared only if:

  1. all parts of "A" are less or equal to relative parts of "B" or
  2. all parts of "B" are less or equal to relative parts of "A".

In the other words, it is impossible to compare "30 days"-Interval with "1 month"-Interval.

func (Interval) Div

func (i Interval) Div(div int64) Interval

Div divides interval by mul. Each part of Interval divides independently. Round rule: 0.4=>0 ; 0.5=>1 ; 0.6=>1 ; -0.4=>0 ; -0.5=>-1 ; -0.6=>-1 Original Interval will be changed. TODO 'will be changed'?

func (Interval) Duration

func (i Interval) Duration(daysInMonth uint8, minutesInDay uint32) time.Duration

Duration convert Interval to time.Duration. It is required to pass number of days in month (usually 30 or something near) and number of minutes in day (usually 1440) because of converting months and days parts of original Interval to time.Duration nanoseconds. Warning: this method is inaccuracy because in real life daysInMonth & minutesInDay vary and depends on relative timestamp.

func (Interval) Encode

func (u Interval) Encode(w *pgx.WriteBuf, oid pgx.Oid) error

Encode implements the pgx.Encoder interface.

func (Interval) Equal

func (i Interval) Equal(i2 Interval) bool

Equal compare original Interval with given for full equality part by part.

func (Interval) FormatCode

func (u Interval) FormatCode() int16

FormatCode implements the pgx.Encoder interface.

func (Interval) Greater

func (i Interval) Greater(i2 Interval) bool

Greater returns true if at least one part of original Interval is greater then relative part of i2 and all other parts of original Interval are greater or equal to relative parts of i2.

func (Interval) GreaterOrEqual

func (i Interval) GreaterOrEqual(i2 Interval) bool

GreaterOrEqual returns true if all parts of original Interval are greater or equal to relative parts of i2.

func (Interval) In

func (i Interval) In(i2 Interval) int64

In counts how many i contains in i2 (=i2/i). Round rule: 0.4=>0 ; 0.5=>1 ; 0.6=>1 ; -0.4=>0 ; -0.5=>-1 ; -0.6=>-1 TODO A lot of overflows

func (Interval) Less

func (i Interval) Less(i2 Interval) bool

Less returns true if at least one part of original Interval is less then relative part of i2 and all other parts of original Interval are less or equal to relative parts of i2.

func (Interval) LessOrEqual

func (i Interval) LessOrEqual(i2 Interval) bool

LessOrEqual returns true if all parts of original Interval are less or equal to relative parts of i2.

func (Interval) Mul

func (i Interval) Mul(mul int64) Interval

Mul multiples interval by mul. Each part of Interval multiples independently. Original Interval will be changed. TODO 'will be changed'?

func (Interval) NormalDays

func (i Interval) NormalDays() int32

NormalDays just returns Days part.

func (Interval) NormalHours

func (i Interval) NormalHours() int64

NormalHours returns number of hours in seconds part.

func (Interval) NormalMinutes

func (i Interval) NormalMinutes() int8

NormalMinutes returns number of hours in seconds part after subtracting NormalHours.

func (Interval) NormalMonths

func (i Interval) NormalMonths() int32

NormalMonths return number of months in month part after subtracting NormalYears*12 (as i.Months % 12). Examples: if .Months = 11 then NormalMonths = 11, but if .Months = 13 then NormalMonths = 1.

func (Interval) NormalNanoseconds

func (i Interval) NormalNanoseconds() int32

NormalNanoseconds returns number of nanoseconds in fraction part of seconds part.

func (Interval) NormalSeconds

func (i Interval) NormalSeconds() int8

NormalSeconds returns number of seconds in seconds part after subtracting NormalHours*3600 and NormalMinutes*60 (as i.Seconds % 60).

func (Interval) NormalYears

func (i Interval) NormalYears() int32

NormalYears return number of years in month part (as i.Months / 12).

func (Interval) Precision

func (i Interval) Precision() uint8

Precision returns internally stored precision.

func (*Interval) Scan

func (u *Interval) Scan(vr *pgx.ValueReader) error

Scan implements the pgx.Scanner interface.

func (Interval) SetPrecision

func (i Interval) SetPrecision(p uint8) Interval

SetPrecision change interval precision and do appropriate stored value recalculation. Possible precision is 0..12 where 0 means second precision and 9 means nanosecond precision. If passed p>12 it will be silently replaced with p=12.

func (Interval) String

func (i Interval) String() string

String returns string representation of interval. Output format is the same as for Parse

func (Interval) Sub

func (i Interval) Sub(sub Interval) Interval

Sub subtracts given Interval from original Interval. Original Interval will be changed. TODO 'will be changed'?

func (Interval) SubFrom

func (i Interval) SubFrom(t time.Time) time.Time

SubFrom subtract original Interval from given timestamp and return result.

Jump to

Keyboard shortcuts

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