1 Star 0 Fork 0

andrew.zhang / libgo

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
timeutil.go 3.80 KB
一键复制 编辑 原始数据 按行查看 历史
andrew.zhang 提交于 2022-10-31 06:14 . 整理以前代码中...
package zbutil
import (
"time"
)
const DateLayout = "2006-01-02"
const TimeLayoutm = "2006-01-02 15:04:05.000"
const TimeLayout = "2006-01-02 15:04:05"
const TimeLayoutM = "2006-01-02 15:04"
const HoMSLayout = "15:04:05"
const TimeInvalid = "2011-12-31 23:59:59"
const TimeBase = "2012-01-01 00:00:00" //这天是星期天,世界末日后新的纪元开始
var timeInvalid, _ = time.ParseInLocation(TimeLayout, TimeInvalid, time.Local)
var timeBase, _ = time.ParseInLocation(TimeLayout, TimeBase, time.Local)
// TimeIsValid 判断时间是不是统一的无效时间
func TimeIsValid(time time.Time) bool { return time.After(timeBase) }
// TimeGetInv 获得统一的无效时间常量
func TimeGetInv() time.Time { return timeInvalid }
// TimeAbs2Rel 绝对时间到相对时间
func TimeAbs2Rel(tm time.Time) uint32 {
d := tm.Sub(timeBase)
return IF(d <= 0, 0, uint32(d/time.Second))
}
func TimeStr2Rel(tm string) uint32 {
l_tm, err := time.ParseInLocation(TimeLayout, tm, time.Local)
if err != nil {
return 0
} else {
return TimeAbs2Rel(l_tm)
}
}
func Now() uint32 { return TimeAbs2Rel(time.Now()) }
func Now64() int64 { return TimeAbs2Rel64(time.Now()) }
func NowStr() string { return time.Now().Format(TimeLayout) }
func NowStr64() string { return time.Now().Format(TimeLayout + ".000") }
// TimeRel2Abs 相对时间到绝对时间
func TimeRel2Abs(rela uint32) time.Time { return timeBase.Add(time.Duration(rela) * time.Second) }
func TimeRel2Str(rela uint32) string { return TimeRel2Abs(rela).Format(TimeLayout) }
func TimeAbs2Str(abs time.Time) string { return abs.Format(TimeLayout) }
func TimeRel2Str64(rela int64) string { return TimeRel2Abs64(rela).Format(TimeLayout + ".000") }
func TimeAbs2Str64(abs time.Time) string { return abs.Format(TimeLayout + ".000") }
// TimeAbs2Rel64 绝对时间到相对时间
func TimeAbs2Rel64(tm time.Time) int64 { return int64(tm.Sub(timeBase) / time.Millisecond) }
// TimeRel2Abs64 相对时间到绝对时间
func TimeRel2Abs64(rela int64) time.Time { return timeBase.Add(time.Duration(rela) * time.Millisecond) }
// GetTodayWeekday 获取当前星期数(周日为7)
func GetTodayWeekday() int32 {
return int32((Now()/(24*3600)+6)%7 + 1)
}
// GetNextWeekdayZeroTime 获取最近一个周几的零点时间戳(周日为7)
func GetNextWeekdayZeroTime(weekday uint32) uint32 {
l_today := Now() / (24 * 3600)
w := (l_today+6)%7 + 1
w = (weekday + 7 - w) % 7
return (l_today + w) * 24 * 3600
}
// GetTodayZeroTime 获取今天零点时间戳
func GetTodayZeroTime() uint32 {
return Now() / (24 * 3600) * (24 * 3600)
}
// GetTomorrowZeroTime 获取明天零点时间戳
func GetTomorrowZeroTime() uint32 {
return GetTodayZeroTime() + 24*3600
}
// GetYesterdayZeroTime 获取昨天零点时间戳
func GetYesterdayZeroTime() uint32 {
return GetTodayZeroTime() - 24*3600
}
// GetWeekZeroTime 获取本周零点时间戳(周一)
func GetWeekZeroTime() uint32 {
l_today := Now() / (24 * 3600)
w := (l_today + 6) % 7
return (l_today - w) * 24 * 3600
}
// GetNextWeekZeroTime 获取下周零点时间戳(下周一)
func GetNextWeekZeroTime() uint32 {
return GetWeekZeroTime() + (7 * 24 * 3600)
}
// GetPreWeekZeroTime 获取上周零点时间戳(上周一)
func GetPreWeekZeroTime() uint32 {
return GetWeekZeroTime() - (7 * 24 * 3600)
}
// GetMonthZeroTime 获取本月零点时间戳
func GetMonthZeroTime() uint32 {
y, m, _ := (time.Now()).Date()
return TimeAbs2Rel(time.Date(y, m, 1, 0, 0, 0, 0, time.Local))
}
// GetNextMonthZeroTime 获取下月零点时间戳
func GetNextMonthZeroTime() uint32 {
y, m, _ := (time.Now()).Date()
return TimeAbs2Rel(time.Date(y, m+1, 1, 0, 0, 0, 0, time.Local))
}
// GetPreMonthZeroTime 获取上月零点时间戳
func GetPreMonthZeroTime() uint32 {
y, m, _ := (time.Now()).Date()
return TimeAbs2Rel(time.Date(y, m-1, 1, 0, 0, 0, 0, time.Local))
}
Go
1
https://gitee.com/andrewzh/libgo.git
git@gitee.com:andrewzh/libgo.git
andrewzh
libgo
libgo
v1.0.3

搜索帮助