1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
| type timex struct { time.Time }
func newDate(year int, month time.Month, day int, hour int, min int, sec int, nsec int, loc *time.Location) timex { return timex{time.Date(year, month, day, hour, min, sec, nsec, loc)} }
func (tx timex) whichWeek() int {
tm1 := time.Date(tx.Year(), tx.Month(), tx.Day()+3-(int(tx.Weekday())+6)%7, 0, 0, 0, 0, time.UTC) week1 := time.Date(tm1.Year(), 1, 4, 0, 0, 0, 0, time.UTC) abs := 1 + math.Round((float64((tm1.Unix()-week1.Unix())/86400)-float64(3)+float64((int(week1.Weekday())+6)%7))/7) return int(abs) }
func (tx timex) whichWeek2() int {
tm1 := time.Date(tx.Year(), tx.Month(), tx.Day()+4-int(tx.Weekday()), 0, 0, 0, 0, time.UTC) yearStart := time.Date(tm1.Year(), 1, 1, 0, 0, 0, 0, time.UTC) sd := math.Ceil((float64((tm1.Unix()-yearStart.Unix())/86400) + 1) / 7)
return int(sd) }
func (tx timex) WeekStart(year, week int) time.Time { if year == 0 || week == 0 { year = tx.Year() week = int(tx.Weekday()) }
t2 := newDate(year, 7, 1, 0, 0, 0, 0, time.UTC)
if wd := t2.Weekday(); wd != time.Sunday { t2.Time = t2.AddDate(0, 0, -int(wd)) }
w := t2.whichWeek2() t2.Time = t2.AddDate(0, 0, (week-w)*7)
if wd := t2.Weekday(); wd != time.Sunday { t2.Time = t2.AddDate(0, 0, -6) }
return t2.Time }
func WeekStart(year, week int) time.Time { t := time.Date(year, 7, 1, 0, 0, 0, 0, time.UTC)
if wd := t.Weekday(); wd == time.Sunday { t = t.AddDate(0, 0, -6) } else { t = t.AddDate(0, 0, -int(wd)+1) }
_, w := t.ISOWeek() t = t.AddDate(0, 0, (week-w)*7)
if wd := t.Weekday(); wd != time.Sunday { t = t.AddDate(0, 0, -6) } else { t = t.AddDate(0, 0, -int(wd)+1) }
return t }
|