-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathcommon.go
112 lines (94 loc) · 2.42 KB
/
common.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package rapina
import (
"fmt"
"net/url"
"path"
"strconv"
"strings"
"time"
)
// IsDate checks if date is in format YYYY-MM-DD.
func IsDate(date string) bool {
if len(date) != len("2021-04-26") || strings.Count(date, "-") != 2 {
return false
}
y, errY := strconv.Atoi(date[0:4])
m, errM := strconv.Atoi(date[5:7])
d, errD := strconv.Atoi(date[8:10])
if errY != nil || errM != nil || errD != nil {
return false
}
// Ok, we'll still be using this in 2200 :)
if y < 1970 || y > 2200 {
return false
}
if m < 1 || m > 12 {
return false
}
nDays := [13]int{0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
if d < 1 || d > nDays[m] {
return false
}
return true
}
// IsURL returns true if 'str' is a valid URL.
func IsURL(str string) bool {
u, err := url.Parse(str)
return err == nil && u.Scheme != "" && u.Host != ""
}
// JoinURL joins strings as URL paths
func JoinURL(base string, paths ...string) string {
p := path.Join(paths...)
return fmt.Sprintf("%s/%s", strings.TrimRight(base, "/"), strings.TrimLeft(p, "/"))
}
var _timeNow = time.Now
// MonthsFromToday returns a list of months including the current.
// Date formatted as YYYY-MM.
func MonthsFromToday(n int) []string {
if n < 1 {
n = 1
}
if n > 100 {
n = 100
}
now := _timeNow()
now = time.Date(now.Year(), now.Month(), 15, 12, 0, 0, 0, time.UTC)
var monthYears []string
for ; n > 0; n-- {
monthYears = append(monthYears, now.Format("2006-01"))
now = now.AddDate(0, -1, 0)
}
return monthYears
}
// LastBusinessDayOfYear returns the last business day of the 'year' (the business
// day before Dec 30). If current year, returns last business day before today.
// Returns date as YYYY-MM-DD.
func LastBusinessDayOfYear(year int) string {
today := time.Now()
if year == today.Year() {
return LastBusinessDay(1)
}
date := time.Date(year, time.December, 29, 12, 0, 0, 0, time.UTC)
if date.Weekday() == time.Saturday {
date = date.AddDate(0, 0, -1)
}
if date.Weekday() == time.Sunday {
date = date.AddDate(0, 0, -2)
}
return date.Format("2006-01-02")
}
// LastBusinessDay returns the most recent business day 'n' days before today.
// Returns date as YYYY-MM-DD.
func LastBusinessDay(n int) string {
date := time.Now()
if n > 0 {
date = date.AddDate(0, 0, -n)
}
if date.Weekday() == time.Saturday {
date = date.AddDate(0, 0, -1)
}
if date.Weekday() == time.Sunday {
date = date.AddDate(0, 0, -2)
}
return date.Format("2006-01-02")
}