xxtime

package
v0.0.12 Latest Latest
Warning

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

Go to latest
Published: Mar 29, 2021 License: GPL-3.0 Imports: 2 Imported by: 1

Documentation

Index

Constants

View Source
const (
	Format_common_minus = "2006-01-02 15:04:05"
	Format_IS08601      = "2006-01-02 15:04:05.000" //yyyy-MM-dd HH:mm:ss.SSS
	Format_common_slash = "2006/01/02 15:04:05"
	Format_common_date  = "2006-01-02"
	Format_common_date2 = "2006/01/02"
	Format_common_time  = "15:04:05"

	// TimeFormat is the time format to use when generating times in HTTP
	// headers. It is like time.RFC1123 but hard-codes GMT as the time
	// zone. The time being formatted must be in UTC for Format to
	// generate the correct format.
	//
	// For parsing this time format, see ParseTime.
	// from golang/http/fs.go
	Format_standard_GMT = "Mon, 02 Jan 2006 15:04:05 GMT"
)

https://www.cnblogs.com/sandea/p/9696712.html 时间可分为时间点与时间段 ,提供了以下两种基础类型 - 时间点(Time) - 时间段(Duration)

除此之外 golang 也提供��了以下类型,做一些特定的业务 - 时区(Location) - Ticker - Timer(定时器)

View Source
const (
	// 时间常量,按照平时年历的计数方法,不考虑闰年闰月之类的
	Day   time.Duration = time.Hour * 24
	Week                = Day * 7
	Month               = Day * 30
	Year                = Day * 365
)

Variables

This section is empty.

Functions

func AtFunc added in v0.0.10

func AtFunc()

func CurrentUnixMillis added in v0.0.6

func CurrentUnixMillis() int64

返回当前系统时间戳,基于毫秒单位

func Format added in v0.0.6

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

func FormatCommon added in v0.0.9

func FormatCommon(t time.Time) string

func GetNow

func GetNow() string

func GetNow2TargetTime

func GetNow2TargetTime(targetTime time.Time) (time.Duration, error)

获取现在的时间 距离 目标时间之间 剩余时间段,

func GetNowDate added in v0.0.8

func GetNowDate() string

func GetNowTime added in v0.0.8

func GetNowTime() string

func GetTarget2NowTime

func GetTarget2NowTime(targetTime time.Time) (time.Duration, error)

获取过去时间 到 现在时间 的时间段

func GetTimeFormat

func GetTimeFormat(duration time.Duration) string

Truncate returns the result of rounding t down to a multiple of d (since the zero time). If d <= 0, Truncate returns t stripped of any monotonic clock reading but otherwise unchanged. 按照常规使用字符串 "yyyy-MM-dd HH:mm:ss" 来格式化时间.

func GetTimeRound

func GetTimeRound(duration time.Duration) (time.Time, error)

func GetTwoTimeSub

func GetTwoTimeSub(startime, endtime time.Time) (time.Duration, error)

返回两个时间段时间的 时间段

func HowCost added in v0.0.10

func HowCost(start time.Time, limit time.Duration, limitFunc CostCallFunc)

use example :

func test() {
	defer HowCost(time.Now(), time.Second, LogCostCallFunc)
	// do some work
}

func HowCostInTrack added in v0.0.10

func HowCostInTrack(start time.Time, limit time.Duration, track interface{}, limitFunc CostCallFunc)

func ParseLocal added in v0.0.10

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

func ParseMillSecond added in v0.0.9

func ParseMillSecond(millSecond int64) time.Duration

将毫秒单位的时间段转换为 以纳秒为单位的time.Duration类型对象

func ParseSecond added in v0.0.9

func ParseSecond(second int64) time.Duration

将秒单位的时间段转换为 time.Duration类型对象

func Time_UnixMillisecond added in v0.0.6

func Time_UnixMillisecond(millisecond int64) time.Time

毫秒单位的时间戳转换为time类型 1ms == 1000us (微秒) 1us == 1000ns (纳秒) 1ms == 1000us * 1000ns == 1*10^6(ns)

func Time_UnixSecond added in v0.0.6

func Time_UnixSecond(second int64) time.Time

秒单位的时间戳 转为 time类型

func UnixMicrosecond added in v0.0.6

func UnixMicrosecond(t time.Time) int64

返回unix时间戳,基于微秒单位

func UnixMillisecond added in v0.0.6

func UnixMillisecond(t time.Time) int64

返回unix时间戳,基于毫秒单位

func UnixSecond added in v0.0.6

func UnixSecond(t time.Time) int64

返回unix时间戳,基于秒单位

Types

type CostCallFunc added in v0.0.10

type CostCallFunc func(realCost time.Duration, track interface{})
var (
	EmptyCostCallFunc CostCallFunc = func(realCost time.Duration, track interface{}) {

	}
	LogCostCallFunc CostCallFunc = func(realCost time.Duration, track interface{}) {
		fmt.Println(realCost)
	}
)

type JsonTime added in v0.0.8

type JsonTime struct {
	time.Time
	// contains filtered or unexported fields
}

func NewJsonTime added in v0.0.8

func NewJsonTime(t time.Time) JsonTime

func NowJsonTime added in v0.0.8

func NowJsonTime() JsonTime

func (JsonTime) MarshalJSON added in v0.0.8

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

所以我们只需定义一个内嵌time.Time的结构体,并重写MarshalJSON方法,然后在定义model的时候把time.Time类型替换为我们自己的类型即可。但是在gorm中只重写MarshalJSON是不够的,只写这个方法会在写数据库的时候会提示delete_at字段不存在,需要加上database/sql的Value和Scan方法 https://github.com/jinzhu/gorm/issues/1611#issuecomment-329654638

func (JsonTime) String added in v0.0.8

func (jt JsonTime) String() string

func (*JsonTime) UnmarshalJSON added in v0.0.8

func (jt *JsonTime) UnmarshalJSON(data []byte) error

type TimeUnit added in v0.0.9

type TimeUnit time.Duration

Jump to

Keyboard shortcuts

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