Skip to content

Commit

Permalink
minor tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
miku committed Dec 22, 2021
1 parent 3a2c26b commit 0f1e2eb
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 20 deletions.
33 changes: 18 additions & 15 deletions dateutil/intervals.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,22 @@ import (
"github.com/jinzhu/now"
)

// MustParse will panic on an unparsable date string.
func MustParse(value string) time.Time {
t, err := dateparse.ParseStrict(value)
if err != nil {
panic(err)
}
return t
}

// Interval groups start and end.
type Interval struct {
Start time.Time
End time.Time
}

// String renders an interval.
func (iv Interval) String() string {
return fmt.Sprintf("%s %s", iv.Start.Format(time.RFC3339), iv.End.Format(time.RFC3339))
}
Expand All @@ -34,6 +44,7 @@ var (
Hourly = makeIntervalFunc(padLHour, padRHour)
Daily = makeIntervalFunc(padLDay, padRDay)
Weekly = makeIntervalFunc(padLWeek, padRWeek)
Biweekly = makeIntervalFunc(padLBiweek, padRBiweek)
Monthly = makeIntervalFunc(padLMonth, padRMonth)

padLMinute = func(t time.Time) time.Time { return now.With(t).BeginningOfMinute() }
Expand All @@ -44,41 +55,33 @@ var (
padRDay = func(t time.Time) time.Time { return now.With(t).EndOfDay() }
padLWeek = func(t time.Time) time.Time { return now.With(t).BeginningOfWeek() }
padRWeek = func(t time.Time) time.Time { return now.With(t).EndOfWeek() }
padLBiweek = func(t time.Time) time.Time { return now.With(t).BeginningOfWeek() }
padRBiweek = func(t time.Time) time.Time { return now.With(t.Add((168 + 24) * time.Hour)).EndOfWeek() }
padLMonth = func(t time.Time) time.Time { return now.With(t).BeginningOfMonth() }
padRMonth = func(t time.Time) time.Time { return now.With(t).EndOfMonth() }
)

// makeIntervalFunc is a helper to create daily, weekly and other intervals.
// Given two shiftFuncs (to mark the beginning of an interval and the end), we
// return a function, that will allow us to generate intervals.
// TODO: We only need right pad, no?
func makeIntervalFunc(padLeft, padRight padFunc) intervalFunc {
return func(s, e time.Time) (result []Interval) {
if e.Before(s) || e.Equal(s) {
return func(start, end time.Time) (result []Interval) {
if end.Before(start) || end.Equal(start) {
return
}
e = e.Add(-1 * time.Second)
end = end.Add(-1 * time.Second)
var (
l time.Time = s
l time.Time = start
r time.Time
)
for {
r = padRight(l)
result = append(result, Interval{l, r})
l = padLeft(r.Add(1 * time.Second))
if l.After(e) {
if l.After(end) {
break
}
}
return result
}
}

// MustParse will panic on an unparsable date string.
func MustParse(value string) time.Time {
t, err := dateparse.ParseStrict(value)
if err != nil {
panic(err)
}
return t
}
7 changes: 7 additions & 0 deletions dateutil/intervals_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ func TestMakeIntervalFunc(t *testing.T) {
end: MustParse("2000-01-01 12:00"),
numIntervals: 1,
},
{
padLeft: padLBiweek,
padRight: padRBiweek,
start: MustParse("2000-01-01 00:00"),
end: MustParse("2000-01-20 00:00"),
numIntervals: 2,
},
}
for i, c := range cases {
var (
Expand Down
11 changes: 6 additions & 5 deletions formats/finc/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,12 @@ var (

// AuthorReplacer is a special cleaner for author names.
var AuthorReplacer = strings.NewReplacer(
"anonym", "",
"AUTHOR INDEX", "",
"AUTHOR Index", "",
"Anonymous", "",
"keine Angabe", "",
"Author Index", "",
"No authorship indicated", "",
"Not Available, Not Available", "",
"Author Index", "",
"AUTHOR Index", "",
"AUTHOR INDEX", "")
"anonym", "",
"keine Angabe", "",
)

0 comments on commit 0f1e2eb

Please sign in to comment.