kmgTime

package
v0.0.0-...-05317bf Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2015 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (
	FormatMysqlZero  = "0000-00-00 00:00:00"
	FormatMysql      = "2006-01-02 15:04:05"
	FormatMysqlUs    = "2006-01-02 15:04:05.999999"
	FormatFileName   = "2006-01-02_15-04-05" //适合显示在文件上面的日期格式 @deprecated
	FormatFileNameV2 = "2006-01-02-15-04-05" //版本2,更规整,方便使用正则取出
	FormatDateMysql  = "2006-01-02"
	Iso3339Hour      = "2006-01-02T15"
	Iso3339Minute    = "2006-01-02T15:04"
	Iso3339Second    = "2006-01-02T15:04:05"
	AppleJsonFormat  = "2006-01-02 15:04:05 Etc/MST" //仅解决GMT的这个特殊情况.其他不管,如果苹果返回的字符串换时区了就悲剧了

	FormatMysqlMinute       = "2006-01-02 15:04"
	FormatMysqlMouthAndDay  = "01-02"
	FormatMysqlYearAndMoney = "2006-01"
)
View Source
const (
	Day = 24 * time.Hour
)

Variables

View Source
var BeijingZone = time.FixedZone("CST", 8*60*60)
View Source
var DefaultNower tDefaultNower

@deprecated

View Source
var DefaultTimeZone = BeijingZone
View Source
var ESTZone = time.FixedZone("EST", -5*60*60) // Eastern Standard Time(加拿大)
View Source
var ErrTimeOut = errors.New("time out")
View Source
var MysqlEnd = "9999-12-31 23:59:59"
View Source
var MysqlStart = "0000-01-01 00:00:00"
View Source
var NotFoundError = errors.New("not found")
View Source
var ReflectTypePeriodGetter = reflect.TypeOf((*PeriodGetter)(nil)).Elem()

Functions

func DateSub

func DateSub(t1 time.Time, t2 time.Time, loc *time.Location) time.Duration

规整到日期然后相减

func DateSubLocal

func DateSubLocal(t1 time.Time, t2 time.Time) time.Duration

func DateSubToDay

func DateSubToDay(t1 time.Time, t2 time.Time, loc *time.Location) int

规整到日期然后相减,返回天数

func DefaultFormat

func DefaultFormat(t time.Time) string

输出成mysql的格式,并且使用默认时区,并且在0值的时候输出空字符串

func DurationFormat

func DurationFormat(dur time.Duration) string

默认的那个精度太高了,此处减少了若干精度.

func FixLocalTimeToOffsetSpecifiedZoneTime

func FixLocalTimeToOffsetSpecifiedZoneTime(timeOffset int, localTime string) string

func FromUnixFloat

func FromUnixFloat(f float64) time.Time

func GetPeriodFromGenericSortedList

func GetPeriodFromGenericSortedList(t time.Time, SortedList interface{}) (index int, err error)

实现1,反射

func GetPeriodFromSortedList

func GetPeriodFromSortedList(t time.Time, SortedList []Period) (index int, ok bool)

SortedList should sort by start time and should not overlap each other

func GetUnixFloat

func GetUnixFloat(t1 time.Time) float64

func IsSameDay

func IsSameDay(t1 time.Time, t2 time.Time, loc *time.Location) bool

是否同一天

func ModBySecond

func ModBySecond(t1 time.Time) time.Time

规则到秒,去掉毫秒什么的

func MonthAndDayFormat

func MonthAndDayFormat(t time.Time) string

func MustFromLocalMysqlFormat

func MustFromLocalMysqlFormat(timeString string) time.Time

local time

func MustFromMysqlFormat

func MustFromMysqlFormat(timeString string) time.Time

utc time

func MustFromMysqlFormatDefaultTZ

func MustFromMysqlFormatDefaultTZ(timeString string) time.Time

使用默认时区解释mysql 数据结构

func MustFromMysqlFormatInLocation

func MustFromMysqlFormatInLocation(timeString string, loc *time.Location) time.Time

func MustLoadZone

func MustLoadZone(name string) (loc *time.Location)

golang 的时区实现看上去很复杂,而且有系统依赖,此处添加一个简单时区枚举,不考虑夏令时,也不考虑时区变化

func MustNotTimeout

func MustNotTimeout(f func(), dur time.Duration)

func MustParseAutoInDefault

func MustParseAutoInDefault(sTime string) (t time.Time)

func MustParseAutoInLocal

func MustParseAutoInLocal(sTime string) (t time.Time)

@deprecated

func MysqlNowFromDefaultNower

func MysqlNowFromDefaultNower() string

func MysqlUsNowFromDefaultNower

func MysqlUsNowFromDefaultNower() string

func NowFromDefaultNower

func NowFromDefaultNower() time.Time

func NowWithFileNameFormatV2

func NowWithFileNameFormatV2() string

func ParseAutoInDefault

func ParseAutoInDefault(sTime string) (t time.Time, err error)

func ParseAutoInLocal

func ParseAutoInLocal(sTime string) (t time.Time, err error)

@deprecated

func ParseAutoInLocation

func ParseAutoInLocation(sTime string, loc *time.Location) (t time.Time, err error)

auto guess format from ParseFormatGuessList

func PeriodListSort

func PeriodListSort(PeriodList PeriodListInterface)

func PeriodSort

func PeriodSort(p []Period)

func SelectPeriodFromSortedPeriodList

func SelectPeriodFromSortedPeriodList(t time.Time, PeriodList PeriodListInterface) (index int, ok bool)

func SetFixNowFromString

func SetFixNowFromString(s string)

func SetNowerToDefault

func SetNowerToDefault()

func Timeout

func Timeout(f func(), dur time.Duration) (hasTimeout bool)

func ToDate

func ToDate(t time.Time) time.Time

规整到日期,去掉时分秒

func ToDateString

func ToDateString(t time.Time) string

func ToLocal

func ToLocal(t time.Time) time.Time

Types

type FixedNower

type FixedNower struct {
	Time time.Time
}

func (FixedNower) Now

func (nower FixedNower) Now() time.Time

type MonthDate

type MonthDate uint32

精确到月份的日期,不携带时区信息 格式如:201407

func MonthDateFromTime

func MonthDateFromTime(t time.Time) MonthDate

func (MonthDate) DayNum

func (m MonthDate) DayNum() int

这个月的天数

func (MonthDate) IsValid

func (m MonthDate) IsValid() bool

func (MonthDate) Month

func (m MonthDate) Month() time.Month

func (MonthDate) Year

func (m MonthDate) Year() int

type Nower

type Nower interface {
	Now() time.Time
}
var NowTime Nower = tDefaultNower{}

func GetDefaultNower

func GetDefaultNower() Nower

func NewFixedNower

func NewFixedNower(time time.Time) Nower

type Period

type Period struct {
	Start time.Time
	End   time.Time
}

a period time start from Start,end to End, start must before or equal end

func MustNewPeriod

func MustNewPeriod(Start time.Time, End time.Time) (period Period)

func NewPeriod

func NewPeriod(Start time.Time, End time.Time) (period Period, err error)

start must before or equal end

func (Period) IsIn

func (p Period) IsIn(t time.Time) bool

func (Period) IsValid

func (p Period) IsValid() bool

func (Period) Overlaps

func (p Period) Overlaps(b Period) bool

type PeriodGetter

type PeriodGetter interface {
	GetPeriod() Period
}

type PeriodHour

type PeriodHour struct {
	Start int
	End   int
}

小时区间,取值范围[0,24]

type PeriodList

type PeriodList []PeriodListElement

实现3,数据结构分离

func (PeriodList) Append

func (p PeriodList) Append(period Period) PeriodList

向后追加一个时间段,index为追加前的长度

func (PeriodList) IsIncludeOverlap

func (p PeriodList) IsIncludeOverlap() bool

sort it first

func (PeriodList) Len

func (p PeriodList) Len() int

func (PeriodList) Less

func (p PeriodList) Less(i, j int) bool

func (PeriodList) SelectFromTime

func (p PeriodList) SelectFromTime(t time.Time) (OriginIndex int, ok bool)

sort it first

func (PeriodList) SetAtIndex

func (p PeriodList) SetAtIndex(period Period, i int)

func (PeriodList) Sort

func (p PeriodList) Sort()

func (PeriodList) Swap

func (p PeriodList) Swap(i, j int)

type PeriodListElement

type PeriodListElement struct {
	Period
	OriginIndex int
}

type PeriodListInterface

type PeriodListInterface interface {
	Len() int
	GetPeriodAtIndex(i int) Period
	Swap(i, j int)
}

实现2,interface

type PeriodSlice

type PeriodSlice []Period

func (PeriodSlice) Len

func (p PeriodSlice) Len() int

func (PeriodSlice) Less

func (p PeriodSlice) Less(i, j int) bool

func (PeriodSlice) Swap

func (p PeriodSlice) Swap(i, j int)

type TimeRecoverInt

type TimeRecoverInt struct {
	Num             int
	Max             int
	LastRecoverTime time.Time
	AddDuration     time.Duration
}

类似于体力一样的,每隔一定时间就会恢复一点(加一点)的值, 没有负值,不会超过最大值 如果执行时间向前退,不会报错

func (*TimeRecoverInt) Full

func (t *TimeRecoverInt) Full(now time.Time)

func (*TimeRecoverInt) Sync

func (t *TimeRecoverInt) Sync(now time.Time)

Jump to

Keyboard shortcuts

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