1package now
2
3import (
4	"testing"
5	"time"
6)
7
8var format = "2006-01-02 15:04:05.999999999"
9
10func TestBeginningOf(t *testing.T) {
11	n := time.Date(2013, 11, 18, 17, 51, 49, 123456789, time.UTC)
12
13	if New(n).BeginningOfMinute().Format(format) != "2013-11-18 17:51:00" {
14		t.Errorf("BeginningOfMinute")
15	}
16
17	if New(n).BeginningOfHour().Format(format) != "2013-11-18 17:00:00" {
18		t.Errorf("BeginningOfHour")
19	}
20
21	if New(n).BeginningOfDay().Format(format) != "2013-11-18 00:00:00" {
22		t.Errorf("BeginningOfDay")
23	}
24
25	location, _ := time.LoadLocation("Japan")
26	beginningOfDay := time.Date(2015, 05, 01, 0, 0, 0, 0, location)
27	if New(beginningOfDay).BeginningOfDay().Format(format) != "2015-05-01 00:00:00" {
28		t.Errorf("BeginningOfDay")
29	}
30
31	if New(n).BeginningOfWeek().Format(format) != "2013-11-17 00:00:00" {
32		t.Errorf("BeginningOfWeek")
33	}
34
35	FirstDayMonday = true
36	if New(n).BeginningOfWeek().Format(format) != "2013-11-18 00:00:00" {
37		t.Errorf("BeginningOfWeek, FirstDayMonday")
38	}
39	FirstDayMonday = false
40
41	if New(n).BeginningOfMonth().Format(format) != "2013-11-01 00:00:00" {
42		t.Errorf("BeginningOfMonth")
43	}
44
45	if New(n).BeginningOfQuarter().Format(format) != "2013-10-01 00:00:00" {
46		t.Error("BeginningOfQuarter")
47	}
48
49	if New(n.AddDate(0, -1, 0)).BeginningOfQuarter().Format(format) != "2013-10-01 00:00:00" {
50		t.Error("BeginningOfQuarter")
51	}
52
53	if New(n.AddDate(0, 1, 0)).BeginningOfQuarter().Format(format) != "2013-10-01 00:00:00" {
54		t.Error("BeginningOfQuarter")
55	}
56
57	if New(n).BeginningOfYear().Format(format) != "2013-01-01 00:00:00" {
58		t.Errorf("BeginningOfYear")
59	}
60}
61
62func TestEndOf(t *testing.T) {
63	n := time.Date(2013, 11, 18, 17, 51, 49, 123456789, time.UTC)
64
65	if New(n).EndOfMinute().Format(format) != "2013-11-18 17:51:59.999999999" {
66		t.Errorf("EndOfMinute")
67	}
68
69	if New(n).EndOfHour().Format(format) != "2013-11-18 17:59:59.999999999" {
70		t.Errorf("EndOfHour")
71	}
72
73	if New(n).EndOfDay().Format(format) != "2013-11-18 23:59:59.999999999" {
74		t.Errorf("EndOfDay")
75	}
76
77	FirstDayMonday = true
78	if New(n).EndOfWeek().Format(format) != "2013-11-24 23:59:59.999999999" {
79		t.Errorf("EndOfWeek, FirstDayMonday")
80	}
81
82	FirstDayMonday = false
83	if New(n).EndOfWeek().Format(format) != "2013-11-23 23:59:59.999999999" {
84		t.Errorf("EndOfWeek")
85	}
86
87	if New(n).EndOfMonth().Format(format) != "2013-11-30 23:59:59.999999999" {
88		t.Errorf("EndOfMonth")
89	}
90
91	if New(n).EndOfQuarter().Format(format) != "2013-12-31 23:59:59.999999999" {
92		t.Errorf("EndOfQuarter")
93	}
94
95	if New(n.AddDate(0, -1, 0)).EndOfQuarter().Format(format) != "2013-12-31 23:59:59.999999999" {
96		t.Errorf("EndOfQuarter")
97	}
98
99	if New(n.AddDate(0, 1, 0)).EndOfQuarter().Format(format) != "2013-12-31 23:59:59.999999999" {
100		t.Errorf("EndOfQuarter")
101	}
102
103	if New(n).EndOfYear().Format(format) != "2013-12-31 23:59:59.999999999" {
104		t.Errorf("EndOfYear")
105	}
106
107	n1 := time.Date(2013, 02, 18, 17, 51, 49, 123456789, time.UTC)
108	if New(n1).EndOfMonth().Format(format) != "2013-02-28 23:59:59.999999999" {
109		t.Errorf("EndOfMonth for 2013/02")
110	}
111
112	n2 := time.Date(1900, 02, 18, 17, 51, 49, 123456789, time.UTC)
113	if New(n2).EndOfMonth().Format(format) != "1900-02-28 23:59:59.999999999" {
114		t.Errorf("EndOfMonth")
115	}
116}
117
118func TestMondayAndSunday(t *testing.T) {
119	n := time.Date(2013, 11, 19, 17, 51, 49, 123456789, time.UTC)
120	n2 := time.Date(2013, 11, 24, 17, 51, 49, 123456789, time.UTC)
121
122	if New(n).Monday().Format(format) != "2013-11-18 00:00:00" {
123		t.Errorf("Monday")
124	}
125
126	if New(n2).Monday().Format(format) != "2013-11-18 00:00:00" {
127		t.Errorf("Monday")
128	}
129
130	if New(n).Sunday().Format(format) != "2013-11-24 00:00:00" {
131		t.Errorf("Sunday")
132	}
133
134	if New(n2).Sunday().Format(format) != "2013-11-24 00:00:00" {
135		t.Errorf("Sunday")
136	}
137
138	if New(n).EndOfSunday().Format(format) != "2013-11-24 23:59:59.999999999" {
139		t.Errorf("Sunday")
140	}
141
142	if New(n).BeginningOfWeek().Format(format) != "2013-11-17 00:00:00" {
143		t.Errorf("BeginningOfWeek, FirstDayMonday")
144	}
145
146	FirstDayMonday = true
147	if New(n).BeginningOfWeek().Format(format) != "2013-11-18 00:00:00" {
148		t.Errorf("BeginningOfWeek, FirstDayMonday")
149	}
150}
151
152func TestParse(t *testing.T) {
153	n := time.Date(2013, 11, 18, 17, 51, 49, 123456789, time.UTC)
154	if New(n).MustParse("10-12").Format(format) != "2013-10-12 00:00:00" {
155		t.Errorf("Parse 10-12")
156	}
157
158	if New(n).MustParse("2002-10-12 22:14").Format(format) != "2002-10-12 22:14:00" {
159		t.Errorf("Parse 2002-10-12 22:14")
160	}
161
162	if New(n).MustParse("2002-10-12 2:4").Format(format) != "2002-10-12 02:04:00" {
163		t.Errorf("Parse 2002-10-12 2:4")
164	}
165
166	if New(n).MustParse("2002-10-12 02:04").Format(format) != "2002-10-12 02:04:00" {
167		t.Errorf("Parse 2002-10-12 02:04")
168	}
169
170	if New(n).MustParse("2002-10-12 22:14:56").Format(format) != "2002-10-12 22:14:56" {
171		t.Errorf("Parse 2002-10-12 22:14:56")
172	}
173
174	if New(n).MustParse("2002-10-12").Format(format) != "2002-10-12 00:00:00" {
175		t.Errorf("Parse 2002-10-12")
176	}
177
178	if New(n).MustParse("18").Format(format) != "2013-11-18 18:00:00" {
179		t.Errorf("Parse 18 as hour")
180	}
181
182	if New(n).MustParse("18:20").Format(format) != "2013-11-18 18:20:00" {
183		t.Errorf("Parse 18:20")
184	}
185
186	if New(n).MustParse("00:01").Format(format) != "2013-11-18 00:01:00" {
187		t.Errorf("Parse 00:01")
188	}
189
190	if New(n).MustParse("18:20:39").Format(format) != "2013-11-18 18:20:39" {
191		t.Errorf("Parse 18:20:39")
192	}
193
194	if New(n).MustParse("18:20:39", "2011-01-01").Format(format) != "2011-01-01 18:20:39" {
195		t.Errorf("Parse two strings 18:20:39, 2011-01-01")
196	}
197
198	if New(n).MustParse("2011-1-1", "18:20:39").Format(format) != "2011-01-01 18:20:39" {
199		t.Errorf("Parse two strings 2011-01-01, 18:20:39")
200	}
201
202	if New(n).MustParse("2011-01-01", "18").Format(format) != "2011-01-01 18:00:00" {
203		t.Errorf("Parse two strings 2011-01-01, 18")
204	}
205
206	TimeFormats = append(TimeFormats, "02 Jan 15:04")
207	if New(n).MustParse("04 Feb 12:09").Format(format) != "2013-02-04 12:09:00" {
208		t.Errorf("Parse 04 Feb 12:09 with specified format")
209	}
210
211	if New(n).MustParse("23:28:9 Dec 19, 2013 PST").Format(format) != "2013-12-19 23:28:09" {
212		t.Errorf("Parse 23:28:9 Dec 19, 2013 PST")
213	}
214
215	if New(n).MustParse("23:28:9 Dec 19, 2013 PST").Location().String() != "PST" {
216		t.Errorf("Parse 23:28:9 Dec 19, 2013 PST shouldn't lose time zone")
217	}
218
219	n2 := New(n).MustParse("23:28:9 Dec 19, 2013 PST")
220	if New(n2).MustParse("10:20").Location().String() != "PST" {
221		t.Errorf("Parse 10:20 shouldn't change time zone")
222	}
223}
224
225func TestBetween(t *testing.T) {
226	tm := time.Date(2015, 06, 30, 17, 51, 49, 123456789, time.Now().Location())
227	if !New(tm).Between("23:28:9 Dec 19, 2013 PST", "23:28:9 Dec 19, 2015 PST") {
228		t.Errorf("Between")
229	}
230
231	if !New(tm).Between("2015-05-12 12:20", "2015-06-30 17:51:50") {
232		t.Errorf("Between")
233	}
234}
235
236func Example() {
237	time.Now() // 2013-11-18 17:51:49.123456789 Mon
238
239	BeginningOfMinute() // 2013-11-18 17:51:00 Mon
240	BeginningOfHour()   // 2013-11-18 17:00:00 Mon
241	BeginningOfDay()    // 2013-11-18 00:00:00 Mon
242	BeginningOfWeek()   // 2013-11-17 00:00:00 Sun
243
244	FirstDayMonday = true // Set Monday as first day
245	BeginningOfWeek()     // 2013-11-18 00:00:00 Mon
246	BeginningOfMonth()    // 2013-11-01 00:00:00 Fri
247	BeginningOfQuarter()  // 2013-10-01 00:00:00 Tue
248	BeginningOfYear()     // 2013-01-01 00:00:00 Tue
249
250	EndOfMinute() // 2013-11-18 17:51:59.999999999 Mon
251	EndOfHour()   // 2013-11-18 17:59:59.999999999 Mon
252	EndOfDay()    // 2013-11-18 23:59:59.999999999 Mon
253	EndOfWeek()   // 2013-11-23 23:59:59.999999999 Sat
254
255	FirstDayMonday = true // Set Monday as first day
256	EndOfWeek()           // 2013-11-24 23:59:59.999999999 Sun
257	EndOfMonth()          // 2013-11-30 23:59:59.999999999 Sat
258	EndOfQuarter()        // 2013-12-31 23:59:59.999999999 Tue
259	EndOfYear()           // 2013-12-31 23:59:59.999999999 Tue
260
261	// Use another time
262	t := time.Date(2013, 02, 18, 17, 51, 49, 123456789, time.UTC)
263	New(t).EndOfMonth() // 2013-02-28 23:59:59.999999999 Thu
264
265	Monday()      // 2013-11-18 00:00:00 Mon
266	Sunday()      // 2013-11-24 00:00:00 Sun
267	EndOfSunday() // 2013-11-24 23:59:59.999999999 Sun
268}
269