98 lines
1.6 KiB
Go
98 lines
1.6 KiB
Go
package main
|
|
|
|
import (
|
|
e "codeberg.org/pebbe/errors"
|
|
|
|
"fmt"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
var (
|
|
x = e.ExitErr
|
|
)
|
|
|
|
func main() {
|
|
aa := strings.Split(os.Args[1], "-")
|
|
if len(aa) != 2 {
|
|
x(fmt.Errorf("ongeldig argument, moet in formaat yyyy-dd zijn"))
|
|
}
|
|
|
|
year, err := strconv.Atoi(aa[0])
|
|
x(err)
|
|
week, err := strconv.Atoi(aa[1])
|
|
x(err)
|
|
|
|
weken, err := strconv.Atoi(os.Args[2])
|
|
|
|
if year < 1000 || year > 9999 {
|
|
x(fmt.Errorf("ongeldig year: %d", year))
|
|
}
|
|
if week < 1 || week > 53 {
|
|
x(fmt.Errorf("ongeldige week: %d", week))
|
|
}
|
|
|
|
// 15 januari van het jaar
|
|
t := time.Date(year, 1, 15, 12, 0, 0, 0, time.UTC)
|
|
|
|
// eerste gok
|
|
t = t.AddDate(0, 0, 7*week-14)
|
|
|
|
// zoek juiste week
|
|
var y, w int
|
|
for {
|
|
y, w = t.ISOWeek()
|
|
if y < year {
|
|
t = t.AddDate(0, 12, 0)
|
|
continue
|
|
}
|
|
if y > year {
|
|
t = t.AddDate(0, -12, 0)
|
|
continue
|
|
}
|
|
break
|
|
}
|
|
for {
|
|
y, w = t.ISOWeek()
|
|
if w < week {
|
|
t = t.AddDate(0, 0, 7)
|
|
continue
|
|
}
|
|
if w > week {
|
|
t = t.AddDate(0, 0, -7)
|
|
}
|
|
break
|
|
}
|
|
if y != year {
|
|
x(fmt.Errorf("ongeldige combinatie van week/jaar: %d/%d", week, year))
|
|
}
|
|
|
|
// zoek begin van de week
|
|
d := int(t.Weekday())
|
|
if d == 0 {
|
|
d = 7
|
|
}
|
|
t = t.AddDate(0, 0, 1-d)
|
|
|
|
// voorgaande weken en deze week
|
|
t2 := t.AddDate(0, 0, -7*(weken-1))
|
|
for i := range weken {
|
|
if i > 0 {
|
|
fmt.Print(" -or")
|
|
}
|
|
y, w := t2.ISOWeek()
|
|
fmt.Printf(" -name %d-%02d.data.dz", y, w)
|
|
t2 = t2.AddDate(0, 0, 7)
|
|
}
|
|
|
|
// vanaf begin voorgaande weken t/m eind huidige week
|
|
t = t.AddDate(0, 0, -7*(weken-1))
|
|
for range 7 * weken {
|
|
fmt.Printf(" -or -name %d-%02d-%02d.data.dz", t.Year(), t.Month(), t.Day())
|
|
t = t.AddDate(0, 0, 1)
|
|
}
|
|
|
|
}
|