gtime

package
v0.0.0-...-f3abbe6 Latest Latest
Warning

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

Go to latest
Published: Sep 27, 2020 License: MIT Imports: 21 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Day     = 24 * time.Hour
	Week    = 7 * Day
	Month30 = 30 * Day
	Year365 = 365 * Day
)
View Source
const TimeLayout_FULL = "2006-01-02 15:04:05.999999999 -0700 MST"
View Source
const TimeLayout_MM_DD_HH = "15:04:05"
View Source
const TimeLayout_YYYY = "2006"
View Source
const TimeLayout_YYYY_MM = "2006-01"
View Source
const TimeLayout_YYYY_MM_DD = "2006-01-02"
View Source
const TimeLayout_YYYY_MM_DD_HH = "2006-01-02 15"
View Source
const TimeLayout_YYYY_MM_DD_HH_mm = "2006-01-02 15:04"
View Source
const TimeLayout_YYYY_MM_DD_HH_mm_SS = "2006-01-02 15:04:05"
View Source
const TimeLayout_YYYY_MM_DD_HH_mm_SS_NS = "2006-01-02 15:04:05.999999999"

Variables

View Source
var (
	EpochBeginTime time.Time = EpochSecToTime(0)              // 1970-01-01 00:00:00 +0000 UTC
	EpochBeginDate Date      = Date(19700101)                 // 0001-01-01 00:00:00 +0000 UTC
	ZeroTime       time.Time = time.Time{}                    // 0001-01-01 00:00:00 +0000 UTC
	ZeroDate       Date      = TimeToDate(ZeroTime, time.UTC) // 0001-01-01 00:00:00 +0000 UTC
	ZeroYearMonth  YearMonth = 0                              // 0000-00
	ZeroDateRange  DateRange = DateRange{Begin: ZeroDate, End: ZeroDate}
)
View Source
var (
	TimeZoneAsiaShanghai, _ = time.LoadLocation("Asia/Shanghai")
)

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 FormatDay

func FormatDay(tm time.Time) string

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 GetSystemUptime

func GetSystemUptime() (uint64, error)

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