Skip to content

Commit b6efbfc

Browse files
committed
fix #58: add jitter to suggested commiter date
1 parent 7dff8db commit b6efbfc

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

internal/command/github/view/suggest.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package view
22

33
import (
44
"fmt"
5+
"math/rand"
56
"strconv"
67
"time"
78

@@ -21,6 +22,11 @@ type SuggestOption struct {
2122
// TODO:refactoring combine with Lookup, use HeatMap as input
2223
// TODO:refactoring extract "table builder", compare with others views
2324

25+
func jitter(duration time.Duration) time.Duration {
26+
r := rand.New(rand.NewSource(time.Now().UnixNano()))
27+
return time.Duration(r.Int63n(int64(duration)))
28+
}
29+
2430
func Suggest(
2531
printer interface{ Println(...interface{}) },
2632

@@ -29,23 +35,22 @@ func Suggest(
2935

3036
option SuggestOption,
3137
) error {
32-
now := time.Now().UTC()
38+
now := time.Now()
39+
day := option.Suggest.Day.Add(jitter(time.Hour)).In(time.Local)
3340

3441
var suggestion string
3542
if option.Delta {
36-
suggestion = fmt.Sprintf("%dd", option.Suggest.Day.Sub(now)/xtime.Day)
43+
days := day.Sub(now) / xtime.Day
44+
tail := -day.Sub(now) % xtime.Day
45+
suggestion = fmt.Sprintf("%dd%s", days, tail)
3746
} else {
38-
day := xtime.CopyClock(now, option.Suggest.Day).In(time.Local)
3947
suggestion = day.Format(time.RFC3339)
4048
}
4149
if option.Short {
4250
printer.Println(suggestion)
4351
return nil
4452
} else if option.Delta {
45-
suggestion = fmt.Sprintf("%s: %s",
46-
option.Suggest.Day.Format(xtime.RFC3339Day),
47-
suggestion,
48-
)
53+
suggestion = fmt.Sprintf("%s: %s", day.Format(time.RFC3339), suggestion)
4954
}
5055

5156
data := convert(scope, histogram)

0 commit comments

Comments
 (0)