Skip to content

Commit d25846f

Browse files
committed
update
1 parent 67a9171 commit d25846f

File tree

2 files changed

+28
-16
lines changed

2 files changed

+28
-16
lines changed

time.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,19 @@ import (
2323
"time"
2424
)
2525

26-
//Date Format unix time int64 to string
26+
// Date Format unix time int64 to string
2727
func Date(ti int64, format string) string {
2828
t := time.Unix(int64(ti), 0)
2929
return DateT(t, format)
3030
}
3131

32-
//DateS Format unix time string to string
32+
// DateS Format unix time string to string
3333
func DateS(ts string, format string) string {
3434
i, _ := strconv.ParseInt(ts, 10, 64)
3535
return Date(i, format)
3636
}
3737

38-
//DateT Format time.Time struct to string
38+
// DateT Format time.Time struct to string
3939
// MM - month - 01
4040
// M - month - 1, single bit
4141
// DD - day - 02
@@ -104,18 +104,18 @@ var datePatterns = []string{
104104
// DateFormatReplacer .
105105
var DateFormatReplacer = strings.NewReplacer(datePatterns...)
106106

107-
//DateParse Parse Date use PHP time format.
107+
// DateParse Parse Date use PHP time format.
108108
func DateParse(dateString, format string) (time.Time, error) {
109109
return time.ParseInLocation(ConvDateFormat(format), dateString, time.Local)
110110
}
111111

112-
//ConvDateFormat Convert PHP time format.
112+
// ConvDateFormat Convert PHP time format.
113113
func ConvDateFormat(format string) string {
114114
format = DateFormatReplacer.Replace(format)
115115
return format
116116
}
117117

118-
//DateFormat 将时间戳格式化为日期字符窜
118+
// DateFormat 将时间戳格式化为日期字符窜
119119
func DateFormat(format string, timestamp interface{}) (t string) { // timestamp
120120
switch format {
121121
case "Y-m-d H:i:s", "":
@@ -140,7 +140,7 @@ func DateFormat(format string, timestamp interface{}) (t string) { // timestamp
140140
return
141141
}
142142

143-
//StrToTime 日期字符窜转为时间戳数字
143+
// StrToTime 日期字符窜转为时间戳数字
144144
func StrToTime(str string, args ...string) (unixtime int) {
145145
layout := "2006-01-02 15:04:05"
146146
if len(args) > 0 {
@@ -207,7 +207,7 @@ func FormatBytes(args ...interface{}) string {
207207
return r + sizeUnits[total]
208208
}
209209

210-
//DateFormatShort 格式化耗时
210+
// DateFormatShort 格式化耗时
211211
func DateFormatShort(timestamp interface{}) string {
212212
now := time.Now()
213213
year := now.Year()
@@ -334,10 +334,10 @@ func FriendlyTime(d time.Duration, args ...interface{}) (r string) {
334334
return
335335
}
336336

337-
//StartTime 开始时间
337+
// StartTime 开始时间
338338
var StartTime = time.Now()
339339

340-
//TotalRunTime 总运行时长
340+
// TotalRunTime 总运行时长
341341
func TotalRunTime() string {
342342
return FriendlyTime(time.Since(StartTime))
343343
}
@@ -545,6 +545,8 @@ func (t Time) IsAfter(timestamp interface{}, agoDays int, units ...int) bool {
545545
}
546546

547547
var numberSpitRule = regexp.MustCompile(`[^\d]+`)
548+
var MinYear = 1000
549+
var MaxYear = 9999
548550

549551
func FixDateString(dateStr string) string {
550552
parts := numberSpitRule.Split(dateStr, 3)
@@ -577,7 +579,7 @@ func FixDateString(dateStr string) string {
577579
case 1:
578580
// year
579581
year, _ := strconv.Atoi(parts[0])
580-
if year <= 1000 || year > 9999 {
582+
if year <= MinYear || year > MaxYear {
581583
return ``
582584
}
583585
dateArr[0] = parts[0]

url.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,20 +66,30 @@ func URLSafeBase64(str string, encode bool) string {
6666
str = strings.Replace(str, `_`, `/`, -1)
6767
str = strings.Replace(str, `-`, `+`, -1)
6868
var missing = (4 - len(str)%4) % 4
69-
str += strings.Repeat(`=`, missing)
69+
if missing > 0 {
70+
str += strings.Repeat(`=`, missing)
71+
}
7072
return str
7173
}
7274

7375
// SafeBase64Encode base64 encode
7476
func SafeBase64Encode(str string) string {
75-
str = Base64Encode(str)
76-
return URLSafeBase64(str, true)
77+
str = base64.URLEncoding.EncodeToString([]byte(str))
78+
str = strings.TrimRight(str, `=`)
79+
return str
7780
}
7881

7982
// SafeBase64Decode base64 decode
8083
func SafeBase64Decode(str string) (string, error) {
81-
str = URLSafeBase64(str, false)
82-
return Base64Decode(str)
84+
var missing = (4 - len(str)%4) % 4
85+
if missing > 0 {
86+
str += strings.Repeat(`=`, missing)
87+
}
88+
b, err := base64.URLEncoding.DecodeString(str)
89+
if err != nil {
90+
return ``, err
91+
}
92+
return string(b), nil
8393
}
8494

8595
// TotalPages 总页数

0 commit comments

Comments
 (0)