Documentation
¶
Overview ¶
Package datetime 提供进程级缓存的时间快照,避免业务热路径每次都调用 time.Now() 的系统调用开销,并保证同一"逻辑帧"内所有调用方读到一致的时间值。
设计要点:
- 缓存值由外部周期性调用 Tick() 刷新 注册为 runtime.Component,默认 100ms 周期)。
- 内部用 atomic.Pointer[time.Time] / atomic.Int32 存储,消除高频读写下的 数据竞争(旧实现裸字段 + 注释掉的锁在 -race 下必报警)。
- 缓存精度 = 刷新周期(默认 ±100ms)。需要更高精度的单点调用应直接用 time.Now(),不要污染全局缓存。
- 保持叶子包:仅 import 标准库,不依赖 logger/scheduler,避免循环依赖。
Package scheduler —— datetime 缓存刷新预设。
datetime 是被 logger/xorm/net 等基础库直接 import 的叶子包,自身不能反向 import scheduler(否则形成 datetime→scheduler→logger→datetime 的循环)。因此 datetime 的周期刷新由 scheduler 侧主动驱动:本文件提供一个开箱即用的 Task 工厂,各 service 在 app.Register 列表注册一行即可,无需手写 tick 逻辑。
Index ¶
- Constants
- Variables
- func AppInTimeRangeByZone(start_time, end_time int64, local_tz bool, utc_offset int32, country string, ...) bool
- func BeginTimeOfToday() int32
- func DefaultDateTimeTick() *scheduler.Task
- func DiffDay(t1 int64, local_tz bool, utc_offset int32, country string) int32
- func DiffHour(t1 int64, local_tz bool, utc_offset int32, country string) int32
- func DiffMin(t1 int64, local_tz bool, utc_offset int32, country string) int32
- func DiffMonth(t1 int64, local_tz bool, utc_offset int32, country string) int32
- func DiffTimeRange(start_time, end_time int64, local_tz bool, utc_offset int32, country string) int64
- func DiffWeek(t1 int64, local_tz bool, utc_offset int32, country string) int32
- func DiffYear(t1 int64, local_tz bool, utc_offset int32, country string) int32
- func GetConfTimeOffset(country string) int32
- func GetData() string
- func GetDataHMS() string
- func GetDateByDiff(diff int32) string
- func GetDateByZone(diff int32) string
- func GetDayOfMonth(t1 int32) int32
- func GetDayOfWeek(t1 int32) int32
- func GetHourMinute() (int, int)
- func GetHourMinuteForTime(now int32) (int, int)
- func GetTimeByDiff(diff int64) int64
- func GetTimeByZone(diff int64) int64
- func GetTodayAssignTime(t time.Time, hour, minute int) time.Time
- func GetUtcTimeSec(time_str string) int64
- func GetYearByDiff(diff int32) string
- func GetYearMonthByDiff(diff int32) string
- func GetYearWeekByDiff(diff int32) (int, int)
- func GetZoneTimeSec(time_str string, diff int64) int64
- func HowDiffDays(t1, t2 int64) int32
- func HowDiffHour(t1, t2 int64) int32
- func HowDiffMin(t1, t2 int64) int32
- func HowDiffMonth(t1, t2 int64) int32
- func HowDiffWeek(t1, t2 int64) int32
- func HowDiffYear(t1, t2 int64) int32
- func InTimeRange(bSecond, eSecond int) bool
- func InTimeRangeByStr(start_str, end_str string, diff int64) bool
- func InTimeRangeByZone(start_time, end_time int64, local_tz bool, utc_offset int32, country string) bool
- func IsSameDay(t1, t2 int64) bool
- func IsSameDayByDayBeginHour(t1, t2 int64, dayBeginTime int) bool
- func IsSameHour(t1, t2 int64) bool
- func IsSameHourBySec(t1, t2 int64) bool
- func IsSameMinute(t1, t2 int64) bool
- func IsSameMinuteBySec(t1, t2 int64) bool
- func IsSameMonth(t1, t2 int64) bool
- func IsSameWeek(t1, t2 int64) bool
- func IsSameYear(t1, t2 int64) bool
- func LocalTimestamp(tt int64, local_tz bool, utc_offset int32, country string) int64
- func Now() int32
- func NowInt64() int64
- func NowMs() int64
- func NowNoOffset() int32
- func NowT() time.Time
- func NowUs() int64
- func ParseTimestamp(time_format string, local_tz bool, utc_offset int32, country string) int64
- func SameDay(t1 int64, local_tz bool, utc_offset int32, country string) bool
- func SameHour(t1 int64, local_tz bool, utc_offset int32, country string) bool
- func SameMonth(t1 int64, local_tz bool, utc_offset int32, country string) bool
- func SameWeek(t1 int64, local_tz bool, utc_offset int32, country string) bool
- func SameYear(t1 int64, local_tz bool, utc_offset int32, country string) bool
- func SetTickInterval(d time.Duration)
- func SetTimeOffset(off int32)
- func Tick()
- func TickInterval() time.Duration
- func TimeOffset() int32
Constants ¶
const ( SECONDS_PER_MINUTE = 60 MINUTES_PER_HOUR = 60 HOURS_PER_DAY = 24 DAY_PER_WEEK = 7 SECONDS_PER_HOUR = SECONDS_PER_MINUTE * MINUTES_PER_HOUR SECONDS_PER_DAY = SECONDS_PER_HOUR * HOURS_PER_DAY MINUTES_PER_DAY = MINUTES_PER_HOUR * HOURS_PER_DAY SECONDS_PER_WEEK = SECONDS_PER_DAY * DAY_PER_WEEK MS_PER_MINUTE = SECONDS_PER_MINUTE * 1000 MS_PER_HOUR = SECONDS_PER_HOUR * 1000 MS_PER_DAY = SECONDS_PER_DAY * 1000 MS_PER_SECOND = 1000 )
const
const DefaultTickInterval = 100 * time.Millisecond
DefaultTickInterval 是 datetime 缓存的默认刷新周期。100ms 在"逻辑帧时间一致性" 与"系统调用开销"之间取平衡;可被 SetTickInterval 在组件启动前覆盖。
Variables ¶
var ( //地区时间偏移配置定义 ZoneTimeOffset = map[string]int32{}/* 159 elements not displayed */ )
Functions ¶
func AppInTimeRangeByZone ¶
func AppInTimeRangeByZone(start_time, end_time int64, local_tz bool, utc_offset int32, country string, app_time int64) bool
* @title: 判断指定时间是否在活动时间内,支持不同时区 * @description: 根据传入的时间,还有时区差,判断是否在活动时间内 * @param: 开始时间,结束时间,时区偏移 * @return: bool * @author Iori
func BeginTimeOfToday ¶
func BeginTimeOfToday() int32
*
- @Description: 获取今日当前的秒数
- @return: int32
- @Author: Iori *
func DefaultDateTimeTick ¶ added in v1.2.5
应注册在所有依赖 datetime 的组件之前(如 logger/xorm/tcp_server 启动时即读时间)。
func DiffDay ¶
* * @Description: 判断当前时间差几天,支持时区偏移 * @param: time * @param: local_tz * @param: utc_offset 偏移分钟 * @return: bool * @Author: Iori *
func DiffHour ¶
* * @Description: 判断当前时间差几小时,支持时区偏移 * @param: time * @param: local_tz * @param: utc_offset 偏移分钟 * @return: bool * @Author: Iori *
func DiffMin ¶
* * @Description: 判断当前时间差几分,支持时区偏移 * @param: time * @param: local_tz * @param: utc_offset 偏移分钟 * @return: bool * @Author: Iori *
func DiffMonth ¶
* * @Description: 判断当前时间差几月,支持时区偏移 * @param: time * @param: local_tz * @param: utc_offset 偏移分钟 * @return: bool * @Author: Iori *
func DiffTimeRange ¶
func DiffTimeRange(start_time, end_time int64, local_tz bool, utc_offset int32, country string) int64
* * @Description: 对比活动时间差了多少秒 * @param: start_time * @param: end_time * @param: local_tz * @param: utc_offset 偏移分钟 * @return: int64 * @Author: Iori *
func DiffWeek ¶
* * @Description: 判断当前时间差几周,支持时区偏移 * @param: time * @param: local_tz * @param: utc_offset 偏移分钟 * @return: bool * @Author: Iori *
func DiffYear ¶
* * @Description: 判断当前时间差几年,支持时区偏移 * @param: time * @param: local_tz * @param: utc_offset 偏移分钟 * @return: bool * @Author: Iori *
func GetConfTimeOffset ¶
* * @Description: 根据国家参数读取大区配置时间偏移值 * @param: country * @return: int32 * @Author: Iori *
func GetData ¶
func GetData() string
*
- @Description: 获取当前日期格式 YYYY-MM-DD
- @return: string
- @Author: Iori *
func GetDataHMS ¶
func GetDataHMS() string
*
- @Description: 获取当前日期格式 YYYY-MM-DD HH:MM:SS
- @return: string
- @Author: Iori *
func GetDateByDiff ¶
* @title: 获取某个时区的当前日期 * @description: YYYY-MM-DD 支持时区 * @param: diff 时区偏移 分钟 * @return: string 日期 * @author Iori
func GetDateByZone ¶
* @title: 获取某个时区的当前日期 * @description: YYYY-MM-DD 支持时区 * @param: diff 时区偏移 小时 * @return: string 日期 * @author Iori
func GetHourMinuteForTime ¶
*
- @Description: 根据传入time 获取小时分钟
- @param: now time
- @return: int
- @return: int
- @Author: Iori *
func GetTimeByDiff ¶
* @title: 活动对应时区的时间戳 * @description: 手动时区偏移,得到对应时区的时间戳 * @param: 时区偏移 支持分钟 * @return: int64 时间戳
func GetTimeByZone ¶
* @title: 活动对应时区的时间戳 * @description: 手动时区偏移,得到对应时区的时间戳 * @param: 时区偏移 * @return: int64 时间戳
func GetTodayAssignTime ¶
*
- @Description: 获取当天XX:XX分的时间
- @param: t
- @param: hour
- @param: minute
- @return: time.Time
- @Author: Iori *
func GetUtcTimeSec ¶
* @title: 获取时间字符串的时间戳 * @description: 根据时间字符串,得到时间戳,秒单位 * @param: 时间字符串 * @return: int64 时间戳
func GetYearByDiff ¶
* @title: 获取某个时区的当前年 * @description: YYYY 支持时区 * @param: diff 时区偏移 分钟 * @return: string 日期 * @author Iori
func GetYearMonthByDiff ¶
* @title: 获取某个时区的当前年与月 * @description: YYYY-MM 支持时区 * @param: diff 时区偏移 分钟 * @return: string 日期 * @author Iori
func GetYearWeekByDiff ¶
* @title: 获取某个时区的当前第几周 * @description: YYYY-MM 支持时区 * @param: diff 时区偏移 分钟 * @return: string 日期 * @author Iori
func GetZoneTimeSec ¶
* @title: 获取字符串时间对应时区的时间戳 * @description: 根据时间字符串,得到对应时区时间戳,秒单位 * @param: 时间字符串,时区偏移 * @return: int64 时间戳
func InTimeRange ¶
*
- @Description: 时间区间,一般用来判断是否活动开启时间 -- 只限当地时区
- @param: bSecond
- @param: eSecond
- @return: bool
- @Author: Iori *
func InTimeRangeByStr ¶
* @title: 判断是否在活动时间内,支持不同时区 * @description: 根据字符串的活动时间,还有时区差,判断是否在活动时间内 * @param: 开始时间,结束时间,时区偏移 * @return: bool * @author Iori
func InTimeRangeByZone ¶
func InTimeRangeByZone(start_time, end_time int64, local_tz bool, utc_offset int32, country string) bool
* @title: 判断是否在活动时间内,支持不同时区 * @description: 根据传入的时间,还有时区差,判断是否在活动时间内 * @param: 开始时间,结束时间,时区偏移 * @return: bool * @author Iori
func IsSameDayByDayBeginHour ¶
*
- @Description: 是否已到开始的小时时间
- @param: t1
- @param: t2
- @param: dayBeginTime
- @return: bool
- @Author: Iori *
func IsSameHourBySec ¶
*
- @Description: 是否同一小时,根据秒来计算
- @param: t1
- @param: t2
- @return: bool
- @Author: Iori *
func IsSameMinuteBySec ¶
*
- @Description: 是否同一分钟,根据秒来计算
- @param: t1
- @param: t2
- @return: bool
- @Author: Iori *
func LocalTimestamp ¶
* * @Description: 获取偏移时间戳 * @param: tt * @param: local_tz * @param: utc_offset 偏移分钟 * @param: country * @return: int64 时间戳 * @Author: Iori *
func ParseTimestamp ¶
* * @Description: 根据字符串获取时间戳 * @param: time_format * @param: local_tz * @param: utc_offset * @return: int64 * @Author: Iori *
func SameDay ¶
* * @Description: 判断是否同一天,支持时区偏移 * @param: time * @param: local_tz * @param: utc_offset 偏移分钟 * @return: bool * @Author: Iori *
func SameHour ¶
* * @Description: 判断是否同一小时,支持时区偏移 * @param: time * @param: local_tz * @param: utc_offset 偏移分钟 * @return: bool * @Author: Iori *
func SameMonth ¶
* * @Description: 判断是否同一月,支持时区偏移 * @param: time * @param: local_tz * @param: utc_offset 偏移分钟 * @return: bool * @Author: Iori *
func SameWeek ¶
* * @Description: 判断是否同一周,支持时区偏移 * @param: time * @param: local_tz * @param: utc_offset 偏移分钟 * @return: bool * @Author: Iori *
func SameYear ¶
* * @Description: 判断是否同一年,支持时区偏移 * @param: time * @param: local_tz * @param: utc_offset 偏移分钟 * @return: bool * @Author: Iori *
func SetTickInterval ¶ added in v1.2.5
SetTickInterval 设置默认刷新周期,供 scheduler.DefaultDateTimeTick() 引用。 必须在组件 Start 之前调用(启动后修改无效,因为 Task 已用旧值构造)。 传入非正值被忽略。
Types ¶
This section is empty.