timeUtil

package
v0.0.0-...-e50112c Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2024 License: BSD-3-Clause Imports: 14 Imported by: 0

README

时间操作

  • 增加了一个 JsonTime 类,在 json 序列化时默认按 “2006-01-02 15:04:05” 格式进行转换,而不是按 golang 默认方式转换。
  • 增加了对时间的各种常用处理函数。
  • 增加了一个可以随时调整间隔、随时启停的计时器。如果程序中有后台调度逻辑的计时间隔是基于可配置参数的,则可以灵活调整计时器间隔而不必重启进程。

Documentation

Index

Constants

View Source
const (
	Day  = time.Hour * 24
	Week = Day * 7

	DateTime     = "2006-01-02 15:04:05"
	DateTimeNano = "2006-01-02 15:04:05.999999999"
	DateOnly     = "2006-01-02"
)

Variables

View Source
var (
	DefaultFormat      = DateTime
	DefaultBeginOfWeek = time.Monday

	ZeroTime          = time.Unix(0, 0)
	Greenwich1970     = ZeroTime
	MinValidTimestamp = time.Unix(1, 0)                                // timestamp 的最小有效值
	MaxValidTimestamp = time.Unix(2147483647, 0)                       // timestamp 的最大有效值
	Local2000         = time.Date(2000, 1, 1, 0, 0, 0, 0, time.Local)  // 本地时区时间 2000-01-01 00:00:00.00000000
	EndOf2037         = time.Date(2038, 1, 1, 0, 0, 0, -1, time.Local) // 本地时区时间 2037-12-31 23:59:59.99999999
	Local2000Sec      = Local2000.Unix()                               // Unix of 本地时区时间 2000-01-01 00:00:00.00000000
	EndOf2037Sec      = EndOf2037.Unix()                               // Unix of 本地时区时间 2037-12-31 23:59:59.99999999
	Local2000Ms       = ToMs(Local2000)                                // Millisecond of 本地时区时间 2000-01-01 00:00:00.00000000
	EndOf2037Ms       = ToMs(EndOf2037)                                // Millisecond of 本地时区时间 2037-12-31 23:59:59.99999999
	Local2000Ns       = Local2000.UnixNano()                           // Nanosecond of 本地时区时间 2000-01-01 00:00:00.00000000
)

Functions

func BeginOfDay

func BeginOfDay(t time.Time) time.Time

获取一天开始的时间(00:00:00)

func BeginOfMonth

func BeginOfMonth(t time.Time) time.Time

func BeginOfWeek

func BeginOfWeek(t time.Time, beginAtMonday ...bool) time.Time

获取一周的开始时间(周日为一周第一天)

func BeginOfYear

func BeginOfYear(t time.Time) time.Time

func CurrentDateStr

func CurrentDateStr() string

func CurrentTimeStr

func CurrentTimeStr(format ...string) string

func DayOfWeek

func DayOfWeek(t time.Time, beginAtMonday ...bool) int

获取指定时间位于一周的第几天(从 1 开始)

func EndOfDay

func EndOfDay(t time.Time) time.Time

获取一天结束的时间(23:59:59.999999999)

func EndOfMonth

func EndOfMonth(t time.Time) time.Time

func EndOfWeek

func EndOfWeek(t time.Time, beginAtMonday ...bool) time.Time

获取一周的结束时间(周六为一周最后一天)

func EndOfYear

func EndOfYear(t time.Time) time.Time

func Format

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

func FormatDate

func FormatDate(t time.Time, format ...string) string

func FromMs

func FromMs(n int64) time.Time

将以毫秒为单位的时间戳整数转换为 time.Time

func FromMsFloat

func FromMsFloat(f float64) time.Time

将以毫秒为单位的时间戳整数转换为 time.Time

func FromNS

func FromNS(n int64) time.Time

func FromSecondFloat

func FromSecondFloat(f float64) time.Time

将以毫秒为单位的时间戳整数转换为 time.Time

func HourOfDay

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

获取一天指定小时数的时间(xx:00:00)

func IsValidTimestamp

func IsValidTimestamp(t time.Time) int

判断一个时间是否在 timestamp 范围内。 return:

 0: 在范围内
-1: 不在范围内,低于最小值
 1: 不在范围内,高于最大值

func LastWeekday

func LastWeekday(t time.Time, w time.Weekday) time.Time

func Max

func Max(a ...time.Time) time.Time

func Milliseconds

func Milliseconds(d time.Duration, decimals ...int) float64

func Min

func Min(a ...time.Time) time.Time

func NextWeekday

func NextWeekday(t time.Time, w time.Weekday) time.Time

func Parse

func Parse(tmStr string, now ...time.Time) (time.Time, error)

将字符串转换为 time.Time 类型。支持的格式如下:

数字:
   如果小于 MaxInt32,则以秒为单位转换; 否则以毫秒为单位转换。
预定义格式:
   now|today|yesterday|tomorrow: 当前时间、当天开始时间、昨天开始时间、明天开始时间
   {n}{unit}: 当前时间 增加 {n} 个 {unit} 后的结果。 n 可以为负数、 unit 支持 year|month|day(d)|hour(h)|minute(min)|second(sec)|millisecond(ms)|microsecond(us)|nanosecond(ns)
   以上格式可以通过英文逗号串联,比如 “tomorrow,2hour,-3min” 表示 “明天01:57”
[yyyy-]MM-dd 格式
[yyyy-]MM-dd HH:mm[:ss] 格式

func ParseNoErr

func ParseNoErr(str string) (t time.Time)

func Rand

func Rand(d time.Duration) time.Duration

func ToFloat

func ToFloat(t time.Time, decimals ...int) float64

将 time.Time 转换为以秒为单位的浮点数

func ToFloatStr

func ToFloatStr(t time.Time, decimals ...int) string

func ToMs

func ToMs(t time.Time) int64

将 time.Time 转换为以毫秒为单位的时间戳整数

func ToMsFloat

func ToMsFloat(t time.Time, decimals ...int) float64

将 time.Time 转换为以毫秒为单位的时间戳浮点数

func ToMsFloatStr

func ToMsFloatStr(t time.Time, decimals ...int) string

Types

type JsonTime

type JsonTime struct {
	time.Time
}

func ToJsonTime

func ToJsonTime(t time.Time) JsonTime

func (JsonTime) Format

func (this JsonTime) Format(format ...string) string

func (JsonTime) MarshalJSON

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

MarshalJSON on LocalTime format Time field with %Y-%m-%d %H:%M:%S

func (*JsonTime) Scan

func (this *JsonTime) Scan(v interface{}) (err error)

Scan valueof time.Time

func (JsonTime) String

func (this JsonTime) String() string

func (*JsonTime) UnmarshalJSON

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

func (JsonTime) Value

func (this JsonTime) Value() (driver.Value, error)

Value insert timestamp into mysql need this function.

type Ticker

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

func NewTicker

func NewTicker(interval, after time.Duration, f func(t time.Time)) *Ticker

func (*Ticker) GetDuration

func (this *Ticker) GetDuration() time.Duration

获取 Ticker 间隔

func (*Ticker) Restart

func (this *Ticker) Restart(after time.Duration) *Ticker

func (*Ticker) SetDuration

func (this *Ticker) SetDuration(d time.Duration)

设置 Ticker 间隔

func (*Ticker) Stop

func (this *Ticker) Stop(d time.Duration) bool

停止

参数:

d: 最长等待时间。

func (*Ticker) Trigger

func (this *Ticker) Trigger()

手动触发

Jump to

Keyboard shortcuts

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