timex

package module
v0.0.0-...-ed02548 Latest Latest
Warning

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

Go to latest
Published: Aug 21, 2018 License: MIT Imports: 3 Imported by: 0

README

Timex

Build Status

Timex is a golang time toolkit package based on time package

Install

go get -u -v github.com/DarthPestilane/timex

Usage

New Timex object
import "github.com/DarthPestilane/timex"

// current time
t := timex.Now() // -> UTC 2018-07-23 15:09:45
t := timex.NowInTz("PRC") // -> CST 2018-07-23 23:09:45

// based on `time.Time`
t := timex.New(time.Now())

// parse string
t := timex.FuzzyParse("2018-07-23 15:45:24")
// also provides `Parse` `MustParse` `ParseInTz` `MustParseInTz`` functions
Format to string
t.DatetimeStr() // 2018-07-23 14:59:58
t.DateStr() // 2018-07-23 00:00:00

// for other formats
t.Format(layout)
StartOf/EndOf functions
t.StartOfDay().DatetimeStr() // 2018-07-23 00:00:00
t.EndOfDay().DatetimeStr() // 2018-07-23 23:59:59

t.StartOfMonth().DatetimeStr() // 2018-07-01 00:00:00
t.EndOfMonth().DatetimeStr() // 2018-07-31 23:59:59

t.StartOfYear().DatetimeStr() // 2018-01-01 00:00:00
t.EndOfYear().DatetimeStr() // 2018-12-31 23:59:59
Timezone convertion
t := timex.NowInTz("UTC") // -> UTC 2018-07-23 15:09:45
t.StartOfDay().MustTz("PRC").DatetimeStr() // 2018-07-23 08:00:00
Raw time.Time operation
tt := t.Raw // time.Time
fmt.Println("Current year is", tt.Year)

Contribute

Feel free

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type JsonTime

type JsonTime time.Time

func (JsonTime) MarshalJSON

func (j JsonTime) MarshalJSON() ([]byte, error)

func (*JsonTime) UnmarshalJSON

func (j *JsonTime) UnmarshalJSON(value []byte) error

type Timex

type Timex struct {
	Raw time.Time
}

func FuzzyParse

func FuzzyParse(value string) *Timex

FuzzyParse try diffrent format and parse a time string panic when no format matched

func MustNowInTz

func MustNowInTz(tz string) *Timex

MustNowInTz panic when error occured

func MustParse

func MustParse(layout, value string) *Timex

MustParse parse a time string with given format but panic when error occured

func MustParseInTz

func MustParseInTz(layout, value, tz string) *Timex

MustParseInTz parse a time string with given format in a timezone but panic when error occured

func New

func New(t time.Time) *Timex

New wrap time.Time into Timex

func Now

func Now() *Timex

Now current time

func NowInTz

func NowInTz(tz string) (*Timex, error)

NowInTz current time of timezone NowInTz("Asia/Chongqing")

func Parse

func Parse(layout, value string) (*Timex, error)

Parse parse a time string with given format

func ParseInTz

func ParseInTz(layout, value, tz string) (*Timex, error)

ParseInTz parse a time string with given format in a timezone

func (*Timex) DateStr

func (t *Timex) DateStr() string

DateStr format time into date string

func (*Timex) DatetimeStr

func (t *Timex) DatetimeStr() string

DatetimeStr format time into datetime string

func (*Timex) EndOfDay

func (t *Timex) EndOfDay() *Timex

EndOfDay calculate the end moment of day of time

func (*Timex) EndOfMonth

func (t *Timex) EndOfMonth() *Timex

EndOfMonth calculate the end moment of month of time

func (*Timex) EndOfYear

func (t *Timex) EndOfYear() *Timex

EndOfYear calculate the end moment of year of time

func (*Timex) Format

func (t *Timex) Format(layout string) string

Format format time to a string

func (*Timex) IsCurrentMonth

func (t *Timex) IsCurrentMonth() bool

IsCurrentMonth check if is in current month

func (*Timex) IsCurrentYear

func (t *Timex) IsCurrentYear() bool

IsCurrentYear check if is in current year

func (*Timex) MustTz

func (t *Timex) MustTz(tz string) *Timex

MustTz convert timezoen but panic when timezone is invalid

func (*Timex) StartOfDay

func (t *Timex) StartOfDay() *Timex

StartOfDay calculate the start moment of day of time

func (*Timex) StartOfMonth

func (t *Timex) StartOfMonth() *Timex

StartOfMonth calculate the start moment of month of time

func (*Timex) StartOfYear

func (t *Timex) StartOfYear() *Timex

StartOfYear calculate the start moment of year of time

func (*Timex) Tz

func (t *Timex) Tz(tz string) (*Timex, error)

Tz convert timezone

Jump to

Keyboard shortcuts

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