parent
6ed4ff2ca6
commit
97dd0d930b
@ -0,0 +1,3 @@
|
|||||||
|
module github.com/dtapps/go-library/utils
|
||||||
|
|
||||||
|
go 1.18
|
@ -0,0 +1,3 @@
|
|||||||
|
module github.com/dtapps/go-library/utils/goaes
|
||||||
|
|
||||||
|
go 1.18
|
@ -0,0 +1,3 @@
|
|||||||
|
module github.com/dtapps/go-library/utils/goarray
|
||||||
|
|
||||||
|
go 1.18
|
@ -1 +1,8 @@
|
|||||||
package goarray
|
package goarray
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestName(t *testing.T) {
|
||||||
|
ss := []string{"c2:37:7d:2b:78:c3", "c2:37:7d:2b:78:c4", "14:98:77:5a:58:66", "c2:37:7d:2b:78:a3", "c2:37:7d:2b:78:a4", "36:ce:4f:d2:84:00", "36:ce:4f:d2:84:04", "36:98:77:50:03:1b", "14:98:77:50:03:1b", "36:ce:4f:d2:84:00", "e2:ed:05:15:9c:2a", "e2:ed:05:15:9c:2a"}
|
||||||
|
t.Log(TurnString(ss))
|
||||||
|
}
|
||||||
|
@ -0,0 +1,3 @@
|
|||||||
|
module github.com/dtapps/go-library/utils/gobase64
|
||||||
|
|
||||||
|
go 1.18
|
@ -0,0 +1,3 @@
|
|||||||
|
module github.com/dtapps/go-library/utils/gostring
|
||||||
|
|
||||||
|
go 1.18
|
@ -0,0 +1,15 @@
|
|||||||
|
package gotime
|
||||||
|
|
||||||
|
import "time"
|
||||||
|
|
||||||
|
// Tomorrow 明天
|
||||||
|
func Tomorrow() Pro {
|
||||||
|
p := NewPro()
|
||||||
|
location, err := time.LoadLocation("Asia/Shanghai")
|
||||||
|
if err != nil {
|
||||||
|
p.Time = time.Now().Add(time.Hour*8).AddDate(0, 0, +1)
|
||||||
|
} else {
|
||||||
|
p.Time = time.Now().In(location).AddDate(0, 0, +1)
|
||||||
|
}
|
||||||
|
return p
|
||||||
|
}
|
@ -0,0 +1,91 @@
|
|||||||
|
package gotime
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// 时间格式化常量
|
||||||
|
const (
|
||||||
|
RFC3339Format = time.RFC3339
|
||||||
|
Iso8601Format = "2006-01-02T15:04:05-07:00"
|
||||||
|
CookieFormat = "Monday, 02-Jan-2006 15:04:05 MST"
|
||||||
|
RFC1036Format = "Mon, 02 Jan 06 15:04:05 -0700"
|
||||||
|
RFC7231Format = "Mon, 02 Jan 2006 15:04:05 GMT"
|
||||||
|
DayDateTimeFormat = "Mon, Jan 2, 2006 3:04 PM"
|
||||||
|
DateTimeFormat = "2006-01-02 15:04:05"
|
||||||
|
DateFormat = "2006-01-02"
|
||||||
|
TimeFormat = "15:04:05"
|
||||||
|
ShortDateTimeFormat = "20060102150405"
|
||||||
|
ShortDateFormat = "20060102"
|
||||||
|
ShortTimeFormat = "150405"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Pro 结构体
|
||||||
|
type Pro struct {
|
||||||
|
Time time.Time
|
||||||
|
loc *time.Location
|
||||||
|
Error error
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewPro 初始化结构体
|
||||||
|
func NewPro() Pro {
|
||||||
|
return Pro{
|
||||||
|
Time: time.Now(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// BeforeSeconds 获取n秒前的时间
|
||||||
|
func (p Pro) BeforeSeconds(seconds int) Pro {
|
||||||
|
st, _ := time.ParseDuration(fmt.Sprintf("-%ds", seconds))
|
||||||
|
p.Time = p.Time.Add(st)
|
||||||
|
return p
|
||||||
|
}
|
||||||
|
|
||||||
|
// AfterSeconds 获取n秒后的时间
|
||||||
|
func (p Pro) AfterSeconds(seconds int) Pro {
|
||||||
|
st, _ := time.ParseDuration(fmt.Sprintf("+%ds", seconds))
|
||||||
|
p.Time = p.Time.Add(st)
|
||||||
|
return p
|
||||||
|
}
|
||||||
|
|
||||||
|
// BeforeHour 获取n小时前的时间
|
||||||
|
func (p Pro) BeforeHour(hour int) Pro {
|
||||||
|
st, _ := time.ParseDuration(fmt.Sprintf("-%dh", hour))
|
||||||
|
p.Time = p.Time.Add(st)
|
||||||
|
return p
|
||||||
|
}
|
||||||
|
|
||||||
|
// AfterHour 获取n小时后的时间
|
||||||
|
func (p Pro) AfterHour(hour int) Pro {
|
||||||
|
st, _ := time.ParseDuration(fmt.Sprintf("+%dh", hour))
|
||||||
|
p.Time = p.Time.Add(st)
|
||||||
|
return p
|
||||||
|
}
|
||||||
|
|
||||||
|
// BeforeDay 获取n天前的时间
|
||||||
|
func (p Pro) BeforeDay(day int) Pro {
|
||||||
|
p.Time = p.Time.AddDate(0, 0, -day)
|
||||||
|
return p
|
||||||
|
}
|
||||||
|
|
||||||
|
// AfterDay 获取n天后的时间
|
||||||
|
func (p Pro) AfterDay(day int) Pro {
|
||||||
|
p.Time = p.Time.AddDate(0, 0, day)
|
||||||
|
return p
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetFormat 格式化
|
||||||
|
func (p Pro) SetFormat(layout string) string {
|
||||||
|
return p.Time.Format(layout)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Month 获取当前月
|
||||||
|
func (p Pro) Month() int {
|
||||||
|
return p.MonthOfYear()
|
||||||
|
}
|
||||||
|
|
||||||
|
// MonthOfYear 获取本年的第几月
|
||||||
|
func (p Pro) MonthOfYear() int {
|
||||||
|
return int(p.Time.In(p.loc).Month())
|
||||||
|
}
|
@ -0,0 +1,69 @@
|
|||||||
|
package gotime
|
||||||
|
|
||||||
|
import "time"
|
||||||
|
|
||||||
|
// DiffInHour 相差多少小时
|
||||||
|
func (p Pro) DiffInHour(t2 time.Time) (hour int64) {
|
||||||
|
t2.Before(p.Time)
|
||||||
|
diff := p.Time.Unix() - t2.Unix()
|
||||||
|
hour = diff / 3600
|
||||||
|
return hour
|
||||||
|
}
|
||||||
|
|
||||||
|
// DiffInHourWithAbs 相差多少小时(绝对值)
|
||||||
|
func (p Pro) DiffInHourWithAbs(t2 time.Time) (hour int64) {
|
||||||
|
p.Time.Before(t2)
|
||||||
|
diff := t2.Unix() - p.Time.Unix()
|
||||||
|
hour = diff / 3600
|
||||||
|
if hour > 0 {
|
||||||
|
return hour
|
||||||
|
}
|
||||||
|
t2.Before(p.Time)
|
||||||
|
diff = p.Time.Unix() - t2.Unix()
|
||||||
|
hour = diff / 3600
|
||||||
|
return hour
|
||||||
|
}
|
||||||
|
|
||||||
|
// DiffInMinutes 相差多少分钟
|
||||||
|
func (p Pro) DiffInMinutes(t2 time.Time) (hour int64) {
|
||||||
|
t2.Before(p.Time)
|
||||||
|
diff := p.Time.Unix() - t2.Unix()
|
||||||
|
hour = diff / 60
|
||||||
|
return hour
|
||||||
|
}
|
||||||
|
|
||||||
|
// DiffInMinutesWithAbs 相差多少分钟(绝对值)
|
||||||
|
func (p Pro) DiffInMinutesWithAbs(t2 time.Time) (hour int64) {
|
||||||
|
p.Time.Before(t2)
|
||||||
|
diff := t2.Unix() - p.Time.Unix()
|
||||||
|
hour = diff / 60
|
||||||
|
if hour > 0 {
|
||||||
|
return hour
|
||||||
|
}
|
||||||
|
t2.Before(p.Time)
|
||||||
|
diff = p.Time.Unix() - t2.Unix()
|
||||||
|
hour = diff / 60
|
||||||
|
return hour
|
||||||
|
}
|
||||||
|
|
||||||
|
// DiffInSecond 相差多少秒
|
||||||
|
func (p Pro) DiffInSecond(t2 time.Time) (hour int64) {
|
||||||
|
t2.Before(p.Time)
|
||||||
|
diff := p.Time.Unix() - t2.Unix()
|
||||||
|
hour = diff
|
||||||
|
return hour
|
||||||
|
}
|
||||||
|
|
||||||
|
// DiffInSecondWithAbs 相差多少秒(绝对值)
|
||||||
|
func (p Pro) DiffInSecondWithAbs(t2 time.Time) (hour int64) {
|
||||||
|
p.Time.Before(t2)
|
||||||
|
diff := t2.Unix() - p.Time.Unix()
|
||||||
|
hour = diff
|
||||||
|
if hour > 0 {
|
||||||
|
return hour
|
||||||
|
}
|
||||||
|
t2.Before(p.Time)
|
||||||
|
diff = p.Time.Unix() - t2.Unix()
|
||||||
|
hour = diff
|
||||||
|
return hour
|
||||||
|
}
|
@ -0,0 +1,8 @@
|
|||||||
|
package gotime
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
// invalidTimezoneError 无效的时区错误
|
||||||
|
var invalidTimezoneError = func(timezone string) error {
|
||||||
|
return fmt.Errorf("invalid timezone %q, please see the file %q for all valid timezones", timezone, "$GOROOT/lib/time/zoneinfo.zip")
|
||||||
|
}
|
@ -0,0 +1,3 @@
|
|||||||
|
module github.com/dtapps/go-library/utils/gotime
|
||||||
|
|
||||||
|
go 1.18
|
@ -1,252 +0,0 @@
|
|||||||
package gotime
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"log"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
RFC822Format = "Mon, 02 Jan 2006 15:04:05 MST"
|
|
||||||
ISO8601Format = "2006-01-02T15:04:05Z"
|
|
||||||
)
|
|
||||||
|
|
||||||
func NowUTCSeconds() int64 { return time.Now().UTC().Unix() }
|
|
||||||
|
|
||||||
func NowUTCNanoSeconds() int64 { return time.Now().UTC().UnixNano() }
|
|
||||||
|
|
||||||
// GetCurrentDate 获取当前的时间 - 字符串
|
|
||||||
func GetCurrentDate() string {
|
|
||||||
return time.Now().Format("2006/01/02 15:04:05")
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetCurrentUnix 获取当前的时间 - Unix时间戳
|
|
||||||
func GetCurrentUnix() int64 {
|
|
||||||
return time.Now().Unix()
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetCurrentMilliUnix 获取当前的时间 - 毫秒级时间戳
|
|
||||||
func GetCurrentMilliUnix() int64 {
|
|
||||||
return time.Now().UnixNano() / 1000000
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetCurrentNanoUnix 获取当前的时间 - 纳秒级时间戳
|
|
||||||
func GetCurrentNanoUnix() int64 {
|
|
||||||
return time.Now().UnixNano()
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetCurrentWjDate 获取当前的时间 - 字符串 - 没有间隔
|
|
||||||
func GetCurrentWjDate() string {
|
|
||||||
return time.Now().Format("20060102")
|
|
||||||
}
|
|
||||||
|
|
||||||
func FormatISO8601Date(timestampSecond int64) string {
|
|
||||||
tm := time.Unix(timestampSecond, 0).UTC()
|
|
||||||
return tm.Format(ISO8601Format)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Pro 结构体
|
|
||||||
type Pro struct {
|
|
||||||
Time time.Time
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewPro 初始化结构体
|
|
||||||
func NewPro() Pro {
|
|
||||||
return Pro{
|
|
||||||
Time: time.Now(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Current 获取当前的时间
|
|
||||||
func Current() Pro {
|
|
||||||
p := NewPro()
|
|
||||||
location, err := time.LoadLocation("Asia/Shanghai")
|
|
||||||
if err != nil {
|
|
||||||
// Docker部署golang应用时时区问题 https://www.ddhigh.com/2018/03/01/golang-docker-timezone.html
|
|
||||||
log.Printf("时区错误:%v\n", err)
|
|
||||||
p.Time = time.Now().Add(time.Hour * 8)
|
|
||||||
} else {
|
|
||||||
p.Time = time.Now().In(location)
|
|
||||||
}
|
|
||||||
return p
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetCurrent 设置当前的时间
|
|
||||||
func SetCurrent(sTime time.Time) Pro {
|
|
||||||
p := NewPro()
|
|
||||||
p.Time = sTime
|
|
||||||
return p
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetCurrentParse 设置当前的时间
|
|
||||||
func SetCurrentParse(str string) Pro {
|
|
||||||
p := NewPro()
|
|
||||||
location, _ := time.ParseInLocation("2006-01-02 15:04:05", str, time.Local)
|
|
||||||
p.Time = location
|
|
||||||
return p
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetCurrentUnix 设置当前的时间 Unix时间戳
|
|
||||||
func SetCurrentUnix(ts int64) Pro {
|
|
||||||
p := NewPro()
|
|
||||||
p.Time = time.Unix(ts, 0)
|
|
||||||
return p
|
|
||||||
}
|
|
||||||
|
|
||||||
// StartOfDay 本日开始时间
|
|
||||||
func (p Pro) StartOfDay() Pro {
|
|
||||||
p.Time = time.Date(p.Time.Year(), p.Time.Month(), p.Time.Day(), 0, 0, 0, 0, p.Time.Location())
|
|
||||||
return p
|
|
||||||
}
|
|
||||||
|
|
||||||
// EndOfDay 本日结束时间
|
|
||||||
func (p Pro) EndOfDay() Pro {
|
|
||||||
p.Time = time.Date(p.Time.Year(), p.Time.Month(), p.Time.Day(), 23, 59, 59, 0, p.Time.Location())
|
|
||||||
return p
|
|
||||||
}
|
|
||||||
|
|
||||||
// BeforeSeconds 获取n秒前的时间
|
|
||||||
func (p Pro) BeforeSeconds(seconds int) Pro {
|
|
||||||
st, _ := time.ParseDuration(fmt.Sprintf("-%ds", seconds))
|
|
||||||
p.Time = p.Time.Add(st)
|
|
||||||
return p
|
|
||||||
}
|
|
||||||
|
|
||||||
// AfterSeconds 获取n秒后的时间
|
|
||||||
func (p Pro) AfterSeconds(seconds int) Pro {
|
|
||||||
st, _ := time.ParseDuration(fmt.Sprintf("+%ds", seconds))
|
|
||||||
p.Time = p.Time.Add(st)
|
|
||||||
return p
|
|
||||||
}
|
|
||||||
|
|
||||||
// BeforeHour 获取n小时前的时间
|
|
||||||
func (p Pro) BeforeHour(hour int) Pro {
|
|
||||||
st, _ := time.ParseDuration(fmt.Sprintf("-%dh", hour))
|
|
||||||
p.Time = p.Time.Add(st)
|
|
||||||
return p
|
|
||||||
}
|
|
||||||
|
|
||||||
// AfterHour 获取n小时后的时间
|
|
||||||
func (p Pro) AfterHour(hour int) Pro {
|
|
||||||
st, _ := time.ParseDuration(fmt.Sprintf("+%dh", hour))
|
|
||||||
p.Time = p.Time.Add(st)
|
|
||||||
return p
|
|
||||||
}
|
|
||||||
|
|
||||||
// BeforeDay 获取n天前的时间
|
|
||||||
func (p Pro) BeforeDay(day int) Pro {
|
|
||||||
p.Time = p.Time.AddDate(0, 0, -day)
|
|
||||||
return p
|
|
||||||
}
|
|
||||||
|
|
||||||
// AfterDay 获取n天后的时间
|
|
||||||
func (p Pro) AfterDay(day int) Pro {
|
|
||||||
p.Time = p.Time.AddDate(0, 0, day)
|
|
||||||
return p
|
|
||||||
}
|
|
||||||
|
|
||||||
// Now 返回当前时间
|
|
||||||
func (p Pro) Now() time.Time {
|
|
||||||
return p.Time
|
|
||||||
}
|
|
||||||
|
|
||||||
// Format 格式化
|
|
||||||
func (p Pro) Format() string {
|
|
||||||
return p.Time.Format("2006-01-02 15:04:05")
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetFormat 格式化
|
|
||||||
func (p Pro) SetFormat(layout string) string {
|
|
||||||
return p.Time.Format(layout)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Timestamp 秒级时间戳
|
|
||||||
func (p Pro) Timestamp() int64 {
|
|
||||||
return p.Time.Unix()
|
|
||||||
}
|
|
||||||
|
|
||||||
// Unix 时间戳
|
|
||||||
func (p Pro) Unix() int64 {
|
|
||||||
return p.Time.Unix()
|
|
||||||
}
|
|
||||||
|
|
||||||
// UnixNano 纳秒级时间戳
|
|
||||||
func (p Pro) UnixNano() int64 {
|
|
||||||
return p.Time.UnixNano()
|
|
||||||
}
|
|
||||||
|
|
||||||
// TimestampWithMillisecond 毫秒级时间戳
|
|
||||||
func (p Pro) TimestampWithMillisecond() int64 {
|
|
||||||
return p.Time.UnixNano() / 100
|
|
||||||
}
|
|
||||||
|
|
||||||
// Nanosecond 时间戳小数部分 单位:纳秒
|
|
||||||
func (p Pro) Nanosecond() int {
|
|
||||||
return p.Time.Nanosecond()
|
|
||||||
}
|
|
||||||
|
|
||||||
// DiffInHour 相差多少小时
|
|
||||||
func (p Pro) DiffInHour(t2 time.Time) (hour int64) {
|
|
||||||
t2.Before(p.Time)
|
|
||||||
diff := p.Time.Unix() - t2.Unix()
|
|
||||||
hour = diff / 3600
|
|
||||||
return hour
|
|
||||||
}
|
|
||||||
|
|
||||||
// DiffInHourWithAbs 相差多少小时(绝对值)
|
|
||||||
func (p Pro) DiffInHourWithAbs(t2 time.Time) (hour int64) {
|
|
||||||
p.Time.Before(t2)
|
|
||||||
diff := t2.Unix() - p.Time.Unix()
|
|
||||||
hour = diff / 3600
|
|
||||||
if hour > 0 {
|
|
||||||
return hour
|
|
||||||
}
|
|
||||||
t2.Before(p.Time)
|
|
||||||
diff = p.Time.Unix() - t2.Unix()
|
|
||||||
hour = diff / 3600
|
|
||||||
return hour
|
|
||||||
}
|
|
||||||
|
|
||||||
// DiffInMinutes 相差多少分钟
|
|
||||||
func (p Pro) DiffInMinutes(t2 time.Time) (hour int64) {
|
|
||||||
t2.Before(p.Time)
|
|
||||||
diff := p.Time.Unix() - t2.Unix()
|
|
||||||
hour = diff / 60
|
|
||||||
return hour
|
|
||||||
}
|
|
||||||
|
|
||||||
// DiffInMinutesWithAbs 相差多少分钟(绝对值)
|
|
||||||
func (p Pro) DiffInMinutesWithAbs(t2 time.Time) (hour int64) {
|
|
||||||
p.Time.Before(t2)
|
|
||||||
diff := t2.Unix() - p.Time.Unix()
|
|
||||||
hour = diff / 60
|
|
||||||
if hour > 0 {
|
|
||||||
return hour
|
|
||||||
}
|
|
||||||
t2.Before(p.Time)
|
|
||||||
diff = p.Time.Unix() - t2.Unix()
|
|
||||||
hour = diff / 60
|
|
||||||
return hour
|
|
||||||
}
|
|
||||||
|
|
||||||
// DiffInSecond 相差多少秒
|
|
||||||
func (p Pro) DiffInSecond(t2 time.Time) (hour int64) {
|
|
||||||
t2.Before(p.Time)
|
|
||||||
diff := p.Time.Unix() - t2.Unix()
|
|
||||||
hour = diff
|
|
||||||
return hour
|
|
||||||
}
|
|
||||||
|
|
||||||
// DiffInSecondWithAbs 相差多少秒(绝对值)
|
|
||||||
func (p Pro) DiffInSecondWithAbs(t2 time.Time) (hour int64) {
|
|
||||||
p.Time.Before(t2)
|
|
||||||
diff := t2.Unix() - p.Time.Unix()
|
|
||||||
hour = diff
|
|
||||||
if hour > 0 {
|
|
||||||
return hour
|
|
||||||
}
|
|
||||||
t2.Before(p.Time)
|
|
||||||
diff = p.Time.Unix() - t2.Unix()
|
|
||||||
hour = diff
|
|
||||||
return hour
|
|
||||||
}
|
|
@ -0,0 +1,14 @@
|
|||||||
|
package gotime
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// 通过时区获取 Location 实例
|
||||||
|
func getLocationByTimezone(timezone string) (*time.Location, error) {
|
||||||
|
loc, err := time.LoadLocation(timezone)
|
||||||
|
if err != nil {
|
||||||
|
err = invalidTimezoneError(timezone)
|
||||||
|
}
|
||||||
|
return loc, err
|
||||||
|
}
|
@ -0,0 +1,124 @@
|
|||||||
|
package gotime
|
||||||
|
|
||||||
|
import "time"
|
||||||
|
|
||||||
|
// 数字常量
|
||||||
|
const (
|
||||||
|
YearsPerMillennium = 1000 // 每千年1000年
|
||||||
|
YearsPerCentury = 100 // 每世纪100年
|
||||||
|
YearsPerDecade = 10 // 每十年10年
|
||||||
|
QuartersPerYear = 4 // 每年4季度
|
||||||
|
MonthsPerYear = 12 // 每年12月
|
||||||
|
MonthsPerQuarter = 3 // 每季度3月
|
||||||
|
WeeksPerNormalYear = 52 // 每常规年52周
|
||||||
|
weeksPerLongYear = 53 // 每长年53周
|
||||||
|
WeeksPerMonth = 4 // 每月4周
|
||||||
|
DaysPerLeapYear = 366 // 每闰年366天
|
||||||
|
DaysPerNormalYear = 365 // 每常规年365天
|
||||||
|
DaysPerWeek = 7 // 每周7天
|
||||||
|
HoursPerWeek = 168 // 每周168小时
|
||||||
|
HoursPerDay = 24 // 每天24小时
|
||||||
|
MinutesPerDay = 1440 // 每天1440分钟
|
||||||
|
MinutesPerHour = 60 // 每小时60分钟
|
||||||
|
SecondsPerWeek = 604800 // 每周604800秒
|
||||||
|
SecondsPerDay = 86400 // 每天86400秒
|
||||||
|
SecondsPerHour = 3600 // 每小时3600秒
|
||||||
|
SecondsPerMinute = 60 // 每分钟60秒
|
||||||
|
MillisecondsPerSecond = 1000 // 每秒1000毫秒
|
||||||
|
MicrosecondsPerMillisecond = 1000 // 每毫秒1000微秒
|
||||||
|
MicrosecondsPerSecond = 1000000 // 每秒1000000微秒
|
||||||
|
)
|
||||||
|
|
||||||
|
// StartOfCentury 本世纪开始时间
|
||||||
|
func (p Pro) StartOfCentury() Pro {
|
||||||
|
p.Time = time.Date(p.Time.Year()/YearsPerCentury*YearsPerCentury, 1, 1, 0, 0, 0, 0, p.Time.Location())
|
||||||
|
return p
|
||||||
|
}
|
||||||
|
|
||||||
|
// EndOfCentury 本世纪结束时间
|
||||||
|
func (p Pro) EndOfCentury() Pro {
|
||||||
|
p.Time = time.Date(p.Time.Year()/YearsPerCentury*YearsPerCentury+99, 12, 31, 23, 59, 59, 999999999, p.Time.Location())
|
||||||
|
return p
|
||||||
|
}
|
||||||
|
|
||||||
|
// StartOfDecade 本年代开始时间
|
||||||
|
func (p Pro) StartOfDecade() Pro {
|
||||||
|
p.Time = time.Date(p.Time.Year()/YearsPerDecade*YearsPerDecade, 1, 1, 0, 0, 0, 0, p.Time.Location())
|
||||||
|
return p
|
||||||
|
}
|
||||||
|
|
||||||
|
// EndOfDecade 本年代结束时间
|
||||||
|
func (p Pro) EndOfDecade() Pro {
|
||||||
|
p.Time = time.Date(p.Time.Year()/YearsPerDecade*YearsPerDecade+9, 12, 31, 23, 59, 59, 999999999, p.Time.Location())
|
||||||
|
return p
|
||||||
|
}
|
||||||
|
|
||||||
|
// StartOfYear 本年开始时间
|
||||||
|
func (p Pro) StartOfYear() Pro {
|
||||||
|
p.Time = time.Date(p.Time.Year(), 1, 1, 0, 0, 0, 0, p.Time.Location())
|
||||||
|
return p
|
||||||
|
}
|
||||||
|
|
||||||
|
// EndOfYear 本年结束时间
|
||||||
|
func (p Pro) EndOfYear() Pro {
|
||||||
|
p.Time = time.Date(p.Time.Year(), 12, 31, 23, 59, 59, 999999999, p.Time.Location())
|
||||||
|
return p
|
||||||
|
}
|
||||||
|
|
||||||
|
// Quarter 获取当前季度
|
||||||
|
func (p Pro) Quarter() (quarter int) {
|
||||||
|
switch {
|
||||||
|
case p.Time.Month() >= 10:
|
||||||
|
quarter = 4
|
||||||
|
case p.Time.Month() >= 7:
|
||||||
|
quarter = 3
|
||||||
|
case p.Time.Month() >= 4:
|
||||||
|
quarter = 2
|
||||||
|
case p.Time.Month() >= 1:
|
||||||
|
quarter = 1
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// StartOfQuarter 本季度开始时间
|
||||||
|
func (p Pro) StartOfQuarter() Pro {
|
||||||
|
p.Time = time.Date(p.Time.Year(), time.Month(3*p.Quarter()-2), 1, 0, 0, 0, 0, p.Time.Location())
|
||||||
|
return p
|
||||||
|
}
|
||||||
|
|
||||||
|
// EndOfQuarter 本季度结束时间
|
||||||
|
func (p Pro) EndOfQuarter() Pro {
|
||||||
|
quarter, day := p.Quarter(), 30
|
||||||
|
switch quarter {
|
||||||
|
case 1, 4:
|
||||||
|
day = 31
|
||||||
|
case 2, 3:
|
||||||
|
day = 30
|
||||||
|
}
|
||||||
|
p.Time = time.Date(p.Time.Year(), time.Month(3*quarter), day, 23, 59, 59, 999999999, p.Time.Location())
|
||||||
|
return p
|
||||||
|
}
|
||||||
|
|
||||||
|
// StartOfMonth 本月开始时间
|
||||||
|
func (p Pro) StartOfMonth() Pro {
|
||||||
|
p.Time = time.Date(p.Time.Year(), time.Month(p.Month()), 1, 0, 0, 0, 0, p.Time.Location())
|
||||||
|
return p
|
||||||
|
}
|
||||||
|
|
||||||
|
// EndOfMonth 本月结束时间
|
||||||
|
func (p Pro) EndOfMonth() Pro {
|
||||||
|
p.Time = time.Date(p.Time.Year(), time.Month(p.Month()), 1, 23, 59, 59, 999999999, p.Time.Location())
|
||||||
|
return p
|
||||||
|
}
|
||||||
|
|
||||||
|
// StartOfDay 本日开始时间
|
||||||
|
func (p Pro) StartOfDay() Pro {
|
||||||
|
p.Time = time.Date(p.Time.Year(), p.Time.Month(), p.Time.Day(), 0, 0, 0, 0, p.Time.Location())
|
||||||
|
return p
|
||||||
|
}
|
||||||
|
|
||||||
|
// EndOfDay 本日结束时间
|
||||||
|
func (p Pro) EndOfDay() Pro {
|
||||||
|
p.Time = time.Date(p.Time.Year(), p.Time.Month(), p.Time.Day(), 23, 59, 59, 0, p.Time.Location())
|
||||||
|
return p
|
||||||
|
}
|
@ -0,0 +1,17 @@
|
|||||||
|
package gotime
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Yesterday 昨天
|
||||||
|
func Yesterday() Pro {
|
||||||
|
p := NewPro()
|
||||||
|
location, err := time.LoadLocation("Asia/Shanghai")
|
||||||
|
if err != nil {
|
||||||
|
p.Time = time.Now().Add(time.Hour*8).AddDate(0, 0, -1)
|
||||||
|
} else {
|
||||||
|
p.Time = time.Now().In(location).AddDate(0, 0, -1)
|
||||||
|
}
|
||||||
|
return p
|
||||||
|
}
|
@ -1 +0,0 @@
|
|||||||
package utils
|
|
Loading…
Reference in new issue