Documentation ¶
Index ¶
- Constants
- func Between(t, min, max time.Time) bool
- func ClearInterval(ticker *time.Ticker)
- func ClearTimeout(timer *time.Timer)
- func ConvertByLocation(t time.Time, loc *time.Location) (time.Time, error)
- func ConvertToLocalLocation(t time.Time) time.Time
- func ConvertToUTC(t time.Time) time.Time
- func CorrectSystemTime(rootPassword string) (t time.Time, err error)
- func FormatCurrentTime(formats ...TimeFormat) string
- func FormatDurationToString(t time.Duration) string
- func FormatTimeToString(t time.Time, format TimeFormat) string
- func GetNetworkTime() (time.Time, string, error)
- func GetSystemTime() time.Time
- func LoadLocation(name string) (*time.Location, error)
- func NewCron(spec string, task func()) (*cron.Cron, cron.EntryID, error)
- func ParseDurationString(str string) (time.Duration, error)
- func ParseTimeString(layout, str string, args ...*time.Location) (time.Time, error)
- func SetInterval(fun func(t time.Time), duration time.Duration) *time.Ticker
- func SetSystemTime(t time.Time, rootPassword string) error
- func SetTimeout(fun func(), duration time.Duration) *time.Timer
- func ToRelativeString(t time.Time) string
- type TimeFormat
Constants ¶
const ( Day = 24 * time.Hour Week = 7 * Day )
Variables ¶
This section is empty.
Functions ¶
func Between ¶
Between 检查给定的时间是否处于某一时间区间内(左右都不包含!!!)
参考: 【收藏】开发常用的 10 个通用函数 https://mp.weixin.qq.com/s/tvy9L-pb_8WFWAmA9u-bMg
e.g. (cur, cur, cur.Add(time.Second)) => false (cur, cur.Add(-time.Second), cur.Add(time.Second)) => true
func ClearInterval ¶
func ClearTimeout ¶
func ConvertByLocation ¶
ConvertByLocation 时区转换
@param loc 目标时区(不能为nil!!!)
e.g. UTC+8 转 UTC+0 2022-05-05 14:33:40.562899 +0800 CST m=+0.001585418 => 2022-05-05 06:33:40.562899 +0000 UTC
func ConvertToLocalLocation ¶
ConvertToLocalLocation 转换为本地时区的时间.
func CorrectSystemTime ¶
CorrectSystemTime (根据网络时间)纠正系统时间
func FormatCurrentTime ¶
func FormatCurrentTime(formats ...TimeFormat) string
FormatCurrentTime 格式化当前时间为字符串
e.g. () => "2022-08-13 14:54:44.336" ("") => ""
func FormatDurationToString ¶
FormatDurationToString
PS: (1) %v 和 %s 都可以; (2) strKit.ToString 也支持.
e.g. (time.Minute*63 + time.Second*15) => "1h3m15s"
func FormatTimeToString ¶
func FormatTimeToString(t time.Time, format TimeFormat) string
FormatTimeToString 格式化时间为字符串
@param t 不用担心t为nil的情况,详见下面的说明 @param formats 不传的话用默认值;传多个(包括一个)的话用第一个
一个方法如果接受类型为time.Time的参数,那么不用考虑该参数为nil的情况,因为: (1)time.Time类型变量的零值不为nil; (2)调用时,该参数位置不能直接传参nil(IDEA报错:Cannot use 'nil' as the type time.Time); (3)time.Time类型变量不能被赋值为nil(IDEA报错:Cannot use 'nil' as the type time.Time)。
func LoadLocation ¶
LoadLocation
LoadLocation的输入参数的取值,除了该函数的源代码中可看到的”UTC”、”Local”,其余的值其实是遵照“IANA Time Zone”的规则,可以解压$GOROOT/lib/time/zoneinfo.zip 这个文件打开查看。 在Asia这个目录,我看到了Chongqing,Hong_Kong,但没Beijing。在国外获取中国北京时间,要用”PRC”,当然”Asia/Chongqing”也是个方法 参考:https://blog.csdn.net/qq_26981997/article/details/53454606
@param name e.g. "Asia/Chongqing"
func NewCron ¶
NewCron
@param spec "@every 10s" || "@every 1m",更多可参考"Golang - 1.docx"
@return 第1个返回值: 可以调用 Run() 或 Start() 以启动.(Run()会阻塞调用此方法的goroutine;Start()不会阻塞)
e.g. spec == "@every 10s"
在调用Start()或Run()的那个瞬间开始计时,每隔10s执行1次传参task.
func ParseDurationString ¶ added in v2.0.9
ParseDurationString string => time.Duration
@param str e.g."300ms"、"-1.5h"、"2h45m"
func ParseTimeString ¶ added in v2.0.9
ParseTimeString 类型转换: string => time.Time
@param layout 时间格式 @param str 要解析的时间字符串
func SetInterval ¶
SetInterval
参考: golang定时器函数 每隔几分钟执行一个函数 https://www.cnblogs.com/niuben/p/14368715.html
func SetSystemTime ¶
SetSystemTime 设置系统时间(机器时间)
PS: (1) 通过date命令设置root权限,需要root权限. (2) 只能精确到"秒".
@param password root用户的密码
func SetTimeout ¶
SetTimeout
参考: golang定时器函数 每隔几分钟执行一个函数
https://www.cnblogs.com/niuben/p/14368715.html
GO语言提前取消定时器
https://blog.csdn.net/u012265809/article/details/114939168
func ToRelativeString ¶ added in v2.0.9
ToRelativeString
e.g. timeKit.ToRelativeString(time.Now().Add(time.Hour * -16)) => "16 hours ago" timeKit.ToRelativeString(time.Now().Add(time.Hour * 16)) => "15 hours from now" timeKit.ToRelativeString(time.Now().Add(time.Hour * 24 * 21)) => "2 weeks from now" timeKit.ToRelativeString(time.Now().Add(time.Hour*24*21 + time.Second)) => "3 weeks from now"
Types ¶
type TimeFormat ¶
type TimeFormat string
const ( // FormatDefault 参考"format.go" FormatDefault TimeFormat = "2006-01-02 15:04:05.999999999 -0700 MST" // FormatFileName 用于作为文件名(或目录名)的一部分 FormatFileName TimeFormat = "2006-01-02T15.04.05.000" // FormatCommon 常规的格式 FormatCommon TimeFormat = "2006-01-02T15:04:05.000" // FormatCommon1 常规的格式1 FormatCommon1 TimeFormat = "2006-01-02 15:04:05.000" // FormatEntire 完整的格式 FormatEntire TimeFormat = "2006-01-02 15:04:05.000Z07:00" FormatEntire1 TimeFormat = "2006-01-02T15:04:05.000Z07:00" FormatA TimeFormat = "2006-01-02 15:04:05" FormatB TimeFormat = "2006-01-02 3:04:05.000 PM Mon Jan" FormatC TimeFormat = "3:04:05.000 PM Mon Jan" // FormatNetwork 网络的格式 FormatNetwork TimeFormat = "Mon, 02 Jan 2006 15:04:05 MST" )
UTC: 世界协调时间(Coordinated Universal Time) GMT: 格林威治时间(UTC+0) CST: 中国标准时间(UTC+8) MST: 北美山区标准时间(UTC-7)