beam

package module
v0.0.0-...-d3e133c Latest Latest
Warning

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

Go to latest
Published: May 16, 2023 License: MIT Imports: 5 Imported by: 0

README

beam

golang 时间包

安装

需要 Go 1.19+ 版本。

go get github.com/baa-god/beam

使用

创建时间
now := beam.Now() // 现在: 2023-03-19 15:05:27.792

beam.New(time.Now())            // 通过 time.Time 创建
beam.Unix(now.Unix())           // 通过秒时间戳
beam.UnixMilli(now.UnixMilli()) // 通过毫秒时间戳
beam.UnixMicro(now.UnixMicro()) // 通过微秒时间戳
beam.UnixNano(now.UnixNano())   // 通过纳秒时间戳
beam.Prase("2023-03-19")        // 通过解析时间字符串
beam.Layout("2006-01-02", "2023-03-19") // 通过布局创建
开始时间
// 加 y 年后第 m 月 d 日的开始时间
now.StartYear(-1, 2, 1) // 去年2月1日: 2022-02-01 00:00:00.000
now.StartYear(-1)       // 去年开始时间: 2022-01-01 00:00:00.000
now.StartYear()         // 今年开始时间: 2023-01-01 00:00:00.000
now.StartYear(1)        // 明年开始时间: 2024-01-01 00:00:00.000

// 加 m 月后第 d 日的开始时间
now.StartMonth(-1, 2)   // 上月2号: 2023-02-02 00:00:00.000
now.StartMonth(-1)      // 上月开始时间: 2023-02-01 00:00:00.000
now.StartMonth()        // 本月开始时间: 2023-03-01 00:00:00.000
now.StartMonth(1)       // 下月开始时间: 2023-04-01 00:00:00.000

now.StartDay(-1)        // 昨天开始时间: 2023-03-18 00:00:00.000
now.StartDay()          // 今天开始时间: 2023-03-19 00:00:00.000
now.StartDay(1)         // 明天开始时间: 2023-03-20 00:00:00.000

now.StartWeek(-1)       // 上周开始时间: 2023-03-06 00:00:00.000
now.StartWeek()         // 本周开始时间: 2023-03-13 00:00:00.000
now.StartWeek(1)        // 下周开始时间: 2023-03-20 00:00:00.000
偏移时间
// 添加年月日,若不传天数则月份不会溢出。
now.AddYear(1, -2, 3) // 此时的明年-2月+3天: 2024-01-22 15:05:27.792
now.AddMonth(-2, 3)   // 前两个月+3天: 2023-01-22 15:05:27.792
now.AddDay(3)         // 三天后: 2023-03-22 15:05:27.792
now.AddWeek(-1)       // 上周: 2023-03-12 15:05:27.792
now.Add(time.Hour)    // 1小时后: 2023-03-19 16:05:27.792

// 去 t.Year()+y 年 a[0] 月 a[1] 日。月、日均不溢出。
now.Go(-1, 11, -3) // 去年11月倒数第三天: 2022-11-28 15:05:27.792
now.GoYear(2024)   // 跟 Go() 一样,但 y 指定去确切的年份而非偏移: 2024-03-19 15:05:27.792
now.GoMonth(-3, 2) // 今年倒数第3月(10月)2号: 2023-10-02 15:05:27.792
now.GoDay(10)      // 本月10号: 2023-03-10 15:05:27.792
获取时间
now.Year()    // 今年: 2023
now.YearDay() // 今年的第几天,平年 1-365,闰年 1-366: 78
now.Month()   // 本月: 3
now.Day()     // 今天: 19
now.Days()    // 本月最大天数: 31
now.Hour()    // 此时: 15
now.Minute()  // 此分: 5
now.Second()  // 此秒: 27
now.Milli()   // 毫秒: 792
now.Micro()   // 微秒: 792000
now.Nano()    // 纳秒: 792000000
beam.DaysIn(2024, 2) // 2024 年 2 月的最大天数: 29
now.Time()           // 返回 now 的 time.Time
时间戳
now.Unix()      // 秒时间戳: 1679209527
now.UnixMilli() // 毫秒时间戳: 1679209527792
now.UnixMicro() // 微秒时间戳: 1679209527792000
now.UnixNano()  // 纳秒时间戳: 1679209527792000000
时间差
now.InYears(beam.Parse("2024-03-19"))          // 相差几年: 1
now.InDays(beam.Parse("2024-03-19"))           // 相差几天: 365
now.InHours(beam.Parse("2024-03-19"))          // 相差几时: 8768
now.InMinute(beam.Parse("2024-03-19"))         // 相差几分: 526134
now.InSeconds(beam.Parse("2024-03-19"))        // 相差几秒: 31568072
now.Sub(beam.Parse("2023-03-19 16:04:27.792")) // 时间差: 1h0m0s
beam.Since(now) // now 到现在所经过的持续时间: 0s
判断时间
IsLeap(2023) // 是否闰年: false
now.IsZero() // 是否零时,即第1年1月1日 00:00:00 UTC: false
now.Compare(beam.Parse("2024")) // 小于指定的日期返回 -1,大于返回 1,等于返回 0: -1
now.Before(beam.Parse("2024"))  // 在指定时间之前 (t < u): true 
now.After(beam.Parse("2024"))   // 在指定时间之后 (t > u): false
now.Equal(beam.Parse("2024"))   // 时间相等: false
时间字符串
now.String()   // 毫秒字符串: 2023-03-19 15:05:27.792
baem.Time{}.StringOr("default") // 用另一个字符串代替零时时间: default
now.DateTime() // 日期和时间: 2023-03-19 15:05:27
now.DateOnly() // 日期字符串: 2023-03-19
now.Format("2006年01年02日") // 格式化字符串: 2023年03月19日

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DaysIn

func DaysIn(y, m int) int

DaysIn 返回 y 年中 m 月的最大天数

func IsLeap

func IsLeap(year int) bool

IsLeap 返回 year 是否闰年

func Since

func Since(t Time) time.Duration

Since 返回自 t 以来经过时间。它是 Now().Sub(t) 的简写。

func Until

func Until(t Time) time.Duration

Until 返回直到 t 的持续时间。它是 t.Sub(Now()) 的简写。

Types

type Time

type Time struct {
	// contains filtered or unexported fields
}

func Layout

func Layout(layout string, value string) (t Time)

Layout 返回忽略错误的 LayoutE()

func LayoutE

func LayoutE(layout string, value string) (Time, error)

LayoutE 通过 layout 和 value 解析并返回它所表示的时间

func New

func New(t time.Time) Time

func Now

func Now() Time

func Parse

func Parse(value string, loc ...*time.Location) Time

Parse 返回忽略错误的 ParseE()

func ParseE

func ParseE(value string, loc ...*time.Location) (t Time, err error)

ParseE 解析 value 并返回它所表示的时间

func Unix

func Unix(sec int64) Time

func UnixMicro

func UnixMicro(usec int64) Time

func UnixMilli

func UnixMilli(msec int64) Time

func UnixNano

func UnixNano(nsec int64) Time

func (Time) Add

func (t Time) Add(d time.Duration) Time

Add 返回 t + d 的时间

func (Time) AddDay

func (t Time) AddDay(d ...int) Time

func (Time) AddMonth

func (t Time) AddMonth(md ...int) Time

func (Time) AddWeek

func (t Time) AddWeek(n int) Time

func (Time) AddYear

func (t Time) AddYear(ymd ...int) Time

AddYear 添加 年[, 月[, 日]]。若不传 [天数] 则月份不会溢出。

func (Time) After

func (t Time) After(u Time) bool

After 返回 t 是否在 u 之后 (t > u)

func (Time) Before

func (t Time) Before(u Time) bool

Before 返回 t 是否在 u 之前 (t < u)

func (Time) Compare

func (t Time) Compare(u Time) int

Compare 比较 t 和 u。 如果 t 小于 u 则返回 -1;大于返回 1;等于返回 0。

func (Time) DateOnly

func (t Time) DateOnly() string

func (Time) DateTime

func (t Time) DateTime() string

func (Time) Day

func (t Time) Day() int

Day 返回 t 的天数

func (Time) Days

func (t Time) Days() int

Days 返回本月份的最大天数

func (Time) Equal

func (t Time) Equal(u Time) bool

Equal 返回 t == u

func (Time) Format

func (t Time) Format(layout string) string

func (Time) Go

func (t Time) Go(y int, md ...int) Time

Go 去 t.Year()+y 年 a[0] 月 a[1] 日。月、日均不溢出。 如果 [月、日] 为负数,则从最后的月、日开始偏移。

func (Time) GoDay

func (t Time) GoDay(d int) Time

func (Time) GoMonth

func (t Time) GoMonth(m int, d ...int) Time

func (Time) GoYear

func (t Time) GoYear(y int, md ...int) Time

GoYear 跟 Go() 一样,但是 y 指定去确切的年份而非偏移。

func (Time) Hour

func (t Time) Hour() int

Hour 返回 t 的小时

func (Time) In

func (t Time) In(loc *time.Location) Time

In 返回 loc 时区的 t

func (Time) InDays

func (t Time) InDays(u Time) int

InDays 返回 t 与 u 的天差

func (Time) InHours

func (t Time) InHours(u Time) int

InHours 返回 t 与 u 的时差

func (Time) InMillis

func (t Time) InMillis(u Time) int

InMillis 返回 t 与 u 毫秒差

func (Time) InMinutes

func (t Time) InMinutes(u Time) int

InMinutes 返回 t 与 u 的分差

func (Time) InSeconds

func (t Time) InSeconds(u Time) int

InSeconds 返回 t 与 u 的秒差

func (Time) InYears

func (t Time) InYears(u Time) int

InYears 返回 t 与 u 的年差

func (Time) IsZero

func (t Time) IsZero() bool

IsZero 返回 t 是否零时刻

func (Time) Local

func (t Time) Local() Time

Local 返回本地时区的 t

func (Time) Location

func (t Time) Location() *time.Location

Location 返回 t 关联的时区信息

func (Time) MarshalJSON

func (t Time) MarshalJSON() ([]byte, error)

MarshalJSON 将 t 转为 JSON 字符串时调用

func (Time) Micro

func (t Time) Micro() int

Micro 返回 t 的微秒

func (Time) Milli

func (t Time) Milli() int

Milli 返回 t 的毫秒

func (Time) Minute

func (t Time) Minute() int

Minute 返回 t 的分钟

func (Time) Month

func (t Time) Month() int

Month 返回 t 的月份

func (Time) Nano

func (t Time) Nano() int

Nano 返回 t 的纳秒

func (*Time) Scan

func (t *Time) Scan(value any) error

Scan 由 DB 转到 Go 时调用

func (Time) Second

func (t Time) Second() int

Second 返回 t 的秒数

func (Time) StartDay

func (t Time) StartDay(d ...int) Time

StartDay +d 日的开始时间

func (Time) StartMonth

func (t Time) StartMonth(md ...int) Time

StartMonth +m 月后第 d 日的开始时间

func (Time) StartWeek

func (t Time) StartWeek(w ...int) Time

StartWeek 本周开始时间

func (Time) StartYear

func (t Time) StartYear(ymd ...int) Time

StartYear +y 年后第 m 月 d 日的开始时间

func (Time) String

func (t Time) String() string

func (Time) StringOr

func (t Time) StringOr(v string) string

func (Time) Sub

func (t Time) Sub(u Time) time.Duration

Sub 返回 t - u 的时间差

func (Time) Time

func (t Time) Time() time.Time

Time 返回 t 的 time.Time

func (Time) UTC

func (t Time) UTC() Time

UTC 返回 UTC 时区的 t

func (Time) Unix

func (t Time) Unix() int64

Unix 返回 t 的秒时间戳

func (Time) UnixMicro

func (t Time) UnixMicro() int64

UnixMicro 返回 t 的微秒时间戳

func (Time) UnixMilli

func (t Time) UnixMilli() int64

UnixMilli 返回 t 毫秒时间戳

func (Time) UnixNano

func (t Time) UnixNano() int64

UnixNano 返回 t 的纳秒时间戳

func (*Time) UnmarshalJSON

func (t *Time) UnmarshalJSON(b []byte) (err error)

UnmarshalJSON 将 JSON 字符串转为 t 时调用

func (Time) Value

func (t Time) Value() (driver.Value, error)

Value 由 Go 转到 DB 时调用

func (Time) Weekday

func (t Time) Weekday() int

Weekday 返回 t 的星期

func (Time) Year

func (t Time) Year() int

Year 返回 t 的年份

func (Time) YearDay

func (t Time) YearDay() int

YearDay 返回 t 年份中的第几天,非闰年为 1-365,闰年为 1-366。

Jump to

Keyboard shortcuts

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