-
Notifications
You must be signed in to change notification settings - Fork 20
/
date.go
104 lines (85 loc) · 2.73 KB
/
date.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
package types
import (
"fmt"
"github.com/nyaruka/gocommon/dates"
"github.com/nyaruka/gocommon/jsonx"
"github.com/nyaruka/goflow/envs"
"github.com/nyaruka/goflow/utils"
)
// XDate is a Gregorian calendar date value.
//
// @(date_from_parts(2019, 4, 11)) -> 2019-04-11
// @(format_date(date_from_parts(2019, 4, 11))) -> 11-04-2019
// @(json(date_from_parts(2019, 4, 11))) -> "2019-04-11"
//
// @type date
type XDate struct {
native dates.Date
}
// NewXDate creates a new date
func NewXDate(value dates.Date) XDate {
return XDate{native: value}
}
// Describe returns a representation of this type for error messages
func (x XDate) Describe() string { return "date" }
// Truthy determines truthiness for this type
func (x XDate) Truthy() bool {
return x != XDateZero
}
// Render returns the canonical text representation
func (x XDate) Render() string { return x.Native().String() }
// Format returns the pretty text representation
func (x XDate) Format(env envs.Environment) string {
formatted, _ := x.FormatCustom(env, string(env.DateFormat()))
return formatted
}
// FormatCustom provides customised formatting
func (x XDate) FormatCustom(env envs.Environment, layout string) (string, error) {
return x.Native().Format(layout, env.DefaultLocale().ToBCP47())
}
// MarshalJSON is called when a struct containing this type is marshaled
func (x XDate) MarshalJSON() ([]byte, error) {
return jsonx.Marshal(x.Native().String())
}
// String returns the native string representation of this type
func (x XDate) String() string {
return fmt.Sprintf(`XDate(%d, %d, %d)`, x.native.Year, x.native.Month, x.native.Day)
}
// Native returns the native value of this type
func (x XDate) Native() dates.Date { return x.native }
// Equals determines equality for this type
func (x XDate) Equals(o XValue) bool {
other := o.(XDate)
return x.Native().Equal(other.Native())
}
// Compare compares this date to another
func (x XDate) Compare(o XValue) int {
other := o.(XDate)
return x.Native().Compare(other.Native())
}
// XDateZero is the zero time value
var XDateZero = NewXDate(dates.ZeroDate)
var _ XValue = XDateZero
// ToXDate converts the given value to a time or returns an error if that isn't possible
func ToXDate(env envs.Environment, x XValue) (XDate, XError) {
if !utils.IsNil(x) {
switch typed := x.(type) {
case XError:
return XDateZero, typed
case XDate:
return typed, nil
case XDateTime:
return typed.In(env.Timezone()).Date(), nil
case XText:
parsed, err := envs.DateFromString(env, typed.Native())
if err == nil {
return NewXDate(parsed), nil
}
case *XObject:
if typed.hasDefault() {
return ToXDate(env, typed.Default())
}
}
}
return XDateZero, NewXErrorf("unable to convert %s to a date", Describe(x))
}