forked from hiscaler/woocommerce-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
51 lines (47 loc) · 1.3 KB
/
utils.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
package woocommerce
import (
"fmt"
"github.com/araddon/dateparse"
"github.com/hiscaler/woocommerce-go/constant"
"strings"
"time"
)
// ToISOTimeString Convert to iso time string
// If date format is invalid, then return original value
// If dateStr include time part, and you set addMinTimeString/addMaxTimeString to true,
// but still return original dateStr value.
func ToISOTimeString(dateStr string, addMinTimeString, addMaxTimeString bool) (s string) {
dateStr = strings.TrimSpace(dateStr)
if dateStr == "" {
return
}
s = dateStr
format, err := dateparse.ParseFormat(dateStr)
if err == nil && (format == constant.DateFormat || format == constant.DatetimeFormat || format == constant.WooDatetimeFormat) {
if strings.Index(dateStr, " ") == -1 {
if addMinTimeString {
dateStr += " 00:00:00"
}
if addMaxTimeString {
dateStr += " 23:59:59"
}
}
if t, err := dateparse.ParseAny(dateStr); err == nil {
return t.Format(time.RFC3339)
}
}
return s
}
// IsValidateTime Is validate time
func IsValidateTime(dateStr string) error {
format, err := dateparse.ParseFormat(dateStr)
if err != nil {
return err
}
switch format {
case constant.DateFormat, constant.DatetimeFormat, constant.WooDatetimeFormat:
return nil
default:
return fmt.Errorf("%s 日期格式无效", dateStr)
}
}