feat(Go-Tool):创建timed包相关时间操作工具,实现时间戳、时间格式化操作。创建timed测试类
This commit is contained in:
@@ -17,3 +17,5 @@
|
|||||||
2020/4/30 : 创建ArrayUtil.go,添加数组对应方法
|
2020/4/30 : 创建ArrayUtil.go,添加数组对应方法
|
||||||
|
|
||||||
2020/5/6 : 创建ArrayUtils测试类,修改对应方法
|
2020/5/6 : 创建ArrayUtils测试类,修改对应方法
|
||||||
|
|
||||||
|
2020/5/7 : 创建timed包相关时间操作工具,实现时间戳、时间格式化操作。创建timed测试类
|
||||||
@@ -58,6 +58,7 @@ func TestBool(t *testing.T) {
|
|||||||
fmt.Println(array.IsSubSet(mm, mm1))
|
fmt.Println(array.IsSubSet(mm, mm1))
|
||||||
fmt.Println(array.IsSubSet(mm, mm2))
|
fmt.Println(array.IsSubSet(mm, mm2))
|
||||||
fmt.Println(array.IsSubSet(mm, nn))
|
fmt.Println(array.IsSubSet(mm, nn))
|
||||||
|
fmt.Printf("\n\n")
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,56 @@
|
|||||||
|
/*
|
||||||
|
* @Author : huangzj
|
||||||
|
* @Time : 2020/5/7 11:27
|
||||||
|
* @Description:
|
||||||
|
*/
|
||||||
|
|
||||||
|
package timed
|
||||||
|
|
||||||
|
import (
|
||||||
|
"Go-Tool/util/timed"
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestTimeIntervalUtil(t *testing.T) {
|
||||||
|
fmt.Print("\n\n")
|
||||||
|
fmt.Print(timed.GetTimeDefaultFormatString(timed.GetSecondsBefore(time.Now().Unix(), 10)))
|
||||||
|
fmt.Print("\n\n")
|
||||||
|
fmt.Print(timed.GetTimeDefaultFormatString(timed.GetSecondsAfter(time.Now().Unix(), 10)))
|
||||||
|
fmt.Print("\n\n")
|
||||||
|
fmt.Print(timed.GetTimeDefaultFormatString(timed.GetMinutesBefore(time.Now().Unix(), 10)))
|
||||||
|
fmt.Print("\n\n")
|
||||||
|
fmt.Print(timed.GetTimeDefaultFormatString(timed.GetMinutesAfter(time.Now().Unix(), 10)))
|
||||||
|
fmt.Print("\n\n")
|
||||||
|
fmt.Print(timed.GetTimeDefaultFormatString(timed.GetHoursBefore(time.Now().Unix(), 10)))
|
||||||
|
fmt.Print("\n\n")
|
||||||
|
fmt.Print(timed.GetTimeDefaultFormatString(timed.GetHoursAfter(time.Now().Unix(), 10)))
|
||||||
|
fmt.Print("\n\n")
|
||||||
|
fmt.Print(timed.GetTimeDefaultFormatString(timed.GetDaysBefore(time.Now().Unix(), 10)))
|
||||||
|
fmt.Print("\n\n")
|
||||||
|
fmt.Print(timed.GetTimeDefaultFormatString(timed.GetDaysAfter(time.Now().Unix(), 10)))
|
||||||
|
fmt.Print("\n\n")
|
||||||
|
fmt.Print(timed.GetTimeDefaultFormatString(timed.GetMonthsBefore(time.Now().Unix(), 10)))
|
||||||
|
fmt.Print("\n\n")
|
||||||
|
fmt.Print(timed.GetTimeDefaultFormatString(timed.GetMonthsAfter(time.Now().Unix(), 10)))
|
||||||
|
fmt.Print("\n\n")
|
||||||
|
fmt.Print(timed.GetTimeDefaultFormatString(timed.GetYearsBefore(time.Now().Unix(), 10)))
|
||||||
|
fmt.Print("\n\n")
|
||||||
|
fmt.Print(timed.GetTimeDefaultFormatString(timed.GetYearsAfter(time.Now().Unix(), 10)))
|
||||||
|
fmt.Print("\n\n")
|
||||||
|
o1 := timed.GetIntervalBetweenTimes(time.Now().Unix(), time.Now().AddDate(3, 2, 5).Add(5*time.Hour).Add(7*time.Minute).Add(10*time.Second).Unix())
|
||||||
|
o2 := timed.GetIntervalBetweenTimes(time.Now().AddDate(3, 2, 5).Add(5*time.Hour).Add(7*time.Minute).Add(10*time.Second).Unix(), time.Now().Unix())
|
||||||
|
o3 := timed.GetIntervalBetweenTimes(time.Now().Unix(), time.Now().AddDate(3, -2, 5).Add(5*time.Hour).Add(7*time.Minute).Add(10*time.Second).Unix())
|
||||||
|
|
||||||
|
fmt.Println(o1.IntervalYear, " y ", o1.IntervalMonth, " m ", o1.IntervalDay, " d ", o1.IntervalHour, " H ", o1.IntervalMinute, " mm ", o1.IntervalSecond, " ss ")
|
||||||
|
fmt.Println(o2.IntervalYear, " y ", o2.IntervalMonth, " m ", o2.IntervalDay, " d ", o2.IntervalHour, " H ", o2.IntervalMinute, " mm ", o2.IntervalSecond, " ss ")
|
||||||
|
fmt.Println(o3.IntervalYear, " y ", o3.IntervalMonth, " m ", o3.IntervalDay, " d ", o3.IntervalHour, " H ", o3.IntervalMinute, " mm ", o3.IntervalSecond, " ss ")
|
||||||
|
o4 := timed.GetIntervalBetweenTimesDetail(time.Now().Unix(), time.Now().AddDate(3, -2, 5).Add(5*time.Hour).Add(7*time.Minute).Add(10*time.Second).Unix())
|
||||||
|
o5 := timed.GetIntervalBetweenTimesDetail(time.Now().Unix(), time.Now().Unix())
|
||||||
|
o6 := timed.GetIntervalBetweenTimesDetail(time.Now().Unix(), time.Now().AddDate(3, 2, 5).Add(5*time.Hour).Add(7*time.Minute).Add(10*time.Second).Unix())
|
||||||
|
|
||||||
|
fmt.Println(o4.Year, " y ", o4.Month, " m ", o4.Week, " w ", o4.Day, " d ", o4.Hour, " H ", o4.Minute, " mm ", o4.Second, " ss ")
|
||||||
|
fmt.Println(o5.Year, " y ", o5.Month, " m ", o5.Week, " w ", o5.Day, " d ", o5.Hour, " H ", o5.Minute, " mm ", o5.Second, " ss ")
|
||||||
|
fmt.Println(o6.Year, " y ", o6.Month, " m ", o6.Week, " w ", o6.Day, " d ", o6.Hour, " H ", o6.Minute, " mm ", o6.Second, " ss ")
|
||||||
|
}
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
/*
|
||||||
|
* @Author : huangzj
|
||||||
|
* @Time : 2020/5/7 13:35
|
||||||
|
* @Description:
|
||||||
|
*/
|
||||||
|
|
||||||
|
package timed
|
||||||
|
|
||||||
|
import (
|
||||||
|
"Go-Tool/util/timed"
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestTimeCompareUtil(t *testing.T) {
|
||||||
|
fmt.Print("\n\n")
|
||||||
|
fmt.Println(timed.IsBefore(time.Now().Unix(), time.Now().AddDate(0, 0, 1).Unix()))
|
||||||
|
fmt.Println(timed.IsBefore(time.Now().Unix(), time.Now().AddDate(0, 0, -1).Unix()))
|
||||||
|
|
||||||
|
fmt.Print("\n\n")
|
||||||
|
fmt.Println(timed.IsAfter(time.Now().Unix(), time.Now().AddDate(0, 0, -1).Unix()))
|
||||||
|
fmt.Println(timed.IsAfter(time.Now().Unix(), time.Now().AddDate(0, 0, 1).Unix()))
|
||||||
|
|
||||||
|
fmt.Print("\n\n")
|
||||||
|
fmt.Println(timed.IsSameTime(time.Now().Unix(), time.Now().Unix()))
|
||||||
|
fmt.Println(timed.IsSameTime(time.Now().Unix(), time.Now().AddDate(0, 0, -1).Unix()))
|
||||||
|
|
||||||
|
fmt.Print("\n\n")
|
||||||
|
fmt.Println(timed.IsDiffTime(time.Now().Unix(), time.Now().Unix()))
|
||||||
|
fmt.Println(timed.IsDiffTime(time.Now().Unix(), time.Now().AddDate(0, 0, -1).Unix()))
|
||||||
|
|
||||||
|
fmt.Print("\n\n")
|
||||||
|
fmt.Println(timed.IsBetween(time.Now().Unix(), time.Now().Unix(), time.Now().Unix()))
|
||||||
|
fmt.Println(timed.IsBetween(time.Now().Unix(), time.Now().AddDate(0, 0, -1).Unix(), time.Now().AddDate(0, 0, 1).Unix()))
|
||||||
|
fmt.Println(timed.IsBetween(time.Now().Unix(), time.Now().AddDate(0, 0, 1).Unix(), time.Now().AddDate(0, 0, -1).Unix()))
|
||||||
|
}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
/*
|
||||||
|
* @Author : huangzj
|
||||||
|
* @Time : 2020/5/7 10:04
|
||||||
|
* @Description:
|
||||||
|
*/
|
||||||
|
|
||||||
|
package timed
|
||||||
|
|
||||||
|
import (
|
||||||
|
"Go-Tool/util/timed"
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestTimeFormatUtil(t *testing.T) {
|
||||||
|
fmt.Print("\n\n")
|
||||||
|
fmt.Print(timed.GetTimeDefaultFormatString(timed.GetNowYearUnix()))
|
||||||
|
fmt.Print("\n\n")
|
||||||
|
fmt.Print(timed.GetTimeFormatString(timed.GetNowHourUnix(), time.ANSIC))
|
||||||
|
fmt.Print("\n\n")
|
||||||
|
fmt.Print(timed.GetNowDefaultFormatString())
|
||||||
|
fmt.Print("\n\n")
|
||||||
|
fmt.Println(timed.GetMonthFormatString(timed.GetNowHourUnix()))
|
||||||
|
fmt.Print("\n\n")
|
||||||
|
fmt.Println(timed.GetSimpleTimeFormatString(time.Now().Unix()))
|
||||||
|
}
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
/*
|
||||||
|
* @Author : huangzj
|
||||||
|
* @Time : 2020/5/7 9:39
|
||||||
|
* @Description:
|
||||||
|
*/
|
||||||
|
|
||||||
|
package timed
|
||||||
|
|
||||||
|
import (
|
||||||
|
"Go-Tool/util/timed"
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestTimeStampUtil(t *testing.T) {
|
||||||
|
fmt.Print("\n\n")
|
||||||
|
fmt.Println(timed.GetNowHourUnix())
|
||||||
|
|
||||||
|
fmt.Print("\n\n")
|
||||||
|
fmt.Println(timed.GetNowUnix())
|
||||||
|
|
||||||
|
fmt.Print("\n\n")
|
||||||
|
fmt.Println(timed.GetZeroHourUnix())
|
||||||
|
|
||||||
|
fmt.Print("\n\n")
|
||||||
|
fmt.Println(timed.GetNowYearUnix())
|
||||||
|
|
||||||
|
fmt.Print("\n\n")
|
||||||
|
fmt.Println(timed.GetNowMonthUnix())
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
/*
|
||||||
|
* @Author : huangzj
|
||||||
|
* @Time : 2020/5/7 12:00
|
||||||
|
* @Description: 时间比较工具
|
||||||
|
*/
|
||||||
|
|
||||||
|
package timed
|
||||||
|
|
||||||
|
func IsBefore(timestamp, compareStamp int64) bool {
|
||||||
|
return timestamp < compareStamp
|
||||||
|
}
|
||||||
|
|
||||||
|
func IsAfter(timestamp, compareStamp int64) bool {
|
||||||
|
return timestamp > compareStamp
|
||||||
|
}
|
||||||
|
|
||||||
|
func IsSameTime(timestamp, compareStamp int64) bool {
|
||||||
|
return timestamp == compareStamp
|
||||||
|
}
|
||||||
|
|
||||||
|
func IsDiffTime(timestamp, compareStamp int64) bool {
|
||||||
|
return !IsSameTime(timestamp, compareStamp)
|
||||||
|
}
|
||||||
|
|
||||||
|
func IsBetween(timeStamp, compareBefore, compareAfter int64) bool {
|
||||||
|
return timeStamp >= compareBefore && timeStamp <= compareAfter
|
||||||
|
}
|
||||||
@@ -0,0 +1,53 @@
|
|||||||
|
/*
|
||||||
|
* @Author : huangzj
|
||||||
|
* @Time : 2020/5/7 9:51
|
||||||
|
* @Description: 时间戳格式化工具
|
||||||
|
*/
|
||||||
|
|
||||||
|
package timed
|
||||||
|
|
||||||
|
import (
|
||||||
|
"Go-Tool/util/timed/enum"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
const defaultFormat = "2006-01-02 15:04:05"
|
||||||
|
const simpleFormat = "15:04:05"
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 获取对应时间的传入格式对应的字符串时间格式
|
||||||
|
*/
|
||||||
|
func GetTimeFormatString(timestamp int64, format string) string {
|
||||||
|
tm := time.Unix(timestamp, 0)
|
||||||
|
return tm.Format(format)
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 获取对应时间的默认格式的时间字符串.
|
||||||
|
*/
|
||||||
|
func GetTimeDefaultFormatString(timestamp int64) string {
|
||||||
|
return GetTimeFormatString(timestamp, defaultFormat)
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 获取当前时间的默认格式的字符串
|
||||||
|
*/
|
||||||
|
func GetNowDefaultFormatString() string {
|
||||||
|
return GetTimeFormatString(time.Now().Unix(), defaultFormat)
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 获取时间月份对应的月份信息
|
||||||
|
*/
|
||||||
|
func GetMonthFormatString(timestamp int64) string {
|
||||||
|
tm := time.Unix(timestamp, 0)
|
||||||
|
return enum.MonthMap[tm.Month().String()]
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 获取时间的简单格式,比如: 小时:分钟:秒钟
|
||||||
|
*/
|
||||||
|
func GetSimpleTimeFormatString(timestamp int64) string {
|
||||||
|
tm := time.Unix(timestamp, 0)
|
||||||
|
return GetTimeFormatString(tm.Unix(), simpleFormat)
|
||||||
|
}
|
||||||
@@ -0,0 +1,211 @@
|
|||||||
|
/*
|
||||||
|
* @Author : huangzj
|
||||||
|
* @Time : 2020/5/7 10:39
|
||||||
|
* @Description: 获取时间间隔工具类(这边只返回时间戳,如果需要格式化,参考TimeFormatUtil.go)
|
||||||
|
*/
|
||||||
|
|
||||||
|
package timed
|
||||||
|
|
||||||
|
import (
|
||||||
|
"Go-Tool/util/timed/vo"
|
||||||
|
"math"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
//年月日等对应的秒数
|
||||||
|
var byTime = []int64{365 * 24 * 60 * 60, 30 * 24 * 60 * 60, 7 * 24 * 60 * 60, 24 * 60 * 60, 60 * 60, 60, 1}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 获取多少秒之前的时间戳
|
||||||
|
*/
|
||||||
|
func GetSecondsBefore(timestamp int64, seconds int) int64 {
|
||||||
|
tm := time.Unix(timestamp, 0)
|
||||||
|
return tm.Unix() - int64(seconds)
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 获取多少秒之后的时间戳
|
||||||
|
*/
|
||||||
|
func GetSecondsAfter(timestamp int64, seconds int) int64 {
|
||||||
|
tm := time.Unix(timestamp, 0)
|
||||||
|
return tm.Unix() + int64(seconds)
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 获取多少分钟之前的时间戳
|
||||||
|
*/
|
||||||
|
func GetMinutesBefore(timestamp int64, minutes int) int64 {
|
||||||
|
tm := time.Unix(timestamp, 0)
|
||||||
|
return tm.Add(time.Minute * time.Duration(-minutes)).Unix()
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 获取多少分钟之后的时间戳
|
||||||
|
*/
|
||||||
|
func GetMinutesAfter(timestamp int64, minutes int) int64 {
|
||||||
|
tm := time.Unix(timestamp, 0)
|
||||||
|
return tm.Add(time.Minute * time.Duration(minutes)).Unix()
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 获取多少小时之前的时间戳
|
||||||
|
*/
|
||||||
|
func GetHoursBefore(timestamp int64, hours int) int64 {
|
||||||
|
tm := time.Unix(timestamp, 0)
|
||||||
|
return tm.Add(time.Hour * time.Duration(-hours)).Unix()
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 获取多少小时之后的时间戳
|
||||||
|
*/
|
||||||
|
func GetHoursAfter(timestamp int64, hours int) int64 {
|
||||||
|
tm := time.Unix(timestamp, 0)
|
||||||
|
return tm.Add(time.Hour * time.Duration(hours)).Unix()
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 获取多少天之前的时间戳
|
||||||
|
*/
|
||||||
|
func GetDaysBefore(timestamp int64, days int) int64 {
|
||||||
|
tm := time.Unix(timestamp, 0)
|
||||||
|
return tm.AddDate(0, 0, -days).Unix()
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 获取多少天之后的时间戳
|
||||||
|
*/
|
||||||
|
func GetDaysAfter(timestamp int64, days int) int64 {
|
||||||
|
tm := time.Unix(timestamp, 0)
|
||||||
|
return tm.AddDate(0, 0, days).Unix()
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 获取多少个月之前的时间戳
|
||||||
|
*/
|
||||||
|
func GetMonthsBefore(timestamp int64, months int) int64 {
|
||||||
|
tm := time.Unix(timestamp, 0)
|
||||||
|
return tm.AddDate(0, -months, 0).Unix()
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 获取多少个月之后的时间戳
|
||||||
|
*/
|
||||||
|
func GetMonthsAfter(timestamp int64, months int) int64 {
|
||||||
|
tm := time.Unix(timestamp, 0)
|
||||||
|
return tm.AddDate(0, months, 0).Unix()
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 获取多少年之前的时间戳
|
||||||
|
*/
|
||||||
|
func GetYearsBefore(timestamp int64, years int) int64 {
|
||||||
|
tm := time.Unix(timestamp, 0)
|
||||||
|
return tm.AddDate(-years, 0, 0).Unix()
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 获取多少年之后的时间戳
|
||||||
|
*/
|
||||||
|
func GetYearsAfter(timestamp int64, years int) int64 {
|
||||||
|
tm := time.Unix(timestamp, 0)
|
||||||
|
return tm.AddDate(years, 0, 0).Unix()
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* @param timestamp 时间戳
|
||||||
|
* @param stamp 时间戳
|
||||||
|
* @description 获取两个时间戳之间相差的年月日时分秒(这边每个参数分别是独立的不相关联)
|
||||||
|
*/
|
||||||
|
func GetIntervalBetweenTimes(timestamp, stamp int64) vo.IntervalObj {
|
||||||
|
//时间大小保证前小后大
|
||||||
|
if timestamp > stamp {
|
||||||
|
timestamp, stamp = stamp, timestamp
|
||||||
|
}
|
||||||
|
timeBefore := time.Unix(timestamp, 0)
|
||||||
|
timeAfter := time.Unix(stamp, 0)
|
||||||
|
m := timeAfter.Sub(timeBefore)
|
||||||
|
month, year := subMonth(timeBefore, timeAfter)
|
||||||
|
return vo.IntervalObj{
|
||||||
|
IntervalYear: year,
|
||||||
|
IntervalMonth: month,
|
||||||
|
IntervalDay: m.Hours() / 24,
|
||||||
|
IntervalHour: m.Hours(),
|
||||||
|
IntervalMinute: m.Minutes(),
|
||||||
|
IntervalSecond: m.Seconds(),
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* @param timestamp 时间戳
|
||||||
|
* @param stamp 时间戳
|
||||||
|
* @description 获取两个时间戳之间相差的年月日时分秒(对象的所有属性组成对应的相差时间,不是分开表示的)
|
||||||
|
*/
|
||||||
|
func GetIntervalBetweenTimesDetail(timestamp, stamp int64) vo.IntervalTimeObj {
|
||||||
|
var obj vo.IntervalTimeObj
|
||||||
|
//时间大小保证前小后大
|
||||||
|
if timestamp > stamp {
|
||||||
|
timestamp, stamp = stamp, timestamp
|
||||||
|
}
|
||||||
|
ct := stamp - timestamp
|
||||||
|
for i := 0; i < len(byTime); i++ {
|
||||||
|
//如果小于的话,采用直接默认值为0.所以这边直接continue
|
||||||
|
if ct < byTime[i] {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
var temp = math.Floor(float64(ct / byTime[i]))
|
||||||
|
ct = ct % byTime[i]
|
||||||
|
if temp > 0 {
|
||||||
|
makeUpObj(&obj, i, int(temp))
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return obj
|
||||||
|
}
|
||||||
|
|
||||||
|
func makeUpObj(obj *vo.IntervalTimeObj, i int, f int) {
|
||||||
|
switch i {
|
||||||
|
case 0:
|
||||||
|
obj.Year = f
|
||||||
|
case 1:
|
||||||
|
obj.Month = f
|
||||||
|
case 2:
|
||||||
|
obj.Week = f
|
||||||
|
case 3:
|
||||||
|
obj.Day = f
|
||||||
|
case 4:
|
||||||
|
obj.Hour = f
|
||||||
|
case 5:
|
||||||
|
obj.Minute = f
|
||||||
|
case 6:
|
||||||
|
obj.Second = f
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func subMonth(timestamp, stamp time.Time) (month float64, year float64) {
|
||||||
|
// 计算日期相差多少月
|
||||||
|
y1 := timestamp.Year()
|
||||||
|
y2 := stamp.Year()
|
||||||
|
m1 := int(timestamp.Month())
|
||||||
|
m2 := int(stamp.Month())
|
||||||
|
d1 := timestamp.Day()
|
||||||
|
d2 := stamp.Day()
|
||||||
|
|
||||||
|
yearInterval := y2 - y1
|
||||||
|
// 如果 d1的 月-日 小于 d2的 月-日 那么 yearInterval-- 这样就得到了相差的年数
|
||||||
|
if m1 > m2 || (m1 == m2 && d1 > d2) {
|
||||||
|
yearInterval--
|
||||||
|
}
|
||||||
|
// 获取月数差值
|
||||||
|
monthInterval := m2 - m1
|
||||||
|
if m2 < m1 {
|
||||||
|
monthInterval += 12
|
||||||
|
}
|
||||||
|
if d1 > d2 {
|
||||||
|
monthInterval--
|
||||||
|
}
|
||||||
|
year = float64(yearInterval) + float64(monthInterval)/12
|
||||||
|
month = float64(yearInterval*12 + monthInterval)
|
||||||
|
return
|
||||||
|
}
|
||||||
@@ -0,0 +1,52 @@
|
|||||||
|
/*
|
||||||
|
* @Author : huangzj
|
||||||
|
* @Time : 2020/5/7 9:20
|
||||||
|
* @Description: 时间戳工具类
|
||||||
|
*/
|
||||||
|
|
||||||
|
package timed
|
||||||
|
|
||||||
|
import "time"
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 获取当月月初的时间戳
|
||||||
|
*/
|
||||||
|
func GetNowMonthUnix() int64 {
|
||||||
|
now := time.Now()
|
||||||
|
month := time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, now.Location())
|
||||||
|
return month.Unix()
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 获取当天零时的时间戳
|
||||||
|
*/
|
||||||
|
func GetZeroHourUnix() int64 {
|
||||||
|
now := time.Now()
|
||||||
|
zeroHour := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
|
||||||
|
return zeroHour.Unix()
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 获取当前时间戳
|
||||||
|
*/
|
||||||
|
func GetNowUnix() int64 {
|
||||||
|
return time.Now().Unix()
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 获取当前小时时间戳
|
||||||
|
*/
|
||||||
|
func GetNowHourUnix() int64 {
|
||||||
|
now := time.Now()
|
||||||
|
hour := time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), 0, 0, 0, now.Location())
|
||||||
|
return hour.Unix()
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 获取今年年初的时间戳
|
||||||
|
*/
|
||||||
|
func GetNowYearUnix() int64 {
|
||||||
|
now := time.Now()
|
||||||
|
tm := time.Date(now.Year(), 1, 1, 0, 0, 0, 0, now.Location())
|
||||||
|
return tm.Unix()
|
||||||
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
/*
|
||||||
|
* @Author : huangzj
|
||||||
|
* @Time : 2020/5/7 10:25
|
||||||
|
* @Description:
|
||||||
|
*/
|
||||||
|
|
||||||
|
package enum
|
||||||
|
|
||||||
|
var MonthMap map[string]string = map[string]string{
|
||||||
|
"January": "01",
|
||||||
|
"February": "02",
|
||||||
|
"March": "03",
|
||||||
|
"April": "04",
|
||||||
|
"May": "05",
|
||||||
|
"June": "06",
|
||||||
|
"July": "07",
|
||||||
|
"August": "08",
|
||||||
|
"September": "09",
|
||||||
|
"October": "10",
|
||||||
|
"November": "11",
|
||||||
|
"December": "12",
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
/*
|
||||||
|
* @Author : huangzj
|
||||||
|
* @Time : 2020/5/7 13:54
|
||||||
|
* @Description:
|
||||||
|
*/
|
||||||
|
|
||||||
|
package vo
|
||||||
|
|
||||||
|
type IntervalObj struct {
|
||||||
|
IntervalYear float64 //间隔年数
|
||||||
|
IntervalMonth float64 //间隔月数
|
||||||
|
IntervalDay float64 //间隔天数
|
||||||
|
IntervalHour float64 //间隔小时数
|
||||||
|
IntervalMinute float64 //间隔分钟数
|
||||||
|
IntervalSecond float64 //间隔秒数
|
||||||
|
}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
/*
|
||||||
|
* @Author : huangzj
|
||||||
|
* @Time : 2020/5/7 15:19
|
||||||
|
* @Description:
|
||||||
|
*/
|
||||||
|
|
||||||
|
package vo
|
||||||
|
|
||||||
|
type IntervalTimeObj struct {
|
||||||
|
Year int
|
||||||
|
Month int
|
||||||
|
Week int
|
||||||
|
Day int
|
||||||
|
|
||||||
|
Hour int
|
||||||
|
Minute int
|
||||||
|
Second int
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user