gtime

package
v0.0.0-...-2bcc11b Latest Latest
Warning

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

Go to latest
Published: May 8, 2024 License: GPL-3.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Day     = 24 * time.Hour
	Week    = 7 * Day
	Month30 = 30 * Day
	Year365 = 365 * Day
)

Variables

View Source
var (
	EpochBeginTime           = EpochSecToTime(0)              // 1970-01-01 00:00:00 +0000 UTC
	EpochBeginDate           = Date(19700101)                 // 0001-01-01 00:00:00 +0000 UTC
	ZeroTime                 = time.Time{}                    // 0001-01-01 00:00:00 +0000 UTC
	ZeroDate                 = TimeToDate(ZeroTime, time.UTC) // 0001-01-01 00:00:00 +0000 UTC
	ZeroYearMonth  YearMonth = 0                              // 0000-00
	ZeroDateRange            = DateRange{Begin: ZeroDate, End: ZeroDate}
)
View Source
var (
	LayoutMM_DD_HH               = registerLayout("15:04:05")
	LayoutYYYY                   = registerLayout("2006")
	LayoutYYYY_MM                = registerLayout("2006-01")
	LayoutYYYY_MM_DD             = registerLayout("2006-01-02")
	LayoutYYYY_MM_DD_HH          = registerLayout("2006-01-02 15")
	LayoutYYYY_MM_DD_HH_mm       = registerLayout("2006-01-02 15:04")
	LayoutYYYY_MM_DD_HH_mm_SS    = registerLayout("2006-01-02 15:04:05")
	LayoutYYYY_MM_DD_HH_mm_SS_NS = registerLayout("2006-01-02 15:04:05.999999999")
	LayoutRFC3339GoExtension     = registerLayout("2006-01-02 15:04:05.999999999 -0700 MST") // Golang custom time layout in time.String(), it is very close to RFC3339.
	LayoutRFC3339Milli           = registerLayout("2006-01-02T15:04:05.999Z")                // RFC 3339 with milliseconds
	LayoutANSIC                  = registerLayout(time.ANSIC)                                // "Mon Jan _2 15:04:05 2006"
	LayoutUnixDate               = registerLayout(time.UnixDate)                             // "Mon Jan _2 15:04:05 MST 2006"
	LayoutRubyDate               = registerLayout(time.RubyDate)                             // "Mon Jan 02 15:04:05 -0700 2006"
	LayoutRFC822                 = registerLayout(time.RFC822)                               // "02 Jan 06 15:04 MST"
	LayoutRFC822Z                = registerLayout(time.RFC822Z)                              // "02 Jan 06 15:04 -0700" RFC822 with numeric zone
	LayoutRFC850                 = registerLayout(time.RFC850)                               // "Monday, 02-Jan-06 15:04:05 MST"
	LayoutRFC1123                = registerLayout(time.RFC1123)                              // "Mon, 02 Jan 2006 15:04:05 MST"
	LayoutRFC1123Z               = registerLayout(time.RFC1123Z)                             // "Mon, 02 Jan 2006 15:04:05 -0700" RFC1123 with numeric zone
	LayoutRFC3339                = registerLayout(time.RFC3339)                              // "2006-01-02T15:04:05Z07:00"
	LayoutRFC3339Nano            = registerLayout(time.RFC3339Nano)                          // "2006-01-02T15:04:05.999999999Z07:00"
	LayoutKitchen                = registerLayout(time.Kitchen)                              // "3:04PM"
	LayoutStamp                  = registerLayout(time.Stamp)                                // "Jan _2 15:04:05"
	LayoutStampMilli             = registerLayout(time.StampMilli)                           // "Jan _2 15:04:05.000"
	LayoutStampMicro             = registerLayout(time.StampMicro)                           // "Jan _2 15:04:05.000000"
	LayoutStampNano              = registerLayout(time.StampNano)                            // "Jan _2 15:04:05.000000000"
)
View Source
var (
	AllLayouts []string
)
View Source
var (
	TimeZoneAsiaShanghai *time.Location = nil
)

Functions

func AfterEqual

func AfterEqual(a, b time.Time) bool

func BeforeEqual

func BeforeEqual(a, b time.Time) bool

func CountDays

func CountDays(begin, end time.Time) int

get how many days across begin time and end time

func CountWorkDays

func CountWorkDays(begin, end time.Time) int

this is not accurate, holidays(christmas, thanks giving day, spring festival...) are marked as workday

func CountWorkDaysThisWeekFrom

func CountWorkDaysThisWeekFrom(from time.Weekday) int

*

    Count how many work days from a specified Weekday in this week

    Weekday-Index 	Weekday 	Work-Days-From	Work-Days-To
	0 				Sunday 		5				0
	1 				Monday 		5				1
	2 				Tuesday 	4				2
	3			 	Wednesday 	3				3
	4 				Thursday 	2				4
	5 				Friday 		1				5
	6 				Saturday 	0				5

'from' is included

func CountWorkDaysThisWeekTo

func CountWorkDaysThisWeekTo(to time.Weekday) int

'to' is included

func CurrentUnixMillis

func CurrentUnixMillis() uint32

returns current time in milliseconds

func DateValid

func DateValid(year, month, day int) bool

check if date is valid invalid date example: 2018-2-30

func DaysBetween

func DaysBetween(since, to time.Time) (exactDays float64, biggerDays, smallerDays int)

计算两个时间直接的间隔天数,不足一天的算一天称为biggerDays,不足一天不计算称为smallerDays

func DetectBestLayout

func DetectBestLayout(in []ElegantTime) string

func DetectBestLayoutRaw

func DetectBestLayoutRaw(in []time.Time) string

func ElapsedTimeInDay

func ElapsedTimeInDay(tm time.Time) time.Duration

func EpochMillisToTime

func EpochMillisToTime(millis int64) time.Time

func EpochNsecToTime

func EpochNsecToTime(nsec int64) time.Time

func EpochSecToTime

func EpochSecToTime(sec int64) time.Time

func EqualUnixNano

func EqualUnixNano(a, b time.Time) bool

EqualUnixNano returns `a`.UnixNano() equals `b`.UnixNano() time.Time{}.Equal() will check if timezone pointer equals, but EqualUnixNano doesn't check.

func GetLocalTimezone

func GetLocalTimezone() (int, error)

func GetMonthDays

func GetMonthDays(year, month int) int

getMonthDays 获取月份对应的月份天数

func GetMonthDuration

func GetMonthDuration(year, month int) time.Duration

func GetNetTimeInLocalONLINE

func GetNetTimeInLocalONLINE() (time.Time, error)

Get network time in local machine timezone

func GetNetTimeInUTCONLINE

func GetNetTimeInUTCONLINE() (time.Time, error)

Get network time in UTC

func GetYearDuration

func GetYearDuration(year int) time.Duration

func HasDuplicated

func HasDuplicated(times []time.Time, accuracy time.Duration) (bool, error)

func IsEpochBeginning

func IsEpochBeginning(tm time.Time) bool

不可以用tm.Seconds() 和 tm.Nanoseconds()作为判断标准, 因为这两个都只是钟表刻度盘上的零头而已 1970-01-01 00:00:00 +0000 UTC

func IsLeapYear

func IsLeapYear(year int) bool

isLearYear 判断年份是否是润年

func IsWorkDay

func IsWorkDay(tm time.Time) bool

func IsZero

func IsZero(tm time.Time) bool

0001-01-01 00:00:00 +0000 UTC

func LeftTimeInDay

func LeftTimeInDay(tm time.Time) time.Duration

func MaxTime

func MaxTime(a time.Time, b ...time.Time) time.Time

func MicrosToMillis

func MicrosToMillis(micros int64) int64

func MillisToDuration

func MillisToDuration(millis int64) time.Duration

func MillisToMicros

func MillisToMicros(millis int64) int64

func MillisToNsec

func MillisToNsec(millis int64) int64

func MinTime

func MinTime(a time.Time, b ...time.Time) time.Time

func MulDuration

func MulDuration(size int64, unit time.Duration) time.Duration

Convert int64 type duration to duration type MulDuration(3, time.Second) -> 3 seconds duration

func NsecToDuration

func NsecToDuration(nsec int64) time.Duration

func NsecToMillis

func NsecToMillis(nsec int64) int64

func NsecToSec

func NsecToSec(nsec int64) int64

func ParseDatetimeStringFuzz

func ParseDatetimeStringFuzz(datetimeString string) (time.Time, error)

3rd choice package: github.com/olebedev/when Parse human readable date time string to machine-oriented time - unix timestamp

func ParseTimeStringStrict

func ParseTimeStringStrict(s string) (time.Time, error)

func ParseTimezoneCode

func ParseTimezoneCode(tz string) (offset int, err error)

func PrettyFormat

func PrettyFormat(d time.Duration) string

time.Duaration.String() = "354h22m3.24s" PrettyFormat() = "2 weeks 18 hours 22 minutes 3 seconds"

func RoundEarlier

func RoundEarlier(t time.Time, d time.Duration) time.Time

RoundEarlier("2017-01-07 09:35:00 +0000 UTC", 20 * time.Minute) -> "2017-01-07 09:20:00 +0000 UTC"

func RoundLater

func RoundLater(t time.Time, d time.Duration) time.Time

RoundLater("2017-01-07 09:35:00 +0000 UTC", 20 * time.Minute) -> "2017-01-07 09:40:00 +0000 UTC"

func SetLocalTimezone

func SetLocalTimezone(timezone int) error

func SetSystemTimeROOT

func SetSystemTimeROOT(t time.Time) error

func SortTimes

func SortTimes(in []time.Time) []time.Time

func StringHumanReadable

func StringHumanReadable(duration time.Duration) string

func StringTimeZone

func StringTimeZone(tm time.Time, tz time.Location) string

func Sub

func Sub(t time.Time, d time.Duration) time.Time

func SyncNetTimeONLINEROOT

func SyncNetTimeONLINEROOT() error

Get network time and update time for local machine This API must run as root/admin, 但不知道Windows下是不是也是如此 有时警告有时不警告,内容为,sudo: timestamp too far in the future

func ThisMonthString

func ThisMonthString() string

func TimeToEpochMillis

func TimeToEpochMillis(t time.Time) int64

func TimeToEpochNsec

func TimeToEpochNsec(t time.Time) int64

func TimeToEpochSec

func TimeToEpochSec(t time.Time) int64

func TimeToIntYYYYMMDDHHMM

func TimeToIntYYYYMMDDHHMM(t time.Time) int

func UnixNanoToTime

func UnixNanoToTime(un int64, loc *time.Location) time.Time

tm.UnixNano()的逆运算

func Uptime

func Uptime() time.Duration

func VerifySystemTimeWithNtpOL

func VerifySystemTimeWithNtpOL(intervalAllowed time.Duration) error

Types

type Clock

type Clock interface {
	Name() string
	Now() time.Time
	Sleep(d time.Duration)
	Set(tm time.Time) error
}

func NewClock

func NewClock(clockName string) (Clock, error)

type Date

type Date int32

func MaxDate

func MaxDate(a, b Date) Date

func MinDate

func MinDate(a, b Date) Date

func NewDate

func NewDate(year, month, day int) (Date, error)

func NewDatePanic

func NewDatePanic(year, month, day int) Date

func ParseDateString

func ParseDateString(s string, strict bool) (Date, error)

strict == true : only can parse ISO standard date: "YYYY-MM-DD" strict == false : fuzzy parse date: "YYYY-MM-DD" or "YYYY.M.D"

func TimeToDate

func TimeToDate(tm time.Time, loc *time.Location) Date

func Today

func Today(loc *time.Location) Date

func Yesterday

func Yesterday(loc *time.Location) Date

func (Date) AddDays

func (d Date) AddDays(days int) Date

func (Date) After

func (d Date) After(cmp Date) bool

func (Date) AfterEqual

func (d Date) AfterEqual(cmp Date) bool

func (Date) Before

func (d Date) Before(cmp Date) bool

func (Date) BeforeEqual

func (d Date) BeforeEqual(cmp Date) bool

func (Date) Day

func (d Date) Day() int

func (Date) Equal

func (d Date) Equal(cmp Date) bool

func (Date) IntYYYYMMDD

func (d Date) IntYYYYMMDD() int

func (Date) IsZero

func (d Date) IsZero() bool

func (Date) MarshalJSON

func (d Date) MarshalJSON() ([]byte, error)

func (Date) Month

func (d Date) Month() time.Month

func (Date) NextDay

func (d Date) NextDay() Date

func (Date) PreviousDay

func (d Date) PreviousDay() Date

func (Date) String

func (d Date) String() string

func (Date) StringYYYYMMDD

func (d Date) StringYYYYMMDD() string

yyyymmdd Notice: 如果你写成了fmt.Sprintf("%04d%02d%02d", d.Year, d.YearMonth, d.Day),编译也能通过 但是返回结果却是很大很大的数字,因为它们代表函数地址

func (Date) StringYYYY_MM_DD

func (d Date) StringYYYY_MM_DD() string

yyyy-mm-dd

func (Date) Sub

func (d Date) Sub(cmp Date) int

func (Date) ToTime

func (d Date) ToTime(hour, minute, sec, nsec int, loc *time.Location) time.Time

func (Date) ToTimeLocation

func (d Date) ToTimeLocation(loc *time.Location) time.Time

func (Date) ToTimeUTC

func (d Date) ToTimeUTC() time.Time

func (Date) UnixDays

func (d Date) UnixDays() int

days from unix epoch date

func (*Date) UnmarshalJSON

func (d *Date) UnmarshalJSON(b []byte) error

func (Date) Year

func (d Date) Year() int

type DateRange

type DateRange struct {
	Begin Date
	End   Date
}

func ParseDateRangeString

func ParseDateRangeString(s string, strict bool) (DateRange, error)

strict == true: standard date range supported only: "YYYY-MM-DD/YYYY-MM-DD" Date Range standard reference:http://www.ukoln.ac.uk/metadata/dcmi/date-dccd-odrf/ strict == false: fuzzy parse in web page parser, these formats supported: 2018-01-02 - 2018-01-03 2018-01-02 ~ 2018-01-03 2017.1.2-2017.1.7

func (DateRange) IsZero

func (dr DateRange) IsZero() bool

func (DateRange) MarshalJSON

func (dr DateRange) MarshalJSON() ([]byte, error)

func (DateRange) String

func (dr DateRange) String() string

func (*DateRange) UnmarshalJSON

func (dr *DateRange) UnmarshalJSON(b []byte) error

type ElegantTime

type ElegantTime struct {
	// contains filtered or unexported fields
}

ElegantTime is the time.Time with JSON marshal and unmarshal capability

func NewElegantTime

func NewElegantTime(tm time.Time, layout string) ElegantTime

func NewElegantTimeArray

func NewElegantTimeArray(tms []time.Time, layout string) []ElegantTime

func (*ElegantTime) DetectBestLayout

func (t *ElegantTime) DetectBestLayout() string

func (*ElegantTime) JSON

func (t *ElegantTime) JSON() ([]byte, error)

func (*ElegantTime) JSONAutoDetect

func (t *ElegantTime) JSONAutoDetect() ([]byte, error)

func (*ElegantTime) MarshalJSON

func (t *ElegantTime) MarshalJSON() ([]byte, error)

MarshalJSON will marshal using 2006-01-02T15:04:05+07:00 layout

func (*ElegantTime) Raw

func (t *ElegantTime) Raw() time.Time

func (*ElegantTime) SetLayout

func (t *ElegantTime) SetLayout(layout string)

func (*ElegantTime) UnmarshalJSON

func (t *ElegantTime) UnmarshalJSON(b []byte) error

UnmarshalJSON will unmarshal using 2006-01-02T15:04:05+07:00 layout

type HumanDuration

type HumanDuration time.Duration

func ParseHumanDuration

func ParseHumanDuration(s string) (*HumanDuration, error)

func ToHumanDuration

func ToHumanDuration(d time.Duration) HumanDuration

func (HumanDuration) MarshalJSON

func (d HumanDuration) MarshalJSON() ([]byte, error)

func (HumanDuration) String

func (d HumanDuration) String() string

func (HumanDuration) ToDuration

func (d HumanDuration) ToDuration() time.Duration

func (*HumanDuration) UnmarshalJSON

func (d *HumanDuration) UnmarshalJSON(b []byte) error

type MockClock

type MockClock struct {
	// contains filtered or unexported fields
}

func NewMockClock

func NewMockClock(begin time.Time, location *time.Location) *MockClock

func (*MockClock) MockAdd

func (m *MockClock) MockAdd(d time.Duration)

func (*MockClock) Name

func (m *MockClock) Name() string

func (*MockClock) Now

func (m *MockClock) Now() time.Time

func (*MockClock) Set

func (m *MockClock) Set(t time.Time) error

func (*MockClock) SetLocation

func (m *MockClock) SetLocation(location *time.Location)

func (*MockClock) Sleep

func (m *MockClock) Sleep(d time.Duration)

type MonoClock

type MonoClock struct {
	// contains filtered or unexported fields
}

func NewMonoClock

func NewMonoClock() *MonoClock

func (*MonoClock) Name

func (c *MonoClock) Name() string

func (*MonoClock) Now

func (c *MonoClock) Now() time.Time

Now returns the current time from a monotonic clock.

The time returned is based on some arbitrary platform-specific point in the past. The time returned is guaranteed to increase monotonically without notable jumps, unlike time.Now() from the Go standard library, which may jump forward or backward significantly due to system time changes or leap seconds.

It's implemented using runtime.nanotime(), which uses CLOCK_MONOTONIC on Linux. Note that unlike CLOCK_MONOTONIC_RAW, CLOCK_MONOTONIC is affected by time changes. However, time changes never cause clock jumps; instead, clock frequency is adjusted slowly.

func (*MonoClock) Set

func (c *MonoClock) Set(tm time.Time) error

func (*MonoClock) Sleep

func (c *MonoClock) Sleep(d time.Duration)

type NtpClock

type NtpClock struct {
	// contains filtered or unexported fields
}

func GetNtpClockONLINE

func GetNtpClockONLINE() (*NtpClock, error)

func GetNtpSyncClockONLINE

func GetNtpSyncClockONLINE() (*NtpClock, error)

func (*NtpClock) IsMock

func (nc *NtpClock) IsMock() bool

func (*NtpClock) Name

func (nc *NtpClock) Name() string

func (*NtpClock) Now

func (nc *NtpClock) Now() time.Time

func (*NtpClock) Set

func (nc *NtpClock) Set(tm time.Time) error

func (*NtpClock) Sleep

func (nc *NtpClock) Sleep(d time.Duration)

type SysClock

type SysClock struct {
}

func GetSysClock

func GetSysClock() *SysClock

func (*SysClock) Name

func (sc *SysClock) Name() string

func (*SysClock) Now

func (sc *SysClock) Now() time.Time

func (*SysClock) Set

func (sc *SysClock) Set(tm time.Time) error

func (*SysClock) Sleep

func (sc *SysClock) Sleep(d time.Duration)

type Time

type Time time.Time

func New

func New(t time.Time) Time

func (Time) Elegant

func (t Time) Elegant(layout string) ElegantTime

func (Time) Std

func (t Time) Std() time.Time

type TimeMap

type TimeMap struct {
	// contains filtered or unexported fields
}

func NewTimeMap

func NewTimeMap(times []time.Time) *TimeMap

func (*TimeMap) Add

func (m *TimeMap) Add(tm time.Time)

func (*TimeMap) Exist

func (m *TimeMap) Exist(tm time.Time) bool

func (*TimeMap) Export

func (m *TimeMap) Export(loc *time.Location) []time.Time

type YearMonth

type YearMonth int32

func NewYearMonth

func NewYearMonth(year, month int) (YearMonth, error)

func ParseYearMonthInt

func ParseYearMonthInt(ym int) (YearMonth, error)

func ParseYearMonthString

func ParseYearMonthString(s string) (YearMonth, error)

support formats: 201601, 2016-01

func TimeToMonth

func TimeToMonth(tm time.Time) YearMonth

NOTICE: 不同的时区可能是不同的月份

func (*YearMonth) Equal

func (ym *YearMonth) Equal(cmp YearMonth) bool

func (*YearMonth) Int

func (ym *YearMonth) Int() int

func (*YearMonth) IsZero

func (ym *YearMonth) IsZero() bool

func (YearMonth) MarshalJSON

func (ym YearMonth) MarshalJSON() ([]byte, error)

func (*YearMonth) Month

func (ym *YearMonth) Month() time.Month

func (YearMonth) String

func (ym YearMonth) String() string

func (*YearMonth) StringYYYYMM

func (ym *YearMonth) StringYYYYMM() string

yyyymm

func (*YearMonth) StringYYYY_MM

func (ym *YearMonth) StringYYYY_MM() string

yyyy-mm

func (*YearMonth) ToTime

func (ym *YearMonth) ToTime(day, hour, minute, sec, nsec int, tz time.Location) time.Time

func (*YearMonth) ToTimeDefault

func (ym *YearMonth) ToTimeDefault() time.Time

func (*YearMonth) UnmarshalJSON

func (ym *YearMonth) UnmarshalJSON(b []byte) error

func (*YearMonth) Year

func (ym *YearMonth) Year() int

Jump to

Keyboard shortcuts

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