timex

package
v0.5.9 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2022 License: MIT Imports: 3 Imported by: 13

README

Timex

Provides an enhanced time.Time implementation, and add more commonly used functional methods.

Install

go get github.com/gookit/goutil/timex

Timex Usage

Create timex instance
now := timex.Now()

// from time.Time
tx := timex.New(time.Now())
tx := timex.FromTime(time.Now())

// from time unix
tx := timex.FromUnix(1647411580)

Create from datetime string:

// auto match layout by datetime
tx, err  := timex.FromString("2022-04-20 19:40:34")
// custom set the datetime layout
tx, err  := timex.FromString("2022-04-20 19:40:34", "2006-01-02 15:04:05")
// use date template as layout
tx, err  := timex.FromDate("2022-04-20 19:40:34", "Y-m-d H:I:S")
Use timex instance
tx := timex.Now()

Change time:

tx.Yesterday()
tx.Tomorrow()

tx.DayStart() // get time at Y-m-d 00:00:00
tx.DayEnd() // get time at Y-m-d 23:59:59
tx.HourStart() // get time at Y-m-d H:00:00
tx.HourEnd() // get time at Y-m-d H:59:59

tx.AddDay(2)
tx.AddHour(1)
tx.AddMinutes(15)
tx.AddSeconds(120)

Compare time:

// before compare
tx.IsBefore(u time.Time)
tx.IsBeforeUnix(1647411580)
// after compare
tx.IsAfter(u time.Time)
tx.IsAfterUnix(1647411580)
Helper functions
ts := timex.NowUnix() // current unix timestamp

t := NowAddDay(1) // from now add 1 day
t := NowAddHour(1) // from now add 1 hour
t := NowAddMinutes(3) // from now add 3 minutes
t := NowAddSeconds(180) // from now add 180 seconds
Convert time to date by template

Template Chars:

 Y,y - year
  Y - year 2006
  y - year 06
 m - month 01-12
 d - day 01-31
 H,h - hour
  H - hour 00-23
  h - hour 01-12
 I,i - minute
  I - minute 00-59
  i - minute 0-59
 S,s - second
  S - second 00-59
  s - second 0-59
... ...

More, please see charMap

Examples, use timex format date:

tx := timex.Now()
date := tx.DateFormat("Y-m-d H:I:S") // Output: 2022-04-20 19:09:03
date = tx.DateFormat("y-m-d h:i:s") // Output: 22-04-20 07:9:3

Format time.Time:

tx := time.Now()
date := timex.DateFormat(tx, "Y-m-d H:I:S") // Output: 2022-04-20 19:40:34

More usage:

ts := timex.NowUnix() // current unix timestamp

date := FormatUnix(ts, "2006-01-02 15:04:05") // Get: 2022-04-20 19:40:34
date := FormatUnixByTpl(ts, "Y-m-d H:I:S") // Get: 2022-04-20 19:40:34

Documentation

Overview

Package timex provides an enhanced time.Time implementation. Add more commonly used functional methods.

such as: DayStart(), DayAfter(), DayAgo(), DateFormat() and more.

Index

Constants

View Source
const (
	OneSecond  = 1
	OneMinSec  = 60
	OneHourSec = 3600
	OneDaySec  = 86400
	OneWeekSec = 7 * 86400

	Second  = time.Second
	OneMin  = time.Minute
	OneHour = time.Hour
	OneDay  = 24 * time.Hour
	OneWeek = 7 * 24 * time.Hour
)

provide some commonly time consts

Variables

View Source
var (
	// DefaultLayout template for format time
	DefaultLayout = "2006-01-02 15:04:05"
)

Functions

func AddDay

func AddDay(t time.Time, day int) time.Time

AddDay add some day time for given time

func AddHour

func AddHour(t time.Time, hour int) time.Time

AddHour add some hour time for given time

func AddMinutes

func AddMinutes(t time.Time, minutes int) time.Time

AddMinutes add some minutes time for given time

func AddSeconds

func AddSeconds(t time.Time, seconds int) time.Time

AddSeconds add some seconds time for given time

func Date added in v0.5.1

func Date(t time.Time, template string) string

Date format time by given date template. see ToLayout()

func DateFormat added in v0.5.1

func DateFormat(t time.Time, template string) string

DateFormat format time by given date template. see ToLayout()

func DayEnd added in v0.5.1

func DayEnd(t time.Time) time.Time

DayEnd time for given time

func DayStart added in v0.5.1

func DayStart(t time.Time) time.Time

DayStart time for given time

func Format

func Format(t time.Time) string

Format use default layout

func FormatBy

func FormatBy(t time.Time, layout string) string

FormatBy given default layout

func FormatByTpl added in v0.5.1

func FormatByTpl(t time.Time, template string) string

FormatByTpl format time by given date template. see ToLayout()

func FormatUnix

func FormatUnix(sec int64) string

FormatUnix time seconds use default layout

func FormatUnixBy

func FormatUnixBy(sec int64, layout string) string

FormatUnixBy format time seconds use given layout

func FormatUnixByTpl added in v0.5.1

func FormatUnixByTpl(sec int64, template string) string

FormatUnixByTpl format time seconds use given date template. see ToLayout()

func HourEnd added in v0.5.2

func HourEnd(t time.Time) time.Time

HourEnd time for given time

func HourStart added in v0.5.1

func HourStart(t time.Time) time.Time

HourStart time for given time

func HowLongAgo added in v0.5.1

func HowLongAgo(sec int64) string

HowLongAgo format given timestamp to string.

func NowAddDay

func NowAddDay(day int) time.Time

NowAddDay add some day time from now

func NowAddHour

func NowAddHour(hour int) time.Time

NowAddHour add some hour time from now

func NowAddMinutes

func NowAddMinutes(minutes int) time.Time

NowAddMinutes add some minutes time from now

func NowAddSeconds

func NowAddSeconds(seconds int) time.Time

NowAddSeconds add some seconds time from now

func NowHourEnd added in v0.5.5

func NowHourEnd() time.Time

NowHourEnd time

func NowHourStart added in v0.5.1

func NowHourStart() time.Time

NowHourStart time

func NowUnix

func NowUnix() int64

NowUnix is short of time.Now().Unix()

func SetLocalByName

func SetLocalByName(tzName string) error

SetLocalByName set local by tz name. eg: UTC, PRC

func ToDuration added in v0.5.6

func ToDuration(s string) (time.Duration, error)

ToDuration parses a duration string. such as "300ms", "-1.5h" or "2h45m". Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".

func ToLayout added in v0.5.1

func ToLayout(template string) string

ToLayout convert chars date template to Go date layout.

Chars see charMap

func TodayEnd added in v0.5.1

func TodayEnd() time.Time

TodayEnd time

func TodayStart added in v0.5.1

func TodayStart() time.Time

TodayStart time

Types

type TimeX

type TimeX struct {
	time.Time
	// Layout set the default date format layout. default use DefaultLayout
	Layout string
}

TimeX struct

func FromDate added in v0.5.2

func FromDate(s string, template ...string) (*TimeX, error)

FromDate create from datetime string.

func FromString added in v0.5.1

func FromString(s string, layouts ...string) (*TimeX, error)

FromString create from datetime string. see strutil.ToTime()

func FromTime added in v0.5.2

func FromTime(t time.Time) *TimeX

FromTime new instance form given time.Time. alias of the New()

func FromUnix added in v0.5.1

func FromUnix(sec int64) *TimeX

FromUnix create from unix time

func Local

func Local() *TimeX

Local time for now

func LocalByName

func LocalByName(tzName string) *TimeX

LocalByName time for now

func New added in v0.5.1

func New(t time.Time) *TimeX

New instance form given time

func Now

func Now() *TimeX

Now time instance

func Wrap added in v0.5.2

func Wrap(t time.Time) *TimeX

Wrap the go time instance. alias of the New()

func (*TimeX) AddDay

func (t *TimeX) AddDay(day int) *TimeX

AddDay add some day time for the time

func (*TimeX) AddHour

func (t *TimeX) AddHour(hours int) *TimeX

AddHour add some hour time

func (*TimeX) AddMinutes

func (t *TimeX) AddMinutes(minutes int) *TimeX

AddMinutes add some minutes time for the time

func (*TimeX) AddSeconds

func (t *TimeX) AddSeconds(seconds int) *TimeX

AddSeconds add some seconds time the time

func (*TimeX) CustomHMS added in v0.5.2

func (t *TimeX) CustomHMS(hour, min, sec int) *TimeX

CustomHMS custom change the hour, minute, second for create new time.

func (*TimeX) DateFormat added in v0.5.1

func (t *TimeX) DateFormat(template string) string

DateFormat use input template format time to date. see ToLayout()

func (*TimeX) Datetime

func (t *TimeX) Datetime() string

Datetime use DefaultLayout format time to date. see Format()

func (*TimeX) DayAfter added in v0.5.1

func (t *TimeX) DayAfter(day int) *TimeX

DayAfter get some day after time for the time. alias of TimeX.AddDay()

func (*TimeX) DayAgo added in v0.5.1

func (t *TimeX) DayAgo(day int) *TimeX

DayAgo get some day ago time for the time

func (*TimeX) DayEnd added in v0.5.1

func (t *TimeX) DayEnd() *TimeX

DayEnd get time at 23:59:59

func (*TimeX) DayStart added in v0.5.1

func (t *TimeX) DayStart() *TimeX

DayStart get time at 00:00:00

func (TimeX) Diff added in v0.5.1

func (t TimeX) Diff(u time.Time) time.Duration

Diff calc diff duration for t - u. alias of time.Time.Sub()

func (TimeX) DiffSec added in v0.5.1

func (t TimeX) DiffSec(u time.Time) int

DiffSec calc diff seconds for t - u

func (*TimeX) Format added in v0.5.1

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

Format returns a textual representation of the time value formatted according to the layout defined by the argument.

see time.Time.Format()

func (*TimeX) HourEnd added in v0.5.2

func (t *TimeX) HourEnd() *TimeX

HourEnd time

func (*TimeX) HourStart added in v0.5.1

func (t *TimeX) HourStart() *TimeX

HourStart time

func (TimeX) HowLongAgo added in v0.5.1

func (t TimeX) HowLongAgo(before time.Time) string

HowLongAgo format diff time to string.

func (*TimeX) IsAfter added in v0.5.1

func (t *TimeX) IsAfter(u time.Time) bool

IsAfter the given time

func (*TimeX) IsAfterUnix added in v0.5.2

func (t *TimeX) IsAfterUnix(ux int64) bool

IsAfterUnix the given unix timestamp

func (*TimeX) IsBefore added in v0.5.1

func (t *TimeX) IsBefore(u time.Time) bool

IsBefore the given time

func (*TimeX) IsBeforeUnix added in v0.5.2

func (t *TimeX) IsBeforeUnix(ux int64) bool

IsBeforeUnix the given unix timestamp

func (TimeX) SubUnix added in v0.5.1

func (t TimeX) SubUnix(u time.Time) int

SubUnix calc diff seconds for t - u

func (TimeX) T added in v0.5.2

func (t TimeX) T() time.Time

T returns the t.Time

func (TimeX) Timestamp added in v0.5.2

func (t TimeX) Timestamp() int64

Timestamp value. alias t.Unix()

func (*TimeX) Tomorrow added in v0.5.1

func (t *TimeX) Tomorrow() *TimeX

Tomorrow time. get tomorrow time for the time

func (*TimeX) TplFormat added in v0.5.1

func (t *TimeX) TplFormat(template string) string

TplFormat use input template format time to date.

func (*TimeX) Yesterday added in v0.5.1

func (t *TimeX) Yesterday() *TimeX

Yesterday get day ago time for the time

Jump to

Keyboard shortcuts

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