forked from hashicorp/terraform-provider-azurerm
/
time.go
78 lines (67 loc) · 1.84 KB
/
time.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
package validate
import (
"fmt"
"time"
"github.com/Azure/go-autorest/autorest/date"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
)
//todo, now in terraform helper, switch over once vended
// -> https://github.com/hashicorp/terraform/blob/master/helper/validation/validation.go#L263
func RFC3339Time(i interface{}, k string) (warnings []string, errors []error) {
v, ok := i.(string)
if !ok {
errors = append(errors, fmt.Errorf("expected type of %q to be string", k))
return
}
if _, err := date.ParseTime(time.RFC3339, v); err != nil {
errors = append(errors, fmt.Errorf("%q has the invalid RFC3339 date format %q: %+v", k, i, err))
}
return warnings, errors
}
// RFC3339 date is duration d or greater into the future
func RFC3339DateInFutureBy(d time.Duration) schema.SchemaValidateFunc {
return func(i interface{}, k string) (warnings []string, errors []error) {
v, ok := i.(string)
if !ok {
errors = append(errors, fmt.Errorf("expected type of %q to be string", k))
return
}
t, err := date.ParseTime(time.RFC3339, v)
if err != nil {
errors = append(errors, fmt.Errorf("%q has the invalid RFC3339 date format %q: %+v", k, i, err))
return
}
if time.Until(t) < d {
errors = append(errors, fmt.Errorf("%q is %q and should be at least %q in the future", k, i, d))
}
return warnings, errors
}
}
func DayOfTheWeek(ignoreCase bool) schema.SchemaValidateFunc {
return validation.StringInSlice([]string{
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
"Sunday",
}, ignoreCase)
}
func Month(ignoreCase bool) schema.SchemaValidateFunc {
return validation.StringInSlice([]string{
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
}, ignoreCase)
}