init: web_hgcg_server 初始化提交
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,286 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
"videoweb/pub/pubdef"
|
||||
)
|
||||
|
||||
var (
|
||||
LocBJ, _ = time.LoadLocation("Asia/Shanghai")
|
||||
)
|
||||
|
||||
func init() {
|
||||
time.Local = LocBJ
|
||||
}
|
||||
|
||||
func Now() time.Time {
|
||||
return time.Now().Local()
|
||||
}
|
||||
|
||||
func NowMillisecond() int64 {
|
||||
return Now().UnixNano() / int64(time.Millisecond)
|
||||
}
|
||||
|
||||
func BeginOfToday() time.Time {
|
||||
t := Now()
|
||||
return BeginOfTime(t)
|
||||
}
|
||||
|
||||
func BeginOfTime(t time.Time) time.Time {
|
||||
year, month, day := t.Date()
|
||||
return time.Date(year, month, day, 0, 0, 0, 0, t.Location())
|
||||
}
|
||||
|
||||
func GetNextMonthTime(t time.Time) time.Time {
|
||||
year, month, day := t.Date()
|
||||
return time.Date(year, month+1, day, 0, 0, 0, 0, t.Location())
|
||||
}
|
||||
func GetNextQuarterTime(t time.Time) time.Time {
|
||||
year, month, day := t.Date()
|
||||
return time.Date(year, month+3, day, 0, 0, 0, 0, t.Location())
|
||||
}
|
||||
|
||||
func GetNextYearTime(t time.Time) time.Time {
|
||||
year, month, day := t.Date()
|
||||
return time.Date(year+1, month, day, 0, 0, 0, 0, t.Location())
|
||||
}
|
||||
|
||||
func GetForeverTime() time.Time {
|
||||
t := time.Now()
|
||||
year, month, day := t.Date()
|
||||
return time.Date(year+999, month, day, 0, 0, 0, 0, t.Location())
|
||||
}
|
||||
|
||||
func IsoWeek() string {
|
||||
year, week := time.Now().ISOWeek()
|
||||
return fmt.Sprintf("%d%02d", year, week)
|
||||
}
|
||||
|
||||
// Deprecated
|
||||
func EndOfToday() time.Time {
|
||||
t := Now()
|
||||
return EndOfTime(t)
|
||||
}
|
||||
|
||||
func EndOfTime(t time.Time) time.Time {
|
||||
year, month, day := t.Date()
|
||||
return time.Date(year, month, day, 23, 59, 59, 0, t.Location())
|
||||
}
|
||||
|
||||
func GetDateString(t time.Time) string {
|
||||
return t.In(LocBJ).Format(pubdef.DateISO)
|
||||
}
|
||||
|
||||
func GetTodayDate(now time.Time) string {
|
||||
return GetDateString(now.In(time.Local))
|
||||
}
|
||||
|
||||
func GetYesterdayDate(now time.Time) string {
|
||||
return GetDateString(now.Local().AddDate(0, 0, -1))
|
||||
}
|
||||
|
||||
func GetDayBeforeYesterdayDate(now time.Time) string {
|
||||
return GetDateString(now.Local().AddDate(0, 0, -2))
|
||||
}
|
||||
|
||||
func GetDayRange(t time.Time) (dayStart time.Time, dayEnd time.Time) {
|
||||
dayStart = BeginOfTime(t)
|
||||
dayEnd = EndOfTime(t)
|
||||
return
|
||||
}
|
||||
|
||||
// GetDayRangeByDate 返回一天的开始和结束时间, 00:00:00 - 23:59:59
|
||||
func GetDayRangeByDate(date string) (dayStart time.Time, dayEnd time.Time, err error) {
|
||||
var d time.Time
|
||||
d, err = time.ParseInLocation(pubdef.DateISO, date, LocBJ)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
dayStart, dayEnd = GetDayRange(d)
|
||||
return
|
||||
}
|
||||
|
||||
func GetDayEnd(date string) time.Time {
|
||||
d, _ := time.ParseInLocation(pubdef.DateISO, date, LocBJ)
|
||||
return EndOfTime(d)
|
||||
}
|
||||
|
||||
func GetDayBegin(date string) time.Time {
|
||||
d, _ := time.ParseInLocation(pubdef.DateISO, date, LocBJ)
|
||||
return BeginOfTime(d)
|
||||
}
|
||||
|
||||
func GetDayEndByTime(date string) time.Time {
|
||||
d, _ := time.ParseInLocation(pubdef.TimeISO, date, LocBJ)
|
||||
return EndOfTime(d)
|
||||
}
|
||||
|
||||
func GetDayBeginByTime(date string) time.Time {
|
||||
d, _ := time.ParseInLocation(pubdef.TimeISO, date, LocBJ)
|
||||
return BeginOfTime(d)
|
||||
}
|
||||
|
||||
func BeginWithThisMonth() time.Time {
|
||||
t := Now()
|
||||
year, month, _ := t.Date()
|
||||
return time.Date(year, month, 1, 0, 0, 0, 0, t.Location())
|
||||
}
|
||||
|
||||
func BeginOfMonth(t time.Time) time.Time {
|
||||
year, month, _ := t.In(time.Local).Date()
|
||||
return time.Date(year, month, 1, 0, 0, 0, 0, t.Location())
|
||||
}
|
||||
|
||||
func EndOfMonth(t time.Time) time.Time {
|
||||
return BeginOfMonth(t).AddDate(0, 1, 0).Add(-time.Nanosecond)
|
||||
}
|
||||
|
||||
func EndOfLastMonth(t time.Time) time.Time {
|
||||
return BeginOfMonth(t).Add(-time.Nanosecond)
|
||||
}
|
||||
|
||||
func EndOfLastMonthDate(t time.Time) string {
|
||||
return GetDateString(EndOfLastMonth(t))
|
||||
}
|
||||
|
||||
func EndOfLastWeek(t time.Time) time.Time {
|
||||
return BeginOfWeekByTime(t).Add(-time.Nanosecond)
|
||||
}
|
||||
|
||||
func EndOfLastWeekDate(t time.Time) string {
|
||||
return GetDateString(EndOfLastWeek(t))
|
||||
}
|
||||
|
||||
// Deprecated
|
||||
func BeginOfWeek() time.Time {
|
||||
return BeginOfWeekByTime(Now())
|
||||
}
|
||||
|
||||
func WeekDateRange(t time.Time) (string, string) {
|
||||
b := BeginOfWeekByTime(t)
|
||||
e := EndOfWeekByTime(t)
|
||||
return GetDateString(b), GetDateString(e)
|
||||
}
|
||||
|
||||
func MonthDateRange(t time.Time) (string, string) {
|
||||
b := BeginOfMonth(t)
|
||||
e := EndOfMonth(t)
|
||||
return GetDateString(b), GetDateString(e)
|
||||
}
|
||||
|
||||
func BeginOfWeekByTime(t time.Time) time.Time {
|
||||
bt := BeginOfTime(t)
|
||||
weekday := int(t.Weekday())
|
||||
weekStartDayInt := int(time.Monday)
|
||||
if weekday < weekStartDayInt {
|
||||
weekday = weekday + 7 - weekStartDayInt
|
||||
} else {
|
||||
weekday = weekday - weekStartDayInt
|
||||
}
|
||||
return bt.AddDate(0, 0, -weekday)
|
||||
}
|
||||
|
||||
func EndOfWeekByTime(t time.Time) time.Time {
|
||||
return BeginOfWeekByTime(t).AddDate(0, 0, 7).Add(-time.Nanosecond)
|
||||
}
|
||||
|
||||
func BeginTodayMillsecond() int64 {
|
||||
return BeginOfToday().UnixNano() / int64(time.Millisecond)
|
||||
}
|
||||
|
||||
// ServerCurrMillisecond 获得时间毫秒数 13位
|
||||
func ServerCurrMillisecond(t time.Time) int64 {
|
||||
return t.UnixNano() / 1000000
|
||||
}
|
||||
|
||||
func BeginWithTime(t time.Time) time.Time {
|
||||
year, month, day := t.Date()
|
||||
return time.Date(year, month, day, 0, 0, 0, 0, t.Location())
|
||||
}
|
||||
|
||||
func EndWithTime(t time.Time) time.Time {
|
||||
year, month, day := t.Date()
|
||||
return time.Date(year, month, day, 23, 59, 59, 0, t.Location())
|
||||
}
|
||||
|
||||
func IsToday(t time.Time) bool {
|
||||
if t.IsZero() {
|
||||
return false
|
||||
}
|
||||
today := Now()
|
||||
|
||||
return t.Year() == today.Year() &&
|
||||
t.Month() == today.Month() &&
|
||||
t.Day() == today.Day()
|
||||
}
|
||||
|
||||
func IsSameYearAndMonth(t1, t2 time.Time) bool {
|
||||
return t1.Year() == t2.Year() &&
|
||||
t1.Month() == t2.Month()
|
||||
}
|
||||
|
||||
// 获取月的第一天
|
||||
func GetFirstDateOfMonth(d time.Time) time.Time {
|
||||
d = d.AddDate(0, 0, -d.Day()+1)
|
||||
return GetZeroTime(d)
|
||||
}
|
||||
|
||||
// 获取月的最后一天
|
||||
func GetLastDateOfMonth(d time.Time) time.Time {
|
||||
return GetFirstDateOfMonth(d).AddDate(0, 1, -1)
|
||||
}
|
||||
|
||||
// 获取周的星期一(星期一~星期日)
|
||||
func GetMondayOfWeek(d time.Time) time.Time {
|
||||
offset := (int(d.Weekday()) + 6) % 7
|
||||
d = d.AddDate(0, 0, -offset)
|
||||
return GetZeroTime(d)
|
||||
}
|
||||
|
||||
// 获取周的星期日(星期一~星期日)
|
||||
func GetSundayOfWeek(d time.Time) time.Time {
|
||||
offset := 6 - (int(d.Weekday())+6)%7
|
||||
d = d.AddDate(0, 0, offset)
|
||||
return GetZeroTime(d)
|
||||
}
|
||||
|
||||
// 获取某一天的0点时间
|
||||
func GetZeroTime(d time.Time) time.Time {
|
||||
return time.Date(d.Year(), d.Month(), d.Day(), 0, 0, 0, 0, d.Location())
|
||||
}
|
||||
|
||||
// 获取当前整点时间
|
||||
func GetWholeTime(d time.Time) time.Time {
|
||||
return time.Date(d.Year(), d.Month(), d.Day(), d.Hour(), 0, 0, 0, d.Location())
|
||||
}
|
||||
|
||||
func MustParseTime(s string) time.Time {
|
||||
layouts := []string{
|
||||
time.RFC3339,
|
||||
pubdef.DateISO,
|
||||
pubdef.TimeISO,
|
||||
}
|
||||
for _, layout := range layouts {
|
||||
t, err := time.ParseInLocation(layout, s, time.Local)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
return t
|
||||
}
|
||||
panic(fmt.Errorf("cannot parse %s as time.Time", s))
|
||||
}
|
||||
|
||||
// 查看两个时间差了多少天,差一秒都算一天
|
||||
func TimeSub(t1, t2 time.Time) int64 {
|
||||
t1 = time.Date(t1.Year(), t1.Month(), t1.Day(), 0, 0, 0, 0, time.Local)
|
||||
t2 = time.Date(t2.Year(), t2.Month(), t2.Day(), 0, 0, 0, 0, time.Local)
|
||||
|
||||
return int64(t1.Sub(t2).Hours() / 24)
|
||||
}
|
||||
func RecentMinute(tim time.Time, scaleMinute int64) time.Time {
|
||||
minute := int64(tim.Minute())
|
||||
lave := minute % scaleMinute
|
||||
formatMinute := minute - lave
|
||||
return time.Date(tim.Year(), tim.Month(), tim.Day(), tim.Hour(), int(formatMinute), 0, 0, tim.Location())
|
||||
}
|
||||
Reference in New Issue
Block a user