-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvague.go
56 lines (46 loc) · 1.29 KB
/
vague.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Package vague produces vague human readable strings from Golang time.Time
package vague
import (
"fmt"
"time"
)
// Month produces a string representation of what part of a month the time occurs
// The possible outcomes are:
// at the start of <month>
// towards the start of <month>
// in the middle of <month>
// towards the end of <month>
// at the end of <month>
func Month(t time.Time) string {
return month(t, false)
}
// MonthWithYear is a helper with the same output as Month, but with the year appended
func MonthWithYear(t time.Time) string {
return month(t, true)
}
func month(t time.Time, includeYear bool) string {
if t.IsZero() {
return "never"
}
var prefix string
daysInMonth := totalDaysInMonth(t)
cut := daysInMonth / 3
if t.Day() == 1 {
prefix = "at the beginning of"
} else if t.Day() == daysInMonth {
prefix = "at the end of"
} else if t.Day() <= cut {
prefix = "towards the beginning of"
} else if t.Day() > daysInMonth-cut {
prefix = "towards the end of"
} else {
prefix = "in the middle of"
}
if includeYear {
return fmt.Sprintf("%s %s %d", prefix, t.Month(), t.Year())
}
return fmt.Sprintf("%s %s", prefix, t.Month())
}
func totalDaysInMonth(t time.Time) int {
return time.Date(t.Year(), t.Month()+1, 0, 0, 0, 0, 0, t.Location()).Day()
}