Documentation
¶
Overview ¶
Package time provides some useful functions about time.
Index ¶
- Constants
- Variables
- func DateTimeToTs(dt string) int64
- func FromMs(ms int64, loc *time.Location) time.Time
- func FromNs(ns int64, loc *time.Location) time.Time
- func FromSec(sec int64, loc *time.Location) time.Time
- func FromString(layout, value string, loc *time.Location) (time.Time, error)
- func FromUs(us int64, loc *time.Location) time.Time
- func GetBeijingTime(layout, value string) (t time.Time, err error)
- func GetDayBeginMoment(t time.Time) time.Time
- func GetDayBeginMomentTs(ts int64) time.Time
- func GetDayElapsedMs(t time.Time) int64
- func GetDayElapsedNs(t time.Time) int64
- func GetDayElapsedS(t time.Time) int64
- func GetDayElapsedUs(t time.Time) int64
- func GetDayEndMoment(t time.Time) time.Time
- func GetDayEndMomentTs(uts int64) time.Time
- func GetDayMomentNanoTs(t time.Time, timezone string, hour, minute, second, nsec int) (int64, error)
- func GetDayMomentTime(t time.Time, timezone string, hour, minute, second, nsec int) (time.Time, error)
- func GetDaysBtwTs(ts0, ts1 int64) int64
- func GetFriDate(t time.Time) string
- func GetHoursBtwTs(ts0, ts1 int64) int64
- func GetLocByUtcOffset(offset time.Duration) *time.Location
- func GetMinutesBtwTs(ts0, ts1 int64) int64
- func GetMonDate(t time.Time) string
- func GetNowDate() string
- func GetNowDateTime() string
- func GetNowDateTimeZ() string
- func GetNowMs() int64
- func GetNowNs() int64
- func GetNowS() int64
- func GetNowTime() string
- func GetNowUs() int64
- func GetSatDate(t time.Time) string
- func GetSunDate(t time.Time) string
- func GetThursDate(t time.Time) string
- func GetTimezoneTime(timezone string, layout, value string) (t time.Time, err error)
- func GetTuesDate(t time.Time) string
- func GetWedDate(t time.Time) string
- func GetWeekday(t time.Time, w time.Weekday) time.Time
- func InCloseIntvl(target, left, right time.Time) bool
- func InLeftCloseIntvl(target, left, right time.Time) bool
- func InOpenIntvl(target, left, right time.Time) bool
- func InRightCloseIntvl(target, left, right time.Time) bool
- func IsLeapYear(year int) bool
- func IsSameDay(ts1, ts2 int64) bool
- func IsSameHour(ts1, ts2 int64) bool
- func IsSameMinute(ts1, ts2 int64) bool
- func IsSameMonth(ts1, ts2 int64) bool
- func IsSameWeek(ts1, ts2 int64) bool
- func IsSameYear(ts1, ts2 int64) bool
- func Ms(t time.Time) int64
- func NowCst() time.Time
- func NowEst() time.Time
- func NowUtc() time.Time
- func Ns(t time.Time) int64
- func Sec(t time.Time) int64
- func TimeCost() func() time.Duration
- func TodayMidnight() time.Time
- func TsToDateTime(ts int64) string
- func Us(t time.Time) int64
- func YmdToTime(day Ymd, loc *time.Location) (time.Time, error)
- type TimeCounter
- type Ymd
Constants ¶
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" )
const ( HoursPerDay = 24 DayDuration time.Duration = time.Hour * HoursPerDay SecondsPerHour = int64(time.Hour / time.Second) SecondsPerDay = int64(DayDuration / time.Second) )
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 ¶
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 ¶
DateTimeToTs converts datetime in YYYY-MM-DD hh:mm:ss to unix timestamp.
func FromMs ¶ added in v0.0.55
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
FromNs converts a nanosecond timestamp to time.Time in the specified location.
func FromSec ¶ added in v0.0.55
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
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
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
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 ¶
GetDayBeginMoment gets the starting moment of one day.
func GetDayBeginMomentTs ¶
GetDayBeginMomentTs gets the starting moment of one day specified by UNIX timestamp.
func GetDayElapsedMs ¶
GetDayElapsedMs gets the elapsed milliseconds since the starting moment of one day.
func GetDayElapsedNs ¶
GetDayElapsedNs gets the elapsed nanoseconds since the starting moment of one day.
func GetDayElapsedS ¶
GetDayElapsedS gets the elapsed seconds since the starting moment of one day.
func GetDayElapsedUs ¶
GetDayElapsedUs gets the elapsed microseconds since the starting moment of one day.
func GetDayEndMoment ¶
GetDayEndMoment gets the ending moment of one day.
func GetDayEndMomentTs ¶
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 ¶
GetDaysBtwTs gets the number of days between two timestamps and round down.
func GetFriDate ¶
GetFriDate gets friday date in format 2006-01-02.
func GetHoursBtwTs ¶
GetHoursBtwTs gets the number of hours between two timestamps and round down.
func GetLocByUtcOffset ¶ added in v0.0.55
GetLocByUtcOffset gets a time.Location based on the offset to UTC.
func GetMinutesBtwTs ¶
GetMinutesBtwTs gets the number of hours between two timestamps and round down.
func GetMonDate ¶
GetMonDate gets monday date in format 2006-01-02.
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 GetSatDate ¶
GetSatDate gets saturday date in format 2006-01-02.
func GetSunDate ¶
GetSunDate gets sunday date in format 2006-01-02.
func GetThursDate ¶
GetThursDate gets thursday date in format 2006-01-02.
func GetTimezoneTime ¶ added in v0.0.55
GetTimezoneTime gets time.Time based on specified timezone, layout and value string.
func GetTuesDate ¶
GetTuesDate gets tuesday date in format 2006-01-02.
func GetWedDate ¶
GetWedDate gets wednesday date in format 2006-01-02.
func GetWeekday ¶
GetWeekday gets the weekday time.
func InCloseIntvl ¶ added in v0.0.63
InCloseIntvl checks the target time is within closed interval [left, right].
func InLeftCloseIntvl ¶ added in v0.0.63
InLeftCloseIntvl checks the target time is within left-closed interval [left, right).
func InOpenIntvl ¶ added in v0.0.63
InOpenIntvl checks the target time is within open interval (left, right).
func InRightCloseIntvl ¶ added in v0.0.63
InRightCloseIntvl checks the target time is within right-closed interval (left, right].
func IsSameHour ¶
IsSameHour checks the unix timestamp whether is the same hour.
func IsSameMinute ¶
IsSameMinute checks the unix timestamp whether is the same minute.
func IsSameMonth ¶
IsSameMonth checks the unix timestamp whether is the same month.
func IsSameWeek ¶
IsSameWeek checks the unix timestamp whether is the same week.
func IsSameYear ¶
IsSameYear checks the unix timestamp whether is the same year.
func TodayMidnight ¶ added in v0.0.55
TodayMidnight returns the time of today's midnight (00:00:00).
func TsToDateTime ¶
TsToDateTime converts unix timestamp to local datetime in YYYY-MM-DD hh:mm:ss.
Types ¶
type TimeCounter ¶
TimeCounter is used to count time interval.
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.
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
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
Add returns a new Ymd value that is the result of adding the specified number of days to the current Ymd value.