timeKit

package
v2.2.24 Latest Latest
Warning

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

Go to latest
Published: Sep 15, 2023 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
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

View Source
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"
View Source
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

func Between(t, min, max time.Time) bool

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 ClearInterval(ticker *time.Ticker)

func ClearTimeout

func ClearTimeout(timer *time.Timer)

func ConvertLocation added in v2.1.47

func ConvertLocation(t time.Time, loc *time.Location) time.Time

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

func CorrectSystemTime(rootPassword string) (t time.Time, err error)

CorrectSystemTime (根据网络时间)纠正系统时间

func Format added in v2.1.43

func Format[F ~string](t time.Time, format F) string

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

func FormatCurrent[F ~string](format F) string

FormatCurrent 格式化 当前时间 为 字符串.

e.g. ("2006-01-02T15:04:05.000") => "2023-08-17T16:05:14.985"

func FormatDuration added in v2.1.43

func FormatDuration(d time.Duration) string

FormatDuration time.Duration => string

e.g.

d := time.Minute*63 + time.Second*15
fmt.Println(timeKit.FormatDuration(d)) // 1h3m15s

func GetNetworkTime

func GetNetworkTime() (time.Time, string, error)

GetNetworkTime 获取网络时间.

PS: 获取不到的话,返回机器时间.

func GetSystemTime

func GetSystemTime() time.Time

GetSystemTime 获取系统时间(机器时间;本地时间;time.Local).

func Parse added in v2.1.42

func Parse[F ~string](format F, timeStr string) (time.Time, error)

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 ParseInLocation added in v2.1.42

func ParseInLocation[F ~string](format F, timeStr string, loc *time.Location) (time.Time, error)

ParseInLocation

@param loc time.Local || time.UTC

func SetInterval

func SetInterval(fun func(t time.Time), duration time.Duration) *time.Ticker

SetInterval

参考: golang定时器函数 每隔几分钟执行一个函数 https://www.cnblogs.com/niuben/p/14368715.html

func SetSystemTime

func SetSystemTime(t time.Time, rootPassword string) error

SetSystemTime 设置系统时间(机器时间)

PS: (1) 通过date命令设置root权限,需要root权限. (2) 只能精确到"秒".

@param password root用户的密码

func SetTimeout

func SetTimeout(fun func(), duration time.Duration) *time.Timer

SetTimeout

参考: golang定时器函数 每隔几分钟执行一个函数

https://www.cnblogs.com/niuben/p/14368715.html

GO语言提前取消定时器

https://blog.csdn.net/u012265809/article/details/114939168

func ToRelativeString

func ToRelativeString(t time.Time) string

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"

func ToZeroAM added in v2.1.47

func ToZeroAM(t time.Time) time.Time

ToZeroAM

@return 传参t对应的当天凌晨零点

e.g.

now := time.Now()
fmt.Println(now) 	// 2023-08-18 15:24:03.167655 +0800 CST m=+0.004041126
t := timeKit.ToZeroAM(now)
fmt.Println(t) 		// 2023-08-18 00:00:00 +0800 CST

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)

Jump to

Keyboard shortcuts

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