feat(Go-Tool):创建timed包相关时间操作工具,实现时间戳、时间格式化操作。创建timed测试类

This commit is contained in:
huangzj
2020-05-07 16:14:37 +08:00
parent d448fbad5e
commit 24c319a182
13 changed files with 554 additions and 1 deletions
+27
View File
@@ -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
}
+53
View File
@@ -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)
}
+211
View File
@@ -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
}
+52
View File
@@ -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()
}
+22
View File
@@ -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",
}
+16
View File
@@ -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 //间隔秒数
}
+18
View File
@@ -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
}