Help us shape the future of Go! Take the 2026 Go Developer Survey

timex

package
v0.9.2 Latest Latest
Warning

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

Go to latest
Published: Oct 17, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package timex 提供常用的时间操作工具函数。

包含时间戳转换、日期格式化、时区处理、时间段边界 (BeginningOfX/EndOfX:分/时/日/周/月/季/年)等功能。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BeginningOfDay added in v0.7.0

func BeginningOfDay(t time.Time) time.Time

BeginningOfDay 返回 t 所在日的起点(00:00:00)。

func BeginningOfHour added in v0.7.0

func BeginningOfHour(t time.Time) time.Time

BeginningOfHour 返回 t 所在小时的起点(分/秒/纳秒归零)。

func BeginningOfMinute added in v0.7.0

func BeginningOfMinute(t time.Time) time.Time

BeginningOfMinute 返回 t 所在分钟的起点(秒与纳秒归零)。

func BeginningOfMonth added in v0.7.0

func BeginningOfMonth(t time.Time) time.Time

BeginningOfMonth 返回 t 所在月第一天(1 号)的起点。

func BeginningOfQuarter added in v0.7.0

func BeginningOfQuarter(t time.Time) time.Time

BeginningOfQuarter 返回 t 所在自然季度首月 1 号的起点。

func BeginningOfWeek added in v0.7.0

func BeginningOfWeek(t time.Time, weekStart time.Weekday) time.Time

BeginningOfWeek 返回 t 所在周的起点(00:00:00)。 weekStart 指定一周起始日,如 time.Monday。

func BeginningOfYear added in v0.7.0

func BeginningOfYear(t time.Time) time.Time

BeginningOfYear 返回 t 所在年 1 月 1 日的起点。

func ConvertTimezone

func ConvertTimezone(datetime string, fromLoc string) (string, error)

ConvertTimezone 将日期时间从指定时区转换为本地时区。

func Date2Stamp

func Date2Stamp(date string, format string) int64

Date2Stamp 将日期字符串转换为 Unix 时间戳。 使用本地时区解析(time.ParseInLocation),避免 UTC 偏移问题。 解析失败时返回 0。

func EndOfDay added in v0.7.0

func EndOfDay(t time.Time) time.Time

EndOfDay 返回 t 所在日的最后一纳秒(23:59:59.999999999)。 跨夏令时的 23/25 小时日按墙钟日界计算,不按固定 24h 时长。

func EndOfHour added in v0.7.0

func EndOfHour(t time.Time) time.Time

EndOfHour 返回 t 所在小时的最后一纳秒。

func EndOfMinute added in v0.7.0

func EndOfMinute(t time.Time) time.Time

EndOfMinute 返回 t 所在分钟的最后一纳秒。

func EndOfMonth added in v0.7.0

func EndOfMonth(t time.Time) time.Time

EndOfMonth 返回 t 所在月最后一天的最后一纳秒。 计算基准取月初 1 号再走 AddDate 到下月 1 号,天然规避大小月与 闰月(2 月 28/29 日)的日期归一化陷阱。

func EndOfQuarter added in v0.7.0

func EndOfQuarter(t time.Time) time.Time

EndOfQuarter 返回 t 所在自然季度最后一日的最后一纳秒。

func EndOfWeek added in v0.7.0

func EndOfWeek(t time.Time, weekStart time.Weekday) time.Time

EndOfWeek 返回 t 所在周(起始日为 weekStart)最后一日的最后一纳秒。

func EndOfYear added in v0.7.0

func EndOfYear(t time.Time) time.Time

EndOfYear 返回 t 所在年 12 月 31 日的最后一纳秒。

func GetCurrentDate

func GetCurrentDate(format string) string

GetCurrentDate 返回当前日期字符串,默认格式为 "2006-01-02"。

func GetCurrentDayStartTime

func GetCurrentDayStartTime() int64

GetCurrentDayStartTime 返回今天 00:00:00 的 Unix 时间戳。

func GetCurrentTime

func GetCurrentTime() int

GetCurrentTime 返回当前 Unix 时间戳。

func NDateBefore

func NDateBefore(n int, isMulti bool, format string) []string

NDateBefore 获取前 n 天的日期列表。 isMulti 为 true 时返回前 n 天每一天的日期,否则只返回第 n 天的日期。

func StampInt2Date

func StampInt2Date(timestamp int64, format string) string

StampInt2Date 将 Unix 时间戳转换为日期格式。

func StampString2Date

func StampString2Date(timestampStr, format string) string

StampString2Date 将时间戳字符串转换为日期格式。 解析失败时返回空字符串。

func TimestampString

func TimestampString() string

TimestampString 返回当前 Unix 时间戳的字符串。 时间戳始终为 UTC 秒数,不含时区偏移(Unix 时间戳是时区无关的)。

Types

This section is empty.

Directories

Path Synopsis
Package cron 提供 Cron 表达式解析和调度功能。
Package cron 提供 Cron 表达式解析和调度功能。

Jump to

Keyboard shortcuts

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