GO 对常用时间函数进行一些封装

2 篇文章 0 订阅
1 篇文章 0 订阅
package utils

import (
	"strings"
	"time"
)

type TimeUtil struct {
}

//获取 nowTime(时间戳)月开始时间戳
func (t *TimeUtil) GetMonthBeginTimeStamp(nowTime int64) int64 {
	thisTime := time.Unix(nowTime, 0).In(time.FixedZone("CST", 8*3600))
	return time.Date(thisTime.Year(), thisTime.Month(), 1, 0, 0, 0, 0, thisTime.Location()).Unix()
}

//获取 nowTime(时间戳)月结束时间戳
func (t *TimeUtil) GetMonthEndTimeStamp(nowTime int64) int64 {
	thisTime := time.Unix(nowTime, 0).In(time.FixedZone("CST", 8*3600))
	return time.Date(thisTime.Year(), thisTime.Month()+1, 0, 23, 59, 59, 59, thisTime.Location()).Unix()
}

//获取 nowTime(时间戳)日开始时间戳
func (t *TimeUtil) GetDayBeginTimeStamp(nowTime int64) int64 {
	thisTime := time.Unix(nowTime, 0).In(time.FixedZone("CST", 8*3600))
	return time.Date(thisTime.Year(), thisTime.Month(), thisTime.Day(), 0, 0, 0, 0, thisTime.Location()).Unix()
}

//获取 nowTime(时间戳)日结束时间戳
func (t *TimeUtil) GetDayEndTimeStamp(nowTime int64) int64 {
	thisTime := time.Unix(nowTime, 0).In(time.FixedZone("CST", 8*3600))
	return time.Date(thisTime.Year(), thisTime.Month(), thisTime.Day(), 23, 59, 59, 59, thisTime.Location()).Unix()
}

//根据输入模板 "Y-m-d H:i:s" or "Y-m-d" 来进行时间转换为时间戳
func (t *TimeUtil) TransformationTimeStamp(toBeCharge string, temp string) int64 {
	temp = strings.Replace(temp, " ", "", -1)
	timeLayout := "2006-01-02 15:04:05"

	if temp == "" || temp == "Y-m-d H:i:s" {
		timeLayout = "2006-01-02 15:04:05"
	} else if temp == "Y-m-d" {
		timeLayout = "2006-01-02"
	}

	theTime, _ := time.ParseInLocation(timeLayout, toBeCharge, time.FixedZone("CST", 8*3600)) //使用模板在对应时区转化为time.time类型
	return theTime.Unix()                                                                     //转化为时间戳 类型是int64
}

//根据输入模板 "Y-m-d H:i:s" or "Y-m-d" 来进行时间戳转换为时间
func (t *TimeUtil) TransformationTime(timeStamp int64, temp string) string {
	temp = strings.Replace(temp, " ", "", -1)
	timeLayout := "2006-01-02 15:04:05"

	if temp == "" || temp == "Y-m-d H:i:s" {
		timeLayout = "2006-01-02 15:04:05"
	} else if temp == "Y-m-d" {
		timeLayout = "2006-01-02"
	}

	thisTime := time.Unix(timeStamp, 0).In(time.FixedZone("CST", 8*3600))
	return thisTime.Format(timeLayout)
}

//获取当前时间戳
func (t *TimeUtil) GetNowTimeStamp() int64 {
	return time.Now().In(time.FixedZone("CST", 8*3600)).Unix()
}

//获取当前时间 (根据模板 "Y-m-d H:i:s" or "Y-m-d")
func (t *TimeUtil) GetNowTime(temp string) string {
	timeStamp := time.Now().In(time.FixedZone("CST", 8*3600)).Unix()
	temp = strings.Replace(temp, " ", "", -1)
	timeLayout := "2006-01-02 15:04:05"

	if temp == "" || temp == "Y-m-d H:i:s" {
		timeLayout = "2006-01-02 15:04:05"
	} else if temp == "Y-m-d" {
		timeLayout = "2006-01-02"
	}
	thisTime := time.Unix(timeStamp, 0).In(time.FixedZone("CST", 8*3600))
	return thisTime.Format(timeLayout)
}

//获取时间戳所在年
func (t *TimeUtil) GetYear(timeStamp int64) int {
	thisTime := time.Unix(timeStamp, 0).In(time.FixedZone("CST", 8*3600))
	return thisTime.Year()
}

//获取时间戳所在月
func (t *TimeUtil) GetMonth(timeStamp int64) int {
	thisTime := time.Unix(timeStamp, 0).In(time.FixedZone("CST", 8*3600))
	return int(thisTime.Month())
}

//获取时间戳所在日
func (t *TimeUtil) GetDay(timeStamp int64) int {
	thisTime := time.Unix(timeStamp, 0).In(time.FixedZone("CST", 8*3600))
	return thisTime.Day()
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值