timex

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 18, 2021 License: MIT Imports: 2 Imported by: 0

README

Timex

Go Report Card Codacy Badge codecov Build Status GoDoc Licenses

Introduction

Time eXtension package providing extra features to work with time for golang

Usage

Installation
go get github.com/askretov/timex
Time Interval examples
// Create an interval
start := time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC)
i := NewInterval(start, start.AddDate(0, 0, 9))
fmt.Println(i)
// > 2021-01-01T00:00:00Z - 2021-01-10T00:00:00Z

// Print the interval duration in days
fmt.Println(i.Days())
// > 9

// Check if t inside of i
t := start.AddDate(0, 0, 5)
fmt.Println(i.Contains(t))
// > true

// Get a half-open interval for i
ho := i.HalfOpenEnd()
fmt.Println(ho)
// > 2021-01-01T00:00:00Z - 2021-01-09T23:59:59Z
A lot more features coming soon, stay tuned!

Documentation

Index

Constants

View Source
const (
	Nanosecond  = units(time.Nanosecond)
	Microsecond = 1000 * Nanosecond
	Millisecond = 1000 * Microsecond
	Second      = 1000 * Millisecond
	Minute      = 60 * Second
	Hour        = 60 * Minute
	Day         = 24 * Hour
	Week        = 7 * Day
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Interval

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

Interval describes a time interval between start and end time values

func NewInterval

func NewInterval(t1, t2 time.Time) Interval

NewInterval returns a new instance of Interval between start and end Input is order-independent, the smaller value will be used as the start when bigger as the end

func (Interval) Contains

func (i Interval) Contains(t time.Time) bool

Contains reports whether t is within of i (closed interval strategy, start <= t <= end)

func (Interval) Days

func (i Interval) Days() float64

Days returns a duration of interval in days

func (Interval) Duration

func (i Interval) Duration() time.Duration

Duration returns i's duration as time.Duration value

func (Interval) End

func (i Interval) End() time.Time

End returns i's end value

func (*Interval) ExtendEnd added in v1.2.0

func (i *Interval) ExtendEnd(d time.Duration)

ExtendEnd extends i to the end adding d for i.End() Example:

fmt.Println(i.End()) // 2021-07-03T10:30:00Z
i.ExtendEnd(time.Day * 2)
fmt.Println(i.End()) // 2021-07-05T10:30:00Z

func (*Interval) ExtendStart added in v1.2.0

func (i *Interval) ExtendStart(d time.Duration)

ExtendStart extends i from the start back in time adding d in the form of i.Start.Add(-d) Example:

fmt.Println(i.Start()) // 2021-07-03T10:30:00Z
i.ExtendStart(time.Day * 2)
fmt.Println(i.Start()) // 2021-07-01T10:30:00Z

func (Interval) HalfOpenEnd

func (i Interval) HalfOpenEnd() Interval

HalfOpenEnd returns i as a half-open interval where the open side is on the end [start, end) Actually, it returns copy of i where end = end - 1 nanosecond

func (Interval) Hours

func (i Interval) Hours() float64

Hours returns a duration of interval in hours

func (Interval) IsValid

func (i Interval) IsValid() bool

IsValid reports whether i's start & end both values are not zero

func (Interval) IsZero

func (i Interval) IsZero() bool

IsZero reports whether i's start & end values are zero time values

func (Interval) Microseconds

func (i Interval) Microseconds() float64

Microseconds returns a duration of interval in microseconds

func (Interval) Milliseconds

func (i Interval) Milliseconds() float64

Milliseconds returns a duration of interval in milliseconds

func (Interval) Minutes

func (i Interval) Minutes() float64

Minutes returns a duration of interval in minutes

func (Interval) Nanoseconds

func (i Interval) Nanoseconds() int64

Nanoseconds returns a duration of interval in hours

func (Interval) Seconds

func (i Interval) Seconds() float64

Seconds returns a duration of interval in seconds

func (Interval) Start

func (i Interval) Start() time.Time

Start returns i's start value

func (Interval) String

func (i Interval) String() string

String returns string representation of i

func (Interval) StringDates

func (i Interval) StringDates() string

StringDates returns string representation of i as date part only

Jump to

Keyboard shortcuts

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