datetime

package
v1.3.4 Latest Latest
Warning

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

Go to latest
Published: Sep 2, 2026 License: MIT Imports: 7 Imported by: 0

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

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

View Source
const DefaultTickInterval = 100 * time.Millisecond

DefaultTickInterval 是 datetime 缓存的默认刷新周期。100ms 在"逻辑帧时间一致性" 与"系统调用开销"之间取平衡;可被 SetTickInterval 在组件启动前覆盖。

Variables

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

func DefaultDateTimeTick() *scheduler.Task

应注册在所有依赖 datetime 的组件之前(如 logger/xorm/tcp_server 启动时即读时间)。

func DiffDay

func DiffDay(t1 int64, local_tz bool, utc_offset int32, country string) int32

* * @Description: 判断当前时间差几天,支持时区偏移 * @param: time * @param: local_tz * @param: utc_offset 偏移分钟 * @return: bool * @Author: Iori *

func DiffHour

func DiffHour(t1 int64, local_tz bool, utc_offset int32, country string) int32

* * @Description: 判断当前时间差几小时,支持时区偏移 * @param: time * @param: local_tz * @param: utc_offset 偏移分钟 * @return: bool * @Author: Iori *

func DiffMin

func DiffMin(t1 int64, local_tz bool, utc_offset int32, country string) int32

* * @Description: 判断当前时间差几分,支持时区偏移 * @param: time * @param: local_tz * @param: utc_offset 偏移分钟 * @return: bool * @Author: Iori *

func DiffMonth

func DiffMonth(t1 int64, local_tz bool, utc_offset int32, country string) int32

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

func DiffWeek(t1 int64, local_tz bool, utc_offset int32, country string) int32

* * @Description: 判断当前时间差几周,支持时区偏移 * @param: time * @param: local_tz * @param: utc_offset 偏移分钟 * @return: bool * @Author: Iori *

func DiffYear

func DiffYear(t1 int64, local_tz bool, utc_offset int32, country string) int32

* * @Description: 判断当前时间差几年,支持时区偏移 * @param: time * @param: local_tz * @param: utc_offset 偏移分钟 * @return: bool * @Author: Iori *

func GetConfTimeOffset

func GetConfTimeOffset(country string) int32

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

func GetDateByDiff(diff int32) string

* @title: 获取某个时区的当前日期 * @description: YYYY-MM-DD 支持时区 * @param: diff 时区偏移 分钟 * @return: string 日期 * @author Iori

func GetDateByZone

func GetDateByZone(diff int32) string

* @title: 获取某个时区的当前日期 * @description: YYYY-MM-DD 支持时区 * @param: diff 时区偏移 小时 * @return: string 日期 * @author Iori

func GetDayOfMonth

func GetDayOfMonth(t1 int32) int32

*

  • @Description: 获取当前月的第几天
  • @param: t1
  • @return: int32
  • @Author: Iori *

func GetDayOfWeek

func GetDayOfWeek(t1 int32) int32

*

  • @Description: 获取当前周的第几天
  • @param: t1
  • @return: int32
  • @Author: Iori *

func GetHourMinute

func GetHourMinute() (int, int)

*

  • @Description: 获取小时,分钟
  • @return: int
  • @return: int
  • @Author: Iori *

func GetHourMinuteForTime

func GetHourMinuteForTime(now int32) (int, int)

*

  • @Description: 根据传入time 获取小时分钟
  • @param: now time
  • @return: int
  • @return: int
  • @Author: Iori *

func GetTimeByDiff

func GetTimeByDiff(diff int64) int64

* @title: 活动对应时区的时间戳 * @description: 手动时区偏移,得到对应时区的时间戳 * @param: 时区偏移 支持分钟 * @return: int64 时间戳

func GetTimeByZone

func GetTimeByZone(diff int64) int64

* @title: 活动对应时区的时间戳 * @description: 手动时区偏移,得到对应时区的时间戳 * @param: 时区偏移 * @return: int64 时间戳

func GetTodayAssignTime

func GetTodayAssignTime(t time.Time, hour, minute int) time.Time

*

  • @Description: 获取当天XX:XX分的时间
  • @param: t
  • @param: hour
  • @param: minute
  • @return: time.Time
  • @Author: Iori *

func GetUtcTimeSec

func GetUtcTimeSec(time_str string) int64

* @title: 获取时间字符串的时间戳 * @description: 根据时间字符串,得到时间戳,秒单位 * @param: 时间字符串 * @return: int64 时间戳

func GetYearByDiff

func GetYearByDiff(diff int32) string

* @title: 获取某个时区的当前年 * @description: YYYY 支持时区 * @param: diff 时区偏移 分钟 * @return: string 日期 * @author Iori

func GetYearMonthByDiff

func GetYearMonthByDiff(diff int32) string

* @title: 获取某个时区的当前年与月 * @description: YYYY-MM 支持时区 * @param: diff 时区偏移 分钟 * @return: string 日期 * @author Iori

func GetYearWeekByDiff

func GetYearWeekByDiff(diff int32) (int, int)

* @title: 获取某个时区的当前第几周 * @description: YYYY-MM 支持时区 * @param: diff 时区偏移 分钟 * @return: string 日期 * @author Iori

func GetZoneTimeSec

func GetZoneTimeSec(time_str string, diff int64) int64

* @title: 获取字符串时间对应时区的时间戳 * @description: 根据时间字符串,得到对应时区时间戳,秒单位 * @param: 时间字符串,时区偏移 * @return: int64 时间戳

func HowDiffDays

func HowDiffDays(t1, t2 int64) int32

*

  • @Description: 计算相差的天数
  • @param: t1
  • @param: t2
  • @return: int
  • @Author: Iori *

func HowDiffHour

func HowDiffHour(t1, t2 int64) int32

*

  • @Description: 差多少小时
  • @param: t1
  • @param: t2
  • @return: int32
  • @Author: Iori *

func HowDiffMin

func HowDiffMin(t1, t2 int64) int32

*

  • @Description: 差多少分钟
  • @param: t1
  • @param: t2
  • @return: int32
  • @Author: Iori *

func HowDiffMonth

func HowDiffMonth(t1, t2 int64) int32

*

  • @Description: 差多少月
  • @param: t1
  • @param: t2
  • @return: int32
  • @Author: Iori *

func HowDiffWeek

func HowDiffWeek(t1, t2 int64) int32

*

  • @Description: 差多少周
  • @param: t1
  • @param: t2
  • @return: int32
  • @Author: Iori *

func HowDiffYear

func HowDiffYear(t1, t2 int64) int32

*

  • @Description: 差多少年
  • @param: t1
  • @param: t2
  • @return: int32
  • @Author: Iori *

func InTimeRange

func InTimeRange(bSecond, eSecond int) bool

*

  • @Description: 时间区间,一般用来判断是否活动开启时间 -- 只限当地时区
  • @param: bSecond
  • @param: eSecond
  • @return: bool
  • @Author: Iori *

func InTimeRangeByStr

func InTimeRangeByStr(start_str, end_str string, diff int64) bool

* @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 IsSameDay

func IsSameDay(t1, t2 int64) bool

*

  • @Description: 是否同一天
  • @param: t1
  • @param: t2
  • @return: bool
  • @Author: Iori *

func IsSameDayByDayBeginHour

func IsSameDayByDayBeginHour(t1, t2 int64, dayBeginTime int) bool

*

  • @Description: 是否已到开始的小时时间
  • @param: t1
  • @param: t2
  • @param: dayBeginTime
  • @return: bool
  • @Author: Iori *

func IsSameHour

func IsSameHour(t1, t2 int64) bool

*

  • @Description: 是否同一小时
  • @param: t1
  • @param: t2
  • @return: bool *

func IsSameHourBySec

func IsSameHourBySec(t1, t2 int64) bool

*

  • @Description: 是否同一小时,根据秒来计算
  • @param: t1
  • @param: t2
  • @return: bool
  • @Author: Iori *

func IsSameMinute

func IsSameMinute(t1, t2 int64) bool

*

  • @Description: 是否同一分钟
  • @param: t1
  • @param: t2
  • @return: bool *

func IsSameMinuteBySec

func IsSameMinuteBySec(t1, t2 int64) bool

*

  • @Description: 是否同一分钟,根据秒来计算
  • @param: t1
  • @param: t2
  • @return: bool
  • @Author: Iori *

func IsSameMonth

func IsSameMonth(t1, t2 int64) bool

*

  • @Description: 是否同一月
  • @param: t1
  • @param: t2
  • @return: bool
  • @Author: Iori *

func IsSameWeek

func IsSameWeek(t1, t2 int64) bool

*

  • @Description: 是否同一周
  • @param: t1
  • @param: t2
  • @return: bool
  • @Author: Iori *

func IsSameYear

func IsSameYear(t1, t2 int64) bool

*

  • @Description: 是否同一年
  • @param: t1
  • @param: t2
  • @return: bool
  • @Author: Iori *

func LocalTimestamp

func LocalTimestamp(tt int64, local_tz bool, utc_offset int32, country string) int64

* * @Description: 获取偏移时间戳 * @param: tt * @param: local_tz * @param: utc_offset 偏移分钟 * @param: country * @return: int64 时间戳 * @Author: Iori *

func Now

func Now() int32

Now 获取当前时间秒(int32),含 Offset。

func NowInt64

func NowInt64() int64

NowInt64 获取当前时间秒(int64),含 Offset。

func NowMs

func NowMs() int64

NowMs 获取当前时间毫秒(int64),含 Offset。

func NowNoOffset

func NowNoOffset() int32

NowNoOffset 获取当前时间秒(int32),不含 Offset。

func NowT

func NowT() time.Time

NowT 获取当前缓存时间(精度 = 刷新周期)。

func NowUs

func NowUs() int64

NowUs 获取当前时间微秒(int64),含 Offset。

func ParseTimestamp

func ParseTimestamp(time_format string, local_tz bool, utc_offset int32, country string) int64

* * @Description: 根据字符串获取时间戳 * @param: time_format * @param: local_tz * @param: utc_offset * @return: int64 * @Author: Iori *

func SameDay

func SameDay(t1 int64, local_tz bool, utc_offset int32, country string) bool

* * @Description: 判断是否同一天,支持时区偏移 * @param: time * @param: local_tz * @param: utc_offset 偏移分钟 * @return: bool * @Author: Iori *

func SameHour

func SameHour(t1 int64, local_tz bool, utc_offset int32, country string) bool

* * @Description: 判断是否同一小时,支持时区偏移 * @param: time * @param: local_tz * @param: utc_offset 偏移分钟 * @return: bool * @Author: Iori *

func SameMonth

func SameMonth(t1 int64, local_tz bool, utc_offset int32, country string) bool

* * @Description: 判断是否同一月,支持时区偏移 * @param: time * @param: local_tz * @param: utc_offset 偏移分钟 * @return: bool * @Author: Iori *

func SameWeek

func SameWeek(t1 int64, local_tz bool, utc_offset int32, country string) bool

* * @Description: 判断是否同一周,支持时区偏移 * @param: time * @param: local_tz * @param: utc_offset 偏移分钟 * @return: bool * @Author: Iori *

func SameYear

func SameYear(t1 int64, local_tz bool, utc_offset int32, country string) bool

* * @Description: 判断是否同一年,支持时区偏移 * @param: time * @param: local_tz * @param: utc_offset 偏移分钟 * @return: bool * @Author: Iori *

func SetTickInterval added in v1.2.5

func SetTickInterval(d time.Duration)

SetTickInterval 设置默认刷新周期,供 scheduler.DefaultDateTimeTick() 引用。 必须在组件 Start 之前调用(启动后修改无效,因为 Task 已用旧值构造)。 传入非正值被忽略。

func SetTimeOffset

func SetTimeOffset(off int32)

SetTimeOffset 设置秒级偏移(用于跨时区业务判定)。

func Tick

func Tick()

Tick 刷新缓存快照一次。由外部周期驱动(推荐 scheduler.DefaultDateTimeTick)。 幂等、线程安全;调用方无需加锁。

func TickInterval added in v1.2.5

func TickInterval() time.Duration

TickInterval 返回当前生效的默认刷新周期。

func TimeOffset added in v1.2.5

func TimeOffset() int32

TimeOffset 返回当前秒级偏移。

Types

This section is empty.

Jump to

Keyboard shortcuts

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