times

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2024 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Examples

Constants

View Source
const (
	IntervalYear = iota
	IntervalDay
	IntervalHour
	IntervalMinute
	IntervalSecond
	IntervalNow
)
View Source
const (
	Nanosecond  = time.Nanosecond
	Microsecond = time.Microsecond
	Millisecond = time.Millisecond
	Second      = time.Second
	Minute      = time.Minute
	Hour        = time.Hour
	Day         = time.Hour * 24
	Week        = Day * 7
)

Variables

View Source
var (
	Zero time.Time // 零值时间
)

Functions

func CalcNextSec

func CalcNextSec(sec int) int

CalcNextSec 计算下一个N秒在多少秒之后

func CalcNextSecWithTime

func CalcNextSecWithTime(t time.Time, sec int) int

CalcNextSecWithTime 计算下一个N秒在多少秒之后

Example
package main

import (
	"fmt"
	"github.com/kercylan98/minotaur/utils/times"
	"time"
)

func main() {
	now := time.Date(2023, 9, 20, 0, 0, 3, 0, time.Local)
	fmt.Println(times.CalcNextSecWithTime(now, 10))

}
Output:

7

func CalcNextTimeWithRefer added in v0.0.18

func CalcNextTimeWithRefer(now time.Time, refer time.Duration) time.Time

CalcNextTimeWithRefer 根据参考时间计算下一个整点时间

  • 假设当 now 为 14:15:16 , 参考时间为 10 分钟, 则返回 14:20:00
  • 假设当 now 为 14:15:16 , 参考时间为 20 分钟, 则返回 14:20:00

当 refer 小于 1 分钟时,将会返回当前时间

func Date added in v0.0.2

func Date(year int, month time.Month, day int) time.Time

Date 返回一个特定日期的时间

func DateWithHMS added in v0.0.2

func DateWithHMS(year int, month time.Month, day, hour, min, sec int) time.Time

DateWithHMS 返回一个精确到秒的时间

func GetDayLast added in v0.0.28

func GetDayLast(t time.Time) time.Time

GetDayLast 获取某天的最后一刻

  • 最后一刻即 23:59:59

func GetDayZero added in v0.0.3

func GetDayZero(t time.Time, day int) time.Time

GetDayZero 获取 t 增加 day 天后的零点时间

func GetDeltaDay added in v0.0.2

func GetDeltaDay(t1, t2 time.Time) int

GetDeltaDay 获取两个时间需要加减的天数

func GetDeltaWeek added in v0.0.2

func GetDeltaWeek(t1, t2 time.Time) int

GetDeltaWeek 获取两个时间需要加减的周数

func GetGlobalTimeOffset added in v0.3.0

func GetGlobalTimeOffset() time.Duration

GetGlobalTimeOffset 获取全局时间偏移量

func GetHSMFromString added in v0.0.2

func GetHSMFromString(timeStr, layout string) (hour, min, sec int)

GetHSMFromString 从时间字符串中获取时分秒

func GetHourEnd added in v0.2.3

func GetHourEnd(t time.Time) time.Time

GetHourEnd 获取一个时间的 59 分 59 秒

func GetHourStart added in v0.2.3

func GetHourStart(t time.Time) time.Time

GetHourStart 获取一个时间的 0 分 0 秒

func GetMinuteEnd added in v0.2.3

func GetMinuteEnd(t time.Time) time.Time

GetMinuteEnd 获取一个时间的 59 秒

func GetMinuteStart added in v0.2.3

func GetMinuteStart(t time.Time) time.Time

GetMinuteStart 获取一个时间的 0 秒

func GetMondayZero

func GetMondayZero(t time.Time) time.Time

GetMondayZero 获取本周一零点

func GetMonthDays

func GetMonthDays(t time.Time) int

GetMonthDays 获取一个时间当月共有多少天

func GetMonthEnd added in v0.2.3

func GetMonthEnd(t time.Time) time.Time

GetMonthEnd 获取一个时间的月末

func GetMonthStart added in v0.2.3

func GetMonthStart(t time.Time) time.Time

GetMonthStart 获取一个时间的月初

func GetNextDayInterval

func GetNextDayInterval(t time.Time) time.Duration

GetNextDayInterval 获取一个时间到下一天间隔多少秒

func GetSecond

func GetSecond(d time.Duration) int

GetSecond 获取共有多少秒

func GetTimeFromString added in v0.0.2

func GetTimeFromString(timeStr, layout string) time.Time

GetTimeFromString 将时间字符串转化为时间

func GetToday

func GetToday(t time.Time) time.Time

GetToday 获取一个时间的今天

func GetYearEnd added in v0.2.3

func GetYearEnd(t time.Time) time.Time

GetYearEnd 获取一个时间的年末

func GetYearStart added in v0.2.3

func GetYearStart(t time.Time) time.Time

GetYearStart 获取一个时间的年初

func GetYesterday added in v0.0.28

func GetYesterday(t time.Time) time.Time

GetYesterday 获取昨天

func GetYesterdayLast added in v0.0.28

func GetYesterdayLast(t time.Time) time.Time

GetYesterdayLast 获取昨天最后一刻

func IntervalFormat

func IntervalFormat(time1, time2 time.Time) string

IntervalFormat 返回指定时间戳之间的间隔

  • 使用传入的时间进行计算换算,将结果体现为几年前、几天前、几小时前、几分钟前、几秒前。

func IntervalFormatSet

func IntervalFormatSet(intervalType int, str string)

IntervalFormatSet 针对 IntervalFormat 函数设置格式化内容

func IsSameDay

func IsSameDay(t1, t2 time.Time) bool

IsSameDay 两个时间是否是同一天

func IsSameHour

func IsSameHour(t1, t2 time.Time) bool

IsSameHour 两个时间是否是同一小时

func NowByNotOffset added in v0.3.0

func NowByNotOffset() time.Time

NowByNotOffset 获取未偏移的当前时间

func ResetGlobalTimeOffset added in v0.3.0

func ResetGlobalTimeOffset()

ResetGlobalTimeOffset 重置全局时间偏移量

func SetGlobalTimeOffset added in v0.3.0

func SetGlobalTimeOffset(offset time.Duration)

SetGlobalTimeOffset 设置全局时间偏移量

func WeekDay

func WeekDay(t time.Time) int

WeekDay 获取一个时间是星期几

  • 1 ~ 7

Types

type Period added in v0.0.3

type Period [2]time.Time

Period 表示一个时间段

func NewPeriod added in v0.0.3

func NewPeriod(start, end time.Time) Period

NewPeriod 创建一个时间段

  • 如果 start 比 end 晚,则会自动交换两个时间

func NewPeriodWindow added in v0.2.3

func NewPeriodWindow(t time.Time, size time.Duration) Period

NewPeriodWindow 创建一个特定长度的时间窗口

func NewPeriodWindowWeek added in v0.2.3

func NewPeriodWindowWeek(t time.Time) Period

NewPeriodWindowWeek 创建一周长度的时间窗口,从周一零点开始至周日 23:59:59 结束

func NewPeriodWithDay added in v0.0.3

func NewPeriodWithDay(t time.Time, day int) Period

NewPeriodWithDay 创建一个时间段,从 t 开始,持续 day 天

func NewPeriodWithDayZero added in v0.0.3

func NewPeriodWithDayZero(t time.Time, day int) Period

NewPeriodWithDayZero 创建一个时间段,从 t 开始,持续到 day 天后的 0 点

func NewPeriodWithHour added in v0.0.3

func NewPeriodWithHour(t time.Time, hour int) Period

NewPeriodWithHour 创建一个时间段,从 t 开始,持续 hour 小时

func NewPeriodWithMicrosecond added in v0.0.3

func NewPeriodWithMicrosecond(t time.Time, microsecond int) Period

NewPeriodWithMicrosecond 创建一个时间段,从 t 开始,持续 microsecond 微秒

func NewPeriodWithMillisecond added in v0.0.3

func NewPeriodWithMillisecond(t time.Time, millisecond int) Period

NewPeriodWithMillisecond 创建一个时间段,从 t 开始,持续 millisecond 毫秒

func NewPeriodWithMinute added in v0.0.3

func NewPeriodWithMinute(t time.Time, minute int) Period

NewPeriodWithMinute 创建一个时间段,从 t 开始,持续 minute 分钟

func NewPeriodWithNanosecond added in v0.0.3

func NewPeriodWithNanosecond(t time.Time, nanosecond int) Period

NewPeriodWithNanosecond 创建一个时间段,从 t 开始,持续 nanosecond 纳秒

func NewPeriodWithSecond added in v0.0.3

func NewPeriodWithSecond(t time.Time, second int) Period

NewPeriodWithSecond 创建一个时间段,从 t 开始,持续 second 秒

func NewPeriodWithTimeArray added in v0.0.3

func NewPeriodWithTimeArray(times [2]time.Time) Period

NewPeriodWithTimeArray 创建一个时间段

func (Period) Day added in v0.0.3

func (slf Period) Day() int

Day 返回时间段的持续天数

func (Period) Duration added in v0.0.3

func (slf Period) Duration() time.Duration

Duration 返回时间段的持续时间

func (Period) End added in v0.0.3

func (slf Period) End() time.Time

End 返回时间段的结束时间

func (Period) Hour added in v0.0.3

func (slf Period) Hour() int

Hour 返回时间段的持续小时数

func (Period) IsAfter added in v0.0.3

func (slf Period) IsAfter(t time.Time) bool

IsAfter 判断时间段是否在指定时间之后

func (Period) IsBefore added in v0.0.3

func (slf Period) IsBefore(t time.Time) bool

IsBefore 判断时间段是否在指定时间之前

func (Period) IsBetween added in v0.0.3

func (slf Period) IsBetween(t time.Time) bool

IsBetween 判断指定时间是否在时间段之间

func (Period) IsBetweenOrEqual added in v0.0.3

func (slf Period) IsBetweenOrEqual(t time.Time) bool

IsBetweenOrEqual 判断指定时间是否在时间段之间或者等于时间段的开始或结束时间

func (Period) IsBetweenOrEqualPeriod added in v0.0.3

func (slf Period) IsBetweenOrEqualPeriod(t Period) bool

IsBetweenOrEqualPeriod 判断指定时间是否在时间段之间或者等于时间段的开始或结束时间

func (Period) IsInvalid added in v0.0.3

func (slf Period) IsInvalid() bool

IsInvalid 判断时间段是否无效

func (Period) IsOngoing added in v0.0.3

func (slf Period) IsOngoing(t time.Time) bool

IsOngoing 判断指定时间是否正在进行时

  • 如果时间段的开始时间在指定时间之前或者等于指定时间,且时间段的结束时间在指定时间之后,则返回 true

func (Period) IsOverlap added in v0.0.3

func (slf Period) IsOverlap(t Period) bool

IsOverlap 判断时间段是否与指定时间段重叠

func (Period) IsZero added in v0.0.3

func (slf Period) IsZero() bool

IsZero 判断时间段是否为零值

func (Period) Microseconds added in v0.0.3

func (slf Period) Microseconds() int

Microseconds 返回时间段的持续微秒数

func (Period) Milliseconds added in v0.0.3

func (slf Period) Milliseconds() int

Milliseconds 返回时间段的持续毫秒数

func (Period) Minute added in v0.0.3

func (slf Period) Minute() int

Minute 返回时间段的持续分钟数

func (Period) Nanoseconds added in v0.0.3

func (slf Period) Nanoseconds() int

Nanoseconds 返回时间段的持续纳秒数

func (Period) Seconds added in v0.0.3

func (slf Period) Seconds() int

Seconds 返回时间段的持续秒数

func (Period) Start added in v0.0.3

func (slf Period) Start() time.Time

Start 返回时间段的开始时间

type StateLine added in v0.3.2

type StateLine[State generic.Basic] struct {
	// contains filtered or unexported fields
}

StateLine 状态时间线

func NewStateLine added in v0.3.2

func NewStateLine[State generic.Basic](zero State) *StateLine[State]

NewStateLine 创建一个从左向右由早到晚的状态时间线

func (*StateLine[State]) AddState added in v0.3.2

func (slf *StateLine[State]) AddState(state State, t time.Time, onTrigger ...func()) *StateLine[State]

AddState 添加一个状态到时间线中,状态不能与任一时间点重合,否则将被忽略

  • onTrigger: 该状态绑定的触发器,该触发器不会被主动执行,需要主动获取触发器执行

func (*StateLine[State]) AddTriggerToState added in v0.3.2

func (slf *StateLine[State]) AddTriggerToState(state State, onTrigger ...func()) *StateLine[State]

AddTriggerToState 给指定状态添加触发器

func (*StateLine[State]) Check added in v0.3.2

func (slf *StateLine[State]) Check(missingAllowed bool, states ...State) bool

Check 根据状态顺序检查时间线是否合法

  • missingAllowed: 是否允许状态缺失,如果为 true,则状态可以不连续,如果为 false,则状态必须连续

状态不连续表示时间线中存在状态缺失,例如:

  • 状态为 [1, 2, 3, 4, 5] 的时间线,如果 missingAllowed 为 true,则状态为 [1, 3, 5] 也是合法的
  • 状态为 [1, 2, 3, 4, 5] 的时间线,如果 missingAllowed 为 false,则状态为 [1, 3, 5] 是不合法的

func (*StateLine[State]) GetIndexByState added in v0.3.2

func (slf *StateLine[State]) GetIndexByState(state State) int

GetIndexByState 获取指定状态的索引

func (*StateLine[State]) GetLastState added in v0.3.2

func (slf *StateLine[State]) GetLastState() State

GetLastState 获取最后一个状态

func (*StateLine[State]) GetMissingStates added in v0.3.2

func (slf *StateLine[State]) GetMissingStates(states ...State) []State

GetMissingStates 获取缺失的状态

func (*StateLine[State]) GetNextStateTimeByIndex added in v0.3.2

func (slf *StateLine[State]) GetNextStateTimeByIndex(index int) time.Time

GetNextStateTimeByIndex 获取指定索引的下一个时间点

func (*StateLine[State]) GetNextTimeByState added in v0.3.2

func (slf *StateLine[State]) GetNextTimeByState(state State) time.Time

GetNextTimeByState 获取指定状态的下一个时间点

func (*StateLine[State]) GetPrevStateTimeByIndex added in v0.3.2

func (slf *StateLine[State]) GetPrevStateTimeByIndex(index int) time.Time

GetPrevStateTimeByIndex 获取指定索引的上一个时间点

func (*StateLine[State]) GetPrevTimeByState added in v0.3.2

func (slf *StateLine[State]) GetPrevTimeByState(state State) time.Time

GetPrevTimeByState 获取指定状态的上一个时间点

func (*StateLine[State]) GetStateByIndex added in v0.3.2

func (slf *StateLine[State]) GetStateByIndex(index int) State

GetStateByIndex 获取指定索引的状态

func (*StateLine[State]) GetStateByTime added in v0.3.2

func (slf *StateLine[State]) GetStateByTime(t time.Time) State

GetStateByTime 获取指定时间点的状态

func (*StateLine[State]) GetStateCount added in v0.3.2

func (slf *StateLine[State]) GetStateCount() int

GetStateCount 获取状态数量

func (*StateLine[State]) GetStateIndexByTime added in v0.3.2

func (slf *StateLine[State]) GetStateIndexByTime(t time.Time) int

GetStateIndexByTime 获取指定时间点的索引

func (*StateLine[State]) GetTimeByIndex added in v0.3.2

func (slf *StateLine[State]) GetTimeByIndex(index int) time.Time

GetTimeByIndex 获取指定索引的时间点

func (*StateLine[State]) GetTimeByState added in v0.3.2

func (slf *StateLine[State]) GetTimeByState(state State) time.Time

GetTimeByState 获取指定状态的时间点

func (*StateLine[State]) GetTriggerByIndex added in v0.3.2

func (slf *StateLine[State]) GetTriggerByIndex(index int) []func()

GetTriggerByIndex 获取指定索引的触发器

func (*StateLine[State]) GetTriggerByState added in v0.3.2

func (slf *StateLine[State]) GetTriggerByState(state State) []func()

GetTriggerByState 获取指定状态的触发器

func (*StateLine[State]) GetTriggerByTime added in v0.3.2

func (slf *StateLine[State]) GetTriggerByTime(t time.Time) []func()

GetTriggerByTime 获取指定时间点的触发器

func (*StateLine[State]) HasState added in v0.3.2

func (slf *StateLine[State]) HasState(state State) bool

HasState 检查时间线中是否包含指定状态

func (*StateLine[State]) Move added in v0.3.2

func (slf *StateLine[State]) Move(d time.Duration) *StateLine[State]

Move 时间线整体移动

func (*StateLine[State]) Range added in v0.3.2

func (slf *StateLine[State]) Range(handler func(index int, state State, t time.Time) bool)

Range 按照时间顺序遍历时间线

func (*StateLine[State]) RangeReverse added in v0.3.2

func (slf *StateLine[State]) RangeReverse(handler func(index int, state State, t time.Time) bool)

RangeReverse 按照时间逆序遍历时间线

func (*StateLine[State]) String added in v0.3.2

func (slf *StateLine[State]) String() string

String 获取时间线的字符串表示

Jump to

Keyboard shortcuts

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