www: selectie van de week
This commit is contained in:
105
cmd/dates2json/dates2json.go
Normal file
105
cmd/dates2json/dates2json.go
Normal file
@@ -0,0 +1,105 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
e "codeberg.org/pebbe/errors"
|
||||
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Date struct {
|
||||
Week string `json:"week"`
|
||||
First string `json:"first"`
|
||||
Last string `json:"last"`
|
||||
}
|
||||
|
||||
var (
|
||||
x = e.ExitErr
|
||||
|
||||
dates = make([]Date, 0)
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
files, err := os.ReadDir("/net/corpora/nlnieuws/data")
|
||||
x(err)
|
||||
|
||||
for _, file := range files {
|
||||
filename := file.Name()
|
||||
if strings.HasPrefix(filename, "DATA-") && strings.HasSuffix(filename, "-4.json") {
|
||||
addWeek(filename[5:12])
|
||||
}
|
||||
}
|
||||
|
||||
slices.Reverse(dates)
|
||||
b, err := json.Marshal(dates)
|
||||
x(err)
|
||||
fmt.Println(string(b))
|
||||
|
||||
}
|
||||
|
||||
func addWeek(s string) {
|
||||
|
||||
week, err := strconv.Atoi(s[5:])
|
||||
x(err)
|
||||
year, err := strconv.Atoi(s[:4])
|
||||
x(err)
|
||||
|
||||
// 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, _ = 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
|
||||
}
|
||||
tFirst := t.AddDate(0, 0, 1-d)
|
||||
tLast := tFirst.AddDate(0, 0, 6)
|
||||
|
||||
dates = append(dates, Date{
|
||||
Week: s,
|
||||
First: makeDate(tFirst),
|
||||
Last: makeDate(tLast),
|
||||
})
|
||||
}
|
||||
|
||||
func makeDate(d time.Time) string {
|
||||
return fmt.Sprintf("%d-%02d-%02d", d.Year(), int(d.Month()), d.Day())
|
||||
}
|
||||
Reference in New Issue
Block a user