File: time.go

package info (click to toggle)
golang-github-vdemeester-shakers 0.0~git20160210.0.24d7f1d-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 192 kB
  • ctags: 158
  • sloc: sh: 159; makefile: 25
file content (234 lines) | stat: -rw-r--r-- 6,383 bytes parent folder | download | duplicates (3)
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package shakers

import (
	"fmt"
	"time"

	"github.com/go-check/check"
)

// Default format when parsing (in addition to RFC and default time formats..)
const shortForm = "2006-01-02"

// IsBefore checker verifies the specified value is before the specified time.
// It is exclusive.
//
//    c.Assert(myTime, IsBefore, theTime, check.Commentf("bouuuhhh"))
//
var IsBefore check.Checker = &isBeforeChecker{
	&check.CheckerInfo{
		Name:   "IsBefore",
		Params: []string{"obtained", "expected"},
	},
}

type isBeforeChecker struct {
	*check.CheckerInfo
}

func (checker *isBeforeChecker) Check(params []interface{}, names []string) (bool, string) {
	return isBefore(params[0], params[1])
}

func isBefore(value, t interface{}) (bool, string) {
	tTime, ok := parseTime(t)
	if !ok {
		return false, "expected must be a Time struct, or parseable."
	}
	valueTime, valueIsTime := parseTime(value)
	if valueIsTime {
		return valueTime.Before(tTime), ""
	}
	return false, "obtained value is not a time.Time struct or parseable as a time."
}

// IsAfter checker verifies the specified value is before the specified time.
// It is exclusive.
//
//    c.Assert(myTime, IsAfter, theTime, check.Commentf("bouuuhhh"))
//
var IsAfter check.Checker = &isAfterChecker{
	&check.CheckerInfo{
		Name:   "IsAfter",
		Params: []string{"obtained", "expected"},
	},
}

type isAfterChecker struct {
	*check.CheckerInfo
}

func (checker *isAfterChecker) Check(params []interface{}, names []string) (bool, string) {
	return isAfter(params[0], params[1])
}

func isAfter(value, t interface{}) (bool, string) {
	tTime, ok := parseTime(t)
	if !ok {
		return false, "expected must be a Time struct, or parseable."
	}
	valueTime, valueIsTime := parseTime(value)
	if valueIsTime {
		return valueTime.After(tTime), ""
	}
	return false, "obtained value is not a time.Time struct or parseable as a time."
}

// IsBetween checker verifies the specified time is between the specified start
// and end. It's exclusive so if the specified time is at the tip of the interval.
//
//    c.Assert(myTime, IsBetween, startTime, endTime, check.Commentf("bouuuhhh"))
//
var IsBetween check.Checker = &isBetweenChecker{
	&check.CheckerInfo{
		Name:   "IsBetween",
		Params: []string{"obtained", "start", "end"},
	},
}

type isBetweenChecker struct {
	*check.CheckerInfo
}

func (checker *isBetweenChecker) Check(params []interface{}, names []string) (bool, string) {
	return isBetween(params[0], params[1], params[2])
}

func isBetween(value, start, end interface{}) (bool, string) {
	startTime, ok := parseTime(start)
	if !ok {
		return false, "start must be a Time struct, or parseable."
	}
	endTime, ok := parseTime(end)
	if !ok {
		return false, "end must be a Time struct, or parseable."
	}
	valueTime, valueIsTime := parseTime(value)
	if valueIsTime {
		return valueTime.After(startTime) && valueTime.Before(endTime), ""
	}
	return false, "obtained value is not a time.Time struct or parseable as a time."
}

// TimeEquals checker verifies the specified time is the equal to the expected
// time.
//
//    c.Assert(myTime, TimeEquals, expected, check.Commentf("bouhhh"))
//
// It's possible to ignore some part of the time (like hours, minutes, etc..) using
// the TimeIgnore checker with it.
//
//    c.Assert(myTime, TimeIgnore(TimeEquals, time.Hour), expected, check.Commentf("... bouh.."))
//
var TimeEquals check.Checker = &timeEqualsChecker{
	&check.CheckerInfo{
		Name:   "TimeEquals",
		Params: []string{"obtained", "expected"},
	},
}

type timeEqualsChecker struct {
	*check.CheckerInfo
}

func (checker *timeEqualsChecker) Check(params []interface{}, names []string) (bool, string) {
	return timeEquals(params[0], params[1])
}

func timeEquals(obtained, expected interface{}) (bool, string) {
	expectedTime, ok := parseTime(expected)
	if !ok {
		return false, "expected must be a Time struct, or parseable."
	}
	valueTime, valueIsTime := parseTime(obtained)
	if valueIsTime {
		return valueTime.Equal(expectedTime), ""
	}
	return false, "obtained value is not a time.Time struct or parseable as a time."
}

// TimeIgnore checker will ignore some part of the time on the encapsulated checker.
//
//    c.Assert(myTime, TimeIgnore(IsBetween, time.Second), start, end)
//
// FIXME use interface{} for ignore (to enable "Month", ..
func TimeIgnore(checker check.Checker, ignore time.Duration) check.Checker {
	return &timeIgnoreChecker{
		sub:    checker,
		ignore: ignore,
	}
}

type timeIgnoreChecker struct {
	sub    check.Checker
	ignore time.Duration
}

func (checker *timeIgnoreChecker) Info() *check.CheckerInfo {
	info := *checker.sub.Info()
	info.Name = fmt.Sprintf("TimeIgnore(%s, %v)", info.Name, checker.ignore)
	return &info
}

func (checker *timeIgnoreChecker) Check(params []interface{}, names []string) (bool, string) {
	// Naive implementation : all params are supposed to be date
	mParams := make([]interface{}, len(params))
	for index, param := range params {
		paramTime, ok := parseTime(param)
		if !ok {
			return false, fmt.Sprintf("%s must be a Time struct, or parseable.", names[index])
		}
		year := paramTime.Year()
		month := paramTime.Month()
		day := paramTime.Day()
		hour := paramTime.Hour()
		min := paramTime.Minute()
		sec := paramTime.Second()
		nsec := paramTime.Nanosecond()
		location := paramTime.Location()
		switch checker.ignore {
		case time.Hour:
			hour = 0
			fallthrough
		case time.Minute:
			min = 0
			fallthrough
		case time.Second:
			sec = 0
			fallthrough
		case time.Millisecond:
			fallthrough
		case time.Microsecond:
			fallthrough
		case time.Nanosecond:
			nsec = 0
		}
		mParams[index] = time.Date(year, month, day, hour, min, sec, nsec, location)
	}
	return checker.sub.Check(mParams, names)
}

func parseTime(datetime interface{}) (time.Time, bool) {
	switch datetime.(type) {
	case time.Time:
		return datetime.(time.Time), true
	case string:
		return parseTimeAsString(datetime.(string))
	default:
		if datetimeWithStr, ok := datetime.(fmt.Stringer); ok {
			return parseTimeAsString(datetimeWithStr.String())
		}
		return time.Time{}, false
	}
}

func parseTimeAsString(timeAsStr string) (time.Time, bool) {
	forms := []string{shortForm, time.RFC3339, time.RFC3339Nano, time.RFC822, time.RFC822Z}
	for _, form := range forms {
		datetime, err := time.Parse(form, timeAsStr)
		if err == nil {
			return datetime, true
		}
	}
	return time.Time{}, false
}