Documentation ¶
Index ¶
- Constants
- func Clamp[T int | int64 | float64](value, min, max T) int
- func DaysIn[T ~int](y T, m ...T) int
- func IsLeapYear(y int) bool
- func Since(t Time) time.Duration
- func Until(t Time) time.Duration
- type Time
- func Date[Month ~int](year int, month Month, day, hour, min, sec, nsec int, loc ...*time.Location) Time
- func New(t time.Time) Time
- func Now() Time
- func Parse(value string, loc ...*time.Location) Time
- func ParseByLayout(layout string, value string, loc ...*time.Location) Time
- func ParseByLayoutE(layout string, value string, loc ...*time.Location) (Time, error)
- func ParseE(value string, loc ...*time.Location) (Time, error)
- func Unix(secs int64) Time
- func (t Time) Add(d time.Duration) Time
- func (t Time) AddDay(d ...int) Time
- func (t Time) AddMonth(md ...int) Time
- func (t Time) AddYear(ymd ...int) Time
- func (t Time) After(u Time) bool
- func (t Time) Before(u Time) bool
- func (t Time) Clock() (hour, min, sec int)
- func (t Time) Compare(u Time) int
- func (t Time) Day() int
- func (t Time) Days() int
- func (t Time) DiffAbsIn(u Time, unit string) float64
- func (t Time) DiffIn(u Time, unit string) float64
- func (t Time) End(ymd ...int) Time
- func (t Time) EndDay(d ...int) Time
- func (t Time) EndMonth(md ...int) Time
- func (t Time) EndWeek(n ...int) Time
- func (t Time) Equal(u Time) bool
- func (t Time) Format(layout string) string
- func (t Time) Go(y int, md ...int) Time
- func (t Time) GoDay(d int) Time
- func (t Time) GoMonth(m int, d ...int) Time
- func (t Time) GoYear(y int, md ...int) Time
- func (t Time) Hour() int
- func (t Time) In(loc *time.Location) Time
- func (t Time) IsDST() bool
- func (t Time) IsZero() bool
- func (t Time) Local() Time
- func (t Time) Location() *time.Location
- func (t Time) MarshalJSON() ([]byte, error)
- func (t Time) Minute() int
- func (t Time) Month() int
- func (t Time) MonthDays() int
- func (t Time) Round(d time.Duration) Time
- func (t *Time) Scan(value any) error
- func (t Time) Second(n ...int) int
- func (t Time) Start(ymd ...int) Time
- func (t Time) StartDay(d ...int) Time
- func (t Time) StartMonth(md ...int) Time
- func (t Time) StartWeek(n ...int) Time
- func (t Time) String() string
- func (t Time) Sub(u Time) time.Duration
- func (t Time) Time() time.Time
- func (t Time) Truncate(d time.Duration) Time
- func (t Time) UTC() Time
- func (t Time) Unix(n ...int) int64
- func (t *Time) UnmarshalJSON(b []byte) (err error)
- func (t Time) Value() (driver.Value, error)
- func (t Time) Weekday() time.Weekday
- func (t Time) Year() int
- func (t Time) YearDay() int
- func (t Time) ZeroOr(u Time) Time
Constants ¶
const ( Layout = "01/02 03:04:05PM '06 -0700" // The reference time, in numerical order. ANSIC = "Mon Jan _2 15:04:05 2006" UnixDate = "Mon Jan _2 15:04:05 MST 2006" RubyDate = "Mon Jan 02 15:04:05 -0700 2006" RFC822 = "02 Jan 06 15:04 MST" RFC822Z = "02 Jan 06 15:04 -0700" // RFC822 with numeric zone RFC850 = "Monday, 02-Jan-06 15:04:05 MST" RFC1123 = "Mon, 02 Jan 2006 15:04:05 MST" RFC1123Z = "Mon, 02 Jan 2006 15:04:05 -0700" // RFC1123 with numeric zone RFC3339 = "2006-01-02T15:04:05Z07:00" RFC3339Nano = "2006-01-02T15:04:05.999999999Z07:00" Kitchen = "3:04PM" Stamp = "Jan _2 15:04:05" StampMilli = "Jan _2 15:04:05.000" StampMicro = "Jan _2 15:04:05.000000" StampNano = "Jan _2 15:04:05.000000000" DateTime = "2006-01-02 15:04:05" DateOnly = "2006-01-02" TimeOnly = "15:04:05" )
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Time ¶
type Time struct {
// contains filtered or unexported fields
}
func ParseByLayoutE ¶
func (Time) DiffIn ¶
DiffIn 返回 t 和 u 的时间差。使用 unit 参数指定比较单位:
- "y": 年
- "M": 月
- "d": 日
- "h": 小时
- "m": 分钟
- "s": 秒
func (Time) MarshalJSON ¶
MarshalJSON 将 t 转为 JSON 字符串时调用
func (Time) Round ¶ added in v0.0.10
Round 返回距离当前时间最近的 "跃点"。
这个函数有点不好理解,让我来尝试解释一下: 想象在当前时间之外存在着另一个时间循环,时间轴每次移动 d,而每个 d 就是一个 "跃点"。 该函数将返回距离当前时间最近的那个 "跃点",如果当前时间正好位于两个 "跃点" 中间,返回指向未来的那个 "跃点"。
示例:假设当前时间是 2021-07-21 14:35:29.650
舍入到秒:t.Round(time.Second)
根据定义,时间 14:35:29.650 距离下一个跃点 14:35:30.000 最近 (只需要 0.35ns), 而距离上一个跃点 14:35:29.000 较远 (需要 65ns),故返回下一个跃点 14:35:30.000。
舍入分钟:t.Round(time.Minute)
时间 14:35:29.650 距离上一个跃点 14:35:00 最近(只需要 29.650s), 而距离下一个跃点 14:36:00 较远 (需要 30.350s),故返回上一个跃点 14:35:00.000。
舍入 15 分钟:t.Round(15 * time.Minute)
跃点:--- 14:00:00 --- 14:15:00 --- 14:30:00 -- t --- 14:45:00 ---
时间 14:35:29.650 处在 14:30 (上一个跃点) 和 14:45 (下一个跃点) 之间, 距离上一个跃点最近,故返回上一个跃点时间:14:30:00。
func (Time) Second ¶
Second 返回时间的秒数或指定纳秒精度的小数部分
参数 n (可选) 指定返回的精度:
- 不提供或 0: 返回整秒数 (0-59)
- 1-9: 返回纳秒精度的小数部分,n 表示小数位数
func (Time) Truncate ¶ added in v0.0.10
Truncate 与 Round 类似,但是 Truncate 永远返回指向过去时间的 "跃点",不会进行四舍五入。
可视化理解:
----|----|----|----|---- d1 t d2 d3
Truncate 将永远返回 d1,如果时间 t 正好处于某个 "跃点" 位置,返回 t。
func (Time) Unix ¶
Unix 返回时间戳,可选择指定精度。
参数 n (可选) 指定返回的时间戳精度:
- 不提供或 0: 秒级 (10位)
- 3: 毫秒级 (13位)
- 6: 微秒级 (16位)
- 9: 纳秒级 (19位)
- 其他值: 对应位数的时间戳
func (*Time) UnmarshalJSON ¶
UnmarshalJSON 将 JSON 字符串转为 t 时调用