|
|
@@ -79,6 +79,11 @@ func TimeParseStd(src string) time.Time { |
|
|
|
return t |
|
|
|
} |
|
|
|
|
|
|
|
func TimeParseDateStd(src string) time.Time { |
|
|
|
t, _ := TimeParse("2006-01-02", src) |
|
|
|
return t |
|
|
|
} |
|
|
|
|
|
|
|
func TimeStdParseUnix(src string) int64 { |
|
|
|
t, err := TimeParse("2006-01-02 15:04:05", src) |
|
|
|
if err != nil { |
|
|
@@ -245,3 +250,19 @@ func GetStartOfWeek(t time.Time) time.Time { |
|
|
|
func GetZeroTime(d time.Time) time.Time { |
|
|
|
return time.Date(d.Year(), d.Month(), d.Day(), 0, 0, 0, 0, d.Location()) |
|
|
|
} |
|
|
|
|
|
|
|
// GetWeekInfoByDate 获取周数、开始时间、结束时间 |
|
|
|
func GetWeekInfoByDate(dateStr string) (string, string, string, string) { |
|
|
|
date := TimeParseDateStd(dateStr) |
|
|
|
year, week := date.ISOWeek() |
|
|
|
location, _ := time.LoadLocation("Asia/Shanghai") |
|
|
|
// 计算给定年份1月1日是星期几 |
|
|
|
startOfYear := time.Date(year, time.January, 1, 0, 0, 0, 0, location) |
|
|
|
daysOffset := int(startOfYear.Weekday()) - int(time.Monday) + 1 |
|
|
|
// 计算给定年份的第一周的开始日期 |
|
|
|
firstWeekStart := startOfYear.AddDate(0, 0, -daysOffset+1) |
|
|
|
// 计算给定周的开始日期 |
|
|
|
weekStart := firstWeekStart.AddDate(0, 0, (week-1)*7) |
|
|
|
weekEnd := weekStart.AddDate(0, 0, 6) |
|
|
|
return IntToStr(year), IntToStr(week), weekStart.Format("2006-01-02"), weekEnd.Format("2006-01-02") |
|
|
|
} |