time

package
v0.0.63 Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2025 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package time provides some useful functions about time.

Index

Constants

View Source
const (
	YearOnly         = "2006"
	YearMonth        = "200601"
	DayOnly          = "20060102"
	DayHour          = "2006010215"
	DayHourMinute    = "200601021504"
	DayHourMinuteSec = "20060102150405"
	HourOnly         = "15"
	HourMinute       = "1504"
	HourMinuteSec    = "150405"
	DateTimeMilli    = "2006-01-02 15:04:05.000"
	DateTimeMicro    = "2006-01-02 15:04:05.000000"
	DateTimeNano     = "2006-01-02 15:04:05.000000000"
	DateTimeZone     = "2006-01-02 15:04:05 Z07:00"
)
View Source
const (
	HoursPerDay                  = 24
	DayDuration    time.Duration = time.Hour * HoursPerDay
	SecondsPerHour               = int64(time.Hour / time.Second)
	SecondsPerDay                = int64(DayDuration / time.Second)
)
View Source
const (
	AsiaShanghai      = "Asia/Shanghai"       // China Standard Time (CST)
	AmericaNewYork    = "America/New_York"    // Eastern Standard Time (EST/EDT)
	AmericaLosAngeles = "America/Los_Angeles" // Pacific Standard Time (PST/PDT)
	EuropeLondon      = "Europe/London"       // UK Time (GMT/BST)
	EuropeBerlin      = "Europe/Berlin"       // Central European Time (CET/CEST)
	AsiaTokyo         = "Asia/Tokyo"          // Japan Standard Time (JST)
	AustraliaSydney   = "Australia/Sydney"    // Australian Eastern Standard Time (AEST/AEDT)
	AsiaHongKong      = "Asia/Hong_Kong"      // Hong Kong Time (HKT)
	AsiaSingapore     = "Asia/Singapore"      // Singapore Time (SGT)
	PacificAuckland   = "Pacific/Auckland"    // New Zealand Standard Time (NZST/NZDT)
	EuropeParis       = "Europe/Paris"        // Central European Time (CET/CEST)
	AsiaSeoul         = "Asia/Seoul"          // Korea Standard Time (KST)
)

Common timezone strings defined as constants. Those timezone standard for a location name corresponding to a file in the IANA(Internet Assigned Numbers Authority) Time Zone database.

Variables

View Source
var (
	LocAsiaShanghai      = loadLocation(AsiaShanghai)
	LocAmericaNewYork    = loadLocation(AmericaNewYork)
	LocAmericaLosAngeles = loadLocation(AmericaLosAngeles)
	LocEuropeLondon      = loadLocation(EuropeLondon)
	LocEuropeBerlin      = loadLocation(EuropeBerlin)
	LocAsiaTokyo         = loadLocation(AsiaTokyo)
	LocAustraliaSydney   = loadLocation(AustraliaSydney)
	LocAsiaHongKong      = loadLocation(AsiaHongKong)
	LocAsiaSingapore     = loadLocation(AsiaSingapore)
	LocPacificAuckland   = loadLocation(PacificAuckland)
	LocEuropeParis       = loadLocation(EuropeParis)
	LocAsiaSeoul         = loadLocation(AsiaSeoul)
)

Location constants.

Functions

func DateTimeToTs

func DateTimeToTs(dt string) int64

DateTimeToTs converts datetime in YYYY-MM-DD hh:mm:ss to unix timestamp.

func FromMs added in v0.0.55

func FromMs(ms int64, loc *time.Location) time.Time

FromMs returns the given zone Time corresponding to the "Unix millisecond" Local zone will be used if loc is nil

func FromNs added in v0.0.55

func FromNs(ns int64, loc *time.Location) time.Time

FromNs converts a nanosecond timestamp to time.Time in the specified location.

func FromSec added in v0.0.55

func FromSec(sec int64, loc *time.Location) time.Time

FromSec returns the given zone Time corresponding to the "Unix second". Local zone will be used if loc is nil.

func FromString added in v0.0.55

func FromString(layout, value string, loc *time.Location) (time.Time, error)

FromString parses a formatted string and returns the time value it represents. Local zone will be used if loc is nil

func FromUs added in v0.0.55

func FromUs(us int64, loc *time.Location) time.Time

FromUs returns the given zone Time corresponding to the "Unix microsecond" Local zone will be used if loc is nil

func GetBeijingTime added in v0.0.52

func GetBeijingTime(layout, value string) (t time.Time, err error)

GetBeijingTime gets Beijing Time from time layout and value string. The location name Asia/Shanghai from IANA Time Zone Database standards for Beijing Time.

func GetDayBeginMoment

func GetDayBeginMoment(t time.Time) time.Time

GetDayBeginMoment gets the starting moment of one day.

func GetDayBeginMomentTs

func GetDayBeginMomentTs(ts int64) time.Time

GetDayBeginMomentTs gets the starting moment of one day specified by UNIX timestamp.

func GetDayElapsedMs

func GetDayElapsedMs(t time.Time) int64

GetDayElapsedMs gets the elapsed milliseconds since the starting moment of one day.

func GetDayElapsedNs

func GetDayElapsedNs(t time.Time) int64

GetDayElapsedNs gets the elapsed nanoseconds since the starting moment of one day.

func GetDayElapsedS

func GetDayElapsedS(t time.Time) int64

GetDayElapsedS gets the elapsed seconds since the starting moment of one day.

func GetDayElapsedUs

func GetDayElapsedUs(t time.Time) int64

GetDayElapsedUs gets the elapsed microseconds since the starting moment of one day.

func GetDayEndMoment

func GetDayEndMoment(t time.Time) time.Time

GetDayEndMoment gets the ending moment of one day.

func GetDayEndMomentTs

func GetDayEndMomentTs(uts int64) time.Time

GetDayEndMomentTs gets the ending moment of one day specified by UNIX timestamp.

func GetDayMomentNanoTs added in v0.0.55

func GetDayMomentNanoTs(t time.Time, timezone string, hour, minute, second, nsec int) (int64, error)

GetDayMomentNanoTs gets the nanosecond timestamp based on specified day and moment. If you want get more info about timezone, please refer to [IANA Time Zone Database](https://www.iana.org/time-zones). Common timezone are: Asia/Shanghai, America/New_York, Europe/London etc.

func GetDayMomentTime added in v0.0.55

func GetDayMomentTime(t time.Time, timezone string, hour, minute, second, nsec int) (time.Time, error)

GetDayMomentTime gets the time.Time based on specified day and moment.

func GetDaysBtwTs

func GetDaysBtwTs(ts0, ts1 int64) int64

GetDaysBtwTs gets the number of days between two timestamps and round down.

func GetFriDate

func GetFriDate(t time.Time) string

GetFriDate gets friday date in format 2006-01-02.

func GetHoursBtwTs

func GetHoursBtwTs(ts0, ts1 int64) int64

GetHoursBtwTs gets the number of hours between two timestamps and round down.

func GetLocByUtcOffset added in v0.0.55

func GetLocByUtcOffset(offset time.Duration) *time.Location

GetLocByUtcOffset gets a time.Location based on the offset to UTC.

func GetMinutesBtwTs

func GetMinutesBtwTs(ts0, ts1 int64) int64

GetMinutesBtwTs gets the number of hours between two timestamps and round down.

func GetMonDate

func GetMonDate(t time.Time) string

GetMonDate gets monday date in format 2006-01-02.

func GetNowDate

func GetNowDate() string

GetNowDate gets now date in YYYY-MM-DD.

func GetNowDateTime

func GetNowDateTime() string

GetNowDateTime gets now datetime in YYYY-MM-DD hh:mm:ss.

func GetNowDateTimeZ

func GetNowDateTimeZ() string

GetNowDateTimeZ gets now datetime with zone in YYYY-MM-DD hh:mm:ss Zone e.g. 2020-05-11 23:18:07 +08:00.

func GetNowMs

func GetNowMs() int64

GetNowMs gets unix timestamp in millisecond.

func GetNowNs

func GetNowNs() int64

GetNowNs gets unix timestamp in nanosecond.

func GetNowS

func GetNowS() int64

GetNowS gets unix timestamp in second.

func GetNowTime

func GetNowTime() string

GetNowDate gets now time in hh:mm:ss.

func GetNowUs

func GetNowUs() int64

GetNowUs gets unix timestamp in microsecond.

func GetSatDate

func GetSatDate(t time.Time) string

GetSatDate gets saturday date in format 2006-01-02.

func GetSunDate

func GetSunDate(t time.Time) string

GetSunDate gets sunday date in format 2006-01-02.

func GetThursDate

func GetThursDate(t time.Time) string

GetThursDate gets thursday date in format 2006-01-02.

func GetTimezoneTime added in v0.0.55

func GetTimezoneTime(timezone string, layout, value string) (t time.Time, err error)

GetTimezoneTime gets time.Time based on specified timezone, layout and value string.

func GetTuesDate

func GetTuesDate(t time.Time) string

GetTuesDate gets tuesday date in format 2006-01-02.

func GetWedDate

func GetWedDate(t time.Time) string

GetWedDate gets wednesday date in format 2006-01-02.

func GetWeekday

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

GetWeekday gets the weekday time.

func InCloseIntvl added in v0.0.63

func InCloseIntvl(target, left, right time.Time) bool

InCloseIntvl checks the target time is within closed interval [left, right].

func InLeftCloseIntvl added in v0.0.63

func InLeftCloseIntvl(target, left, right time.Time) bool

InLeftCloseIntvl checks the target time is within left-closed interval [left, right).

func InOpenIntvl added in v0.0.63

func InOpenIntvl(target, left, right time.Time) bool

InOpenIntvl checks the target time is within open interval (left, right).

func InRightCloseIntvl added in v0.0.63

func InRightCloseIntvl(target, left, right time.Time) bool

InRightCloseIntvl checks the target time is within right-closed interval (left, right].

func IsLeapYear

func IsLeapYear(year int) bool

IsLeapYear checks the year whether is leap year.

func IsSameDay

func IsSameDay(ts1, ts2 int64) bool

IsSameDay checks the unix timestamp whether is the same day.

func IsSameHour

func IsSameHour(ts1, ts2 int64) bool

IsSameHour checks the unix timestamp whether is the same hour.

func IsSameMinute

func IsSameMinute(ts1, ts2 int64) bool

IsSameMinute checks the unix timestamp whether is the same minute.

func IsSameMonth

func IsSameMonth(ts1, ts2 int64) bool

IsSameMonth checks the unix timestamp whether is the same month.

func IsSameWeek

func IsSameWeek(ts1, ts2 int64) bool

IsSameWeek checks the unix timestamp whether is the same week.

func IsSameYear

func IsSameYear(ts1, ts2 int64) bool

IsSameYear checks the unix timestamp whether is the same year.

func Ms added in v0.0.56

func Ms(t time.Time) int64

Ms return "unix millisecond" of the given time.

func NowCst added in v0.0.55

func NowCst() time.Time

NowCst returns now CST(China Standard Time) time.

func NowEst added in v0.0.55

func NowEst() time.Time

NowEst returns now EST(Eastern Standard Time) time.

func NowUtc added in v0.0.55

func NowUtc() time.Time

NowUtc returns now UTC time.

func Ns added in v0.0.56

func Ns(t time.Time) int64

Ns return "unix nanosecond" of the given time.

func Sec added in v0.0.56

func Sec(t time.Time) int64

Sec return "unix second" of the given time.

func TimeCost

func TimeCost() func() time.Duration

TimeCost counts time cost.

func TodayMidnight added in v0.0.55

func TodayMidnight() time.Time

TodayMidnight returns the time of today's midnight (00:00:00).

func TsToDateTime

func TsToDateTime(ts int64) string

TsToDateTime converts unix timestamp to local datetime in YYYY-MM-DD hh:mm:ss.

func Us added in v0.0.56

func Us(t time.Time) int64

Us return "unix microsecond" of the given time.

func YmdToTime added in v0.0.63

func YmdToTime(day Ymd, loc *time.Location) (time.Time, error)

YmdToTime converts a yyyymmdd integer like: 20080808 for the loc and returns the time value. Attention: the malform value will cause error, like 200808080, -20080808 etc. Local zone will be used if loc is nil.

Types

type TimeCounter

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

TimeCounter is used to count time interval.

func NewTimeCounter

func NewTimeCounter() (t *TimeCounter)

NewTimeCounter creates a time counter.

func (*TimeCounter) GetD

func (t *TimeCounter) GetD() time.Duration

GetD returns the time interval from the beginning to now in time.Duration.

func (*TimeCounter) GetMs

func (t *TimeCounter) GetMs() int64

GetMs returns the time interval from the beginning to now in millisecond.

func (*TimeCounter) GetNs

func (t *TimeCounter) GetNs() int64

GetNs returns the time interval from the beginning to now in nanosecond.

func (*TimeCounter) GetS

func (t *TimeCounter) GetS() int64

GetS returns the time interval from the beginning to now in second.

func (*TimeCounter) GetUs

func (t *TimeCounter) GetUs() int64

GetUs returns the time interval from the beginning to now in microsecond.

func (*TimeCounter) Reset

func (t *TimeCounter) Reset()

Reset sets start time to now.

type Ymd added in v0.0.55

type Ymd int

Ymd represents a date as an integer in the format YYYYMMDD. For example, June 15, 2025 would be represented as 20250615.

func TimeToYmd added in v0.0.63

func TimeToYmd(t time.Time) Ymd

TimeToYmd return the yyyymmdd integer, like "20080808", is very useful in finance. Must sure the year has four digit!

func (Ymd) Add added in v0.0.55

func (y Ymd) Add(days int) (Ymd, error)

Add returns a new Ymd value that is the result of adding the specified number of days to the current Ymd value.

func (Ymd) Next added in v0.0.55

func (y Ymd) Next() (Ymd, error)

Next returns the next yyyymmdd.

func (Ymd) Ok added in v0.0.55

func (y Ymd) Ok() bool

Ok checks the yyyymmdd integer is valid.

func (Ymd) Prev added in v0.0.55

func (y Ymd) Prev() (Ymd, error)

Prev return the prev yyyymmdd.

func (Ymd) String added in v0.0.55

func (y Ymd) String() string

String returns the ymd string format, like "20080808".

func (Ymd) Sub added in v0.0.55

func (y Ymd) Sub(u Ymd) (int, error)

Sub return the duration of natural days for y - u (like: 20080808 - 20080731 = 8)

func (Ymd) ToTime added in v0.0.63

func (y Ymd) ToTime(loc *time.Location) time.Time

ToTime converts the Ymd value to a time.Time in the specified location. It panics if the Ymd value is invalid or cannot be converted to a time.Time.

Jump to

Keyboard shortcuts

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