collect.sh
This commit is contained in:
95
cmd/week2files/week2files.go
Normal file
95
cmd/week2files/week2files.go
Normal file
@@ -0,0 +1,95 @@
|
||||
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)
|
||||
|
||||
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)
|
||||
|
||||
// drie voorgaande weken
|
||||
t2 := t.AddDate(0, 0, -21)
|
||||
for i := range 4 {
|
||||
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 drie weken geleden t/m eind huidige week
|
||||
t = t.AddDate(0, 0, -21)
|
||||
for range 28 {
|
||||
fmt.Printf(" -or -name %d-%02d-%02d.data.dz", t.Year(), t.Month(), t.Day())
|
||||
t = t.AddDate(0, 0, 1)
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user