Documentation ¶
Index ¶
- Constants
- func UnixEpoch() time.Time
- type Interval
- func Day() Interval
- func Diff(from, to time.Time) Interval
- func DiffExtended(from, to time.Time) (i Interval)
- func FromDuration(d time.Duration) Interval
- func Hour() Interval
- func Microsecond() Interval
- func Millisecond() Interval
- func Minute() Interval
- func Month() Interval
- func Nanosecond() Interval
- func NewGoInterval() Interval
- func NewInterval(p uint8) Interval
- func NewPgInterval() Interval
- func Parse(s string, p uint8) (i Interval, err error)
- func Picosecond() Interval
- func Second() Interval
- func Since(t time.Time) Interval
- func SinceExtended(t time.Time) Interval
- func Year() Interval
- func (i Interval) Add(add Interval) Interval
- func (i Interval) AddTo(t time.Time) time.Time
- func (i Interval) Comparable(i2 Interval) bool
- func (i Interval) Div(div int64) Interval
- func (i Interval) Duration(daysInMonth uint8, minutesInDay uint32) time.Duration
- func (u Interval) Encode(w *pgx.WriteBuf, oid pgx.Oid) error
- func (i Interval) Equal(i2 Interval) bool
- func (u Interval) FormatCode() int16
- func (i Interval) Greater(i2 Interval) bool
- func (i Interval) GreaterOrEqual(i2 Interval) bool
- func (i Interval) In(i2 Interval) int64
- func (i Interval) Less(i2 Interval) bool
- func (i Interval) LessOrEqual(i2 Interval) bool
- func (i Interval) Mul(mul int64) Interval
- func (i Interval) NormalDays() int32
- func (i Interval) NormalHours() int64
- func (i Interval) NormalMinutes() int8
- func (i Interval) NormalMonths() int32
- func (i Interval) NormalNanoseconds() int32
- func (i Interval) NormalSeconds() int8
- func (i Interval) NormalYears() int32
- func (i Interval) Precision() uint8
- func (u *Interval) Scan(vr *pgx.ValueReader) error
- func (i Interval) SetPrecision(p uint8) Interval
- func (i Interval) String() string
- func (i Interval) Sub(sub Interval) Interval
- func (i Interval) SubFrom(t time.Time) time.Time
Constants ¶
const ( SecondPrecision = 0 MillisecondPrecision = 3 MicrosecondPrecision = 6 NanosecondPrecision = 9 PicosecondPrecision = 12 GoPrecision = NanosecondPrecision PostgreSQLPrecision = MicrosecondPrecision )
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 )
const (
// Approximate
DaysInMonth = 30
)
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 ¶
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:
- compatibility with Postgres;
- day may have different amount of seconds and month may have different amount of days.
func Diff ¶
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 ¶
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 ¶
FromDuration returns new Interval equivalent for given time.Duration (convert time.Duration to Interval).
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 NewGoInterval ¶
func NewGoInterval() Interval
New returns zero interval with GoLang precision (= nanosecond)
func NewInterval ¶
New returns zero interval with specified precision p
func NewPgInterval ¶
func NewPgInterval() Interval
New returns zero interval with PostgreSQL precision (= microsecond)
func Parse ¶
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 Since ¶
Since returns elapsed time since given timestamp as Interval (=Diff(t, time.New()) Result always have months & days parts set to zero.
func SinceExtended ¶
SinceExtended returns elapsed time since given timestamp as Interval (=DiffExtended(t, time.New()) Result may have non-zero months & days parts.
func (Interval) Add ¶
Add adds given Interval to original Interval. Original Interval will be changed. TODO 'will be changed'?
func (Interval) Comparable ¶
Comparable returns true only if it is possible to compare Intervals. Intervals "A" and "B" can be compared only if:
- all parts of "A" are less or equal to relative parts of "B" or
- 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 ¶
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 ¶
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) FormatCode ¶
FormatCode implements the pgx.Encoder interface.
func (Interval) Greater ¶
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 ¶
GreaterOrEqual returns true if all parts of original Interval are greater or equal to relative parts of i2.
func (Interval) In ¶
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 ¶
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 ¶
LessOrEqual returns true if all parts of original Interval are less or equal to relative parts of i2.
func (Interval) Mul ¶
Mul multiples interval by mul. Each part of Interval multiples independently. Original Interval will be changed. TODO 'will be changed'?
func (Interval) NormalDays ¶
NormalDays just returns Days part.
func (Interval) NormalHours ¶
NormalHours returns number of hours in seconds part.
func (Interval) NormalMinutes ¶
NormalMinutes returns number of hours in seconds part after subtracting NormalHours.
func (Interval) NormalMonths ¶
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 ¶
NormalNanoseconds returns number of nanoseconds in fraction part of seconds part.
func (Interval) NormalSeconds ¶
NormalSeconds returns number of seconds in seconds part after subtracting NormalHours*3600 and NormalMinutes*60 (as i.Seconds % 60).
func (Interval) NormalYears ¶
NormalYears return number of years in month part (as i.Months / 12).
func (*Interval) Scan ¶
func (u *Interval) Scan(vr *pgx.ValueReader) error
Scan implements the pgx.Scanner interface.
func (Interval) SetPrecision ¶
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 ¶
String returns string representation of interval. Output format is the same as for Parse