xtime

package
v0.0.0-...-f89cf20 Latest Latest
Warning

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

Go to latest
Published: Jan 8, 2020 License: MIT Imports: 7 Imported by: 0

README

XTime

time 工具集

parse 支持多种类型的parse

go 默认的time.Parse需要指定对应的layout,导致使用起来不够便利,因此催生了xtime.ParseAny

package main

import (
    "time"

    "github.com/leaxoy/x-go/xtime"
)

func main() {
    // 标准库
    t, err := time.Parse(time.RFC3339, "2006-01-02T15:04:05Z07:00")
    println(t, err)
    // vs xtime
    t, err = xtime.ParseAny("2018/11/12")
    println(t, err)
}

duration 时间度量

package main

import (
    "github.com/leaxoy/x-go/xtime"
)

func main() {
    seconds := xtime.MeasureDuration(func () {
        // do lots of work
    })
    println(seconds)
}

relative time 相对时间计算

package main

import (
    "time"

    "github.com/leaxoy/x-go/xtime"
)

func main()  {
    xtime.StartOfDay(time.Now()) // 给定时间对应日期的开始时间
    xtime.EndOfDay(time.Now()) // 给定时间对应日期的结束时间
    xtime.StartOfToday() // 今天的开始时间
    xtime.EndOfToday() // 今天的结束时间
    xtime.UTC() // 当前UTC时间
    xtime.Local() // 当前的本地时间
    xtime.Yesterday() // 昨天
    xtime.Tomorrow() // 明天
    xtime.NowMillis() // 当前时间的毫秒单位
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrAmbiguousMMDD for date formats such as 04/02/2014 the mm/dd vs dd/mm are
	// ambiguous, so it is an error for strict parse rules.
	ErrAmbiguousMMDD = fmt.Errorf("err: this date has ambiguous mm/dd vs dd/mm type format")
)

Functions

func EndOfDay

func EndOfDay(t time.Time) time.Time

func EndOfToday

func EndOfToday() time.Time

func Interval

func Interval(ctx context.Context, duration time.Duration) (chan struct{}, context.CancelFunc)

Interval return a chan chan emit event every duration and a cancel function to cancel interval.

func Local

func Local() time.Time

Local return local time

func MeasureDuration

func MeasureDuration(fn func()) time.Duration

MeasureDuration will measure duration of an operation.

func MustParse

func MustParse(datestr string) time.Time

MustParse parse a date, and panic if it can't be parsed. Used for testing. Not recommended for most use-cases.

func NowMillis

func NowMillis() int64

NowMillis return now time in milliseconds

func ParseAny

func ParseAny(datestr string) (time.Time, error)

ParseAny parse an unknown date format, detect the layout. Normal parse. Equivalent Timezone rules as time.Parse(). NOTE: please see readme on mmdd vs ddmm ambiguous dates.

func ParseFormat

func ParseFormat(datestr string) (string, error)

ParseFormat parse's an unknown date-time string and returns a layout string that can parse this (and exact same format) other date-time strings.

layout, err := dateparse.ParseFormat("2013-02-01 00:00:00")
// layout = "2006-01-02 15:04:05"

func ParseIn

func ParseIn(datestr string, loc *time.Location) (time.Time, error)

ParseIn with Location, equivalent to time.ParseInLocation() timezone/offset rules. Using location arg, if timezone/offset info exists in the datestring, it uses the given location rules for any zone interpretation. That is, MST means one thing when using America/Denver and something else in other locations.

func ParseLocal

func ParseLocal(datestr string) (time.Time, error)

ParseLocal Given an unknown date format, detect the layout, using time.Local, parse.

Set Location to time.Local. Same as ParseIn Location but lazily uses the global time.Local variable for Location argument.

denverLoc, _ := time.LoadLocation("America/Denver")
time.Local = denverLoc

t, err := dateparse.ParseLocal("3/1/2014")

Equivalent to:

t, err := dateparse.ParseIn("3/1/2014", denverLoc)

func ParseStrict

func ParseStrict(datestr string) (time.Time, error)

ParseStrict parse an unknown date format. IF the date is ambigous mm/dd vs dd/mm then return an error. These return errors: 3.3.2014 , 8/8/71 etc

func RunInterval

func RunInterval(ctx context.Context, duration time.Duration, fn func()) context.CancelFunc

RunInterval run user-defined function every duration until cancel.

func StartOfDay

func StartOfDay(t time.Time) time.Time

func StartOfToday

func StartOfToday() time.Time

func Tomorrow

func Tomorrow() time.Time

func UTC

func UTC() time.Time

UTC return utc time

func Yesterday

func Yesterday() time.Time

Types

This section is empty.

Jump to

Keyboard shortcuts

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