Documentation ¶
Index ¶
- Constants
- Variables
- func Between(t, min, max time.Time) bool
- func ClearInterval(ticker *time.Ticker)
- func ClearTimeout(timer *time.Timer)
- func ConvertLocation(t time.Time, loc *time.Location) time.Time
- func CorrectSystemTime(rootPassword string) (t time.Time, err error)
- func Format[F ~string](t time.Time, format F) string
- func FormatCurrent[F ~string](format F) string
- func FormatDuration(d time.Duration) string
- func GetNetworkTime() (time.Time, string, error)
- func GetSystemTime() time.Time
- func Parse[F ~string](format F, timeStr string) (time.Time, error)
- func ParseInLocation[F ~string](format F, timeStr string, loc *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
- func ToZeroAM(t time.Time) time.Time
- type TimeFormat
Constants ¶
const ( Nanosecond time.Duration = time.Nanosecond Microsecond = time.Microsecond Millisecond = time.Millisecond Second = time.Second Minute = time.Minute Hour = time.Hour Day = 24 * time.Hour Week = 7 * Day HalfHour = time.Minute * 30 )
Variables ¶
var LoadLocation func(name string) (*time.Location, error) = time.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 (a) 可以为""(将返回 time.UTC)
(b) e.g. "Asia/Chongqing"
var ParseDuration func(str string) (time.Duration, error) = time.ParseDuration
ParseDuration string => time.Duration
@param str (1) 如果为 "",将返回error
(2) e.g. "300ms"、"-1.5h"、"2h45m"
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 ConvertLocation ¶ added in v2.1.47
ConvertLocation 时区转换.
PS: 返回值是个副本,不会修改传参t.
@param loc 目标时区
e.g. UTC+8(CST) 转 UTC+0
2022-05-05 14:33:40.562899 +0800 CST m=+0.001585418 => 2022-05-05 06:33:40.562899 +0000 UTC
func CorrectSystemTime ¶
CorrectSystemTime (根据网络时间)纠正系统时间
func Format ¶ added in v2.1.43
Format time.Time => string
@param t 不用担心t为nil的情况,详见下面的说明 @param formats 不传的话用默认值;传多个(包括一个)的话用第一个
一个方法如果接受类型为time.Time的参数,那么不用考虑该参数为nil的情况,因为: (1)time.Time类型变量的零值不为nil; (2)调用时,该参数位置不能直接传参nil(IDE报错:Cannot use 'nil' as the type time.Time); (3)time.Time类型变量不能被赋值为nil(IDE报错:Cannot use 'nil' as the type time.Time).
e.g.
str := timeKit.Format(time.Now(), timeKit.FormatCommon) fmt.Println(str) // 2023-08-14T17:10:17.057
func FormatCurrent ¶ added in v2.1.43
FormatCurrent 格式化 当前时间 为 字符串.
e.g. ("2006-01-02T15:04:05.000") => "2023-08-17T16:05:14.985"
func FormatDuration ¶ added in v2.1.43
FormatDuration time.Duration => string
e.g.
d := time.Minute*63 + time.Second*15 fmt.Println(timeKit.FormatDuration(d)) // 1h3m15s
func Parse ¶ added in v2.1.42
Parse 类型转换: string => time.Time
PS: (1) 为什么不直接使用 time.Parse()?
因为time.Parse使用 time.UTC 作为loc,会有时差.
(2) 本函数使用 time.Local 作为loc.
@param layout 时间格式 @param str 要解析的时间字符串
e.g. (timeKit.FormatDate, "2016-08-08")
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 ¶
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 ( FormatDate TimeFormat = "2006-01-02" // FormatDefault 参考 time/format.go FormatDefault TimeFormat = "2006-01-02 15:04:05.999999999 -0700 MST" // FormatFileName 用于作为文件名(或目录名)的一部分 /* PS: (1) 不能使用 "2006-01-02T15-04-05-000". (2) Windows OS,文件名不支持: \ / : * ? " < > | */ 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 MST" FormatEntire1 TimeFormat = "2006-01-02T15:04:05.000Z07:00 MST" FormatEntire2 TimeFormat = "2006-01-02T15:04:05.000Z07:00" // FormatNetwork 网络的格式 FormatNetwork TimeFormat = "Mon, 02 Jan 2006 15:04:05 MST" 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" )
UTC: 世界协调时间(Coordinated Universal Time) GMT: 格林威治时间(UTC+0) CST: 中国标准时间(UTC+8) MST: 北美山区标准时间(UTC-7)