Skip to content

Commit f992845

Browse files
authored
Merge pull request #19 from ThatEric/master
Added WeekStartDay so that any day can be the start of the week.
2 parents d0d8993 + 9da05f4 commit f992845

File tree

4 files changed

+98
-44
lines changed

4 files changed

+98
-44
lines changed

README.md

+19-19
Original file line numberDiff line numberDiff line change
@@ -29,25 +29,25 @@ import "github.com/jinzhu/now"
2929

3030
time.Now() // 2013-11-18 17:51:49.123456789 Mon
3131

32-
now.BeginningOfMinute() // 2013-11-18 17:51:00 Mon
33-
now.BeginningOfHour() // 2013-11-18 17:00:00 Mon
34-
now.BeginningOfDay() // 2013-11-18 00:00:00 Mon
35-
now.BeginningOfWeek() // 2013-11-17 00:00:00 Sun
36-
now.FirstDayMonday = true // Set Monday as first day, default is Sunday
37-
now.BeginningOfWeek() // 2013-11-18 00:00:00 Mon
38-
now.BeginningOfMonth() // 2013-11-01 00:00:00 Fri
39-
now.BeginningOfQuarter() // 2013-10-01 00:00:00 Tue
40-
now.BeginningOfYear() // 2013-01-01 00:00:00 Tue
41-
42-
now.EndOfMinute() // 2013-11-18 17:51:59.999999999 Mon
43-
now.EndOfHour() // 2013-11-18 17:59:59.999999999 Mon
44-
now.EndOfDay() // 2013-11-18 23:59:59.999999999 Mon
45-
now.EndOfWeek() // 2013-11-23 23:59:59.999999999 Sat
46-
now.FirstDayMonday = true // Set Monday as first day, default is Sunday
47-
now.EndOfWeek() // 2013-11-24 23:59:59.999999999 Sun
48-
now.EndOfMonth() // 2013-11-30 23:59:59.999999999 Sat
49-
now.EndOfQuarter() // 2013-12-31 23:59:59.999999999 Tue
50-
now.EndOfYear() // 2013-12-31 23:59:59.999999999 Tue
32+
now.BeginningOfMinute() // 2013-11-18 17:51:00 Mon
33+
now.BeginningOfHour() // 2013-11-18 17:00:00 Mon
34+
now.BeginningOfDay() // 2013-11-18 00:00:00 Mon
35+
now.BeginningOfWeek() // 2013-11-17 00:00:00 Sun
36+
now.WeekStartDay = time.Monday // Set Monday as first day, default is Sunday
37+
now.BeginningOfWeek() // 2013-11-18 00:00:00 Mon
38+
now.BeginningOfMonth() // 2013-11-01 00:00:00 Fri
39+
now.BeginningOfQuarter() // 2013-10-01 00:00:00 Tue
40+
now.BeginningOfYear() // 2013-01-01 00:00:00 Tue
41+
42+
now.EndOfMinute() // 2013-11-18 17:51:59.999999999 Mon
43+
now.EndOfHour() // 2013-11-18 17:59:59.999999999 Mon
44+
now.EndOfDay() // 2013-11-18 23:59:59.999999999 Mon
45+
now.EndOfWeek() // 2013-11-23 23:59:59.999999999 Sat
46+
now.WeekStartDay = time.Monday // Set Monday as first day, default is Sunday
47+
now.EndOfWeek() // 2013-11-24 23:59:59.999999999 Sun
48+
now.EndOfMonth() // 2013-11-30 23:59:59.999999999 Sat
49+
now.EndOfQuarter() // 2013-12-31 23:59:59.999999999 Tue
50+
now.EndOfYear() // 2013-12-31 23:59:59.999999999 Tue
5151

5252

5353
// Use another time

main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ package now
1111

1212
import "time"
1313

14-
var FirstDayMonday bool
14+
var WeekStartDay = time.Sunday
1515
var TimeFormats = []string{"1/2/2006", "1/2/2006 15:4:5", "2006-1-2 15:4:5", "2006-1-2 15:4", "2006-1-2", "1-2", "15:4:5", "15:4", "15", "15:4:5 Jan 2, 2006 MST", "2006-01-02 15:04:05.999999999 -0700 MST"}
1616

1717
type Now struct {

now.go

+8-4
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,15 @@ func (now *Now) BeginningOfDay() time.Time {
2222
func (now *Now) BeginningOfWeek() time.Time {
2323
t := now.BeginningOfDay()
2424
weekday := int(t.Weekday())
25-
if FirstDayMonday {
26-
if weekday == 0 {
27-
weekday = 7
25+
26+
if WeekStartDay != time.Sunday {
27+
weekStartDayInt := int(WeekStartDay)
28+
29+
if weekday < weekStartDayInt {
30+
weekday = weekday + 7 - weekStartDayInt
31+
} else {
32+
weekday = weekday - weekStartDayInt
2833
}
29-
weekday = weekday - 1
3034
}
3135

3236
d := time.Duration(-weekday) * 24 * time.Hour

now_test.go

+70-20
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,40 @@ func TestBeginningOf(t *testing.T) {
2828
t.Errorf("BeginningOfDay")
2929
}
3030

31-
if New(n).BeginningOfWeek().Format(format) != "2013-11-17 00:00:00" {
32-
t.Errorf("BeginningOfWeek")
33-
}
34-
35-
FirstDayMonday = true
31+
WeekStartDay = time.Monday
3632
if New(n).BeginningOfWeek().Format(format) != "2013-11-18 00:00:00" {
3733
t.Errorf("BeginningOfWeek, FirstDayMonday")
3834
}
39-
FirstDayMonday = false
35+
36+
WeekStartDay = time.Tuesday
37+
if New(n).BeginningOfWeek().Format(format) != "2013-11-12 00:00:00" {
38+
t.Errorf("BeginningOfWeek, FirstDayTuesday")
39+
}
40+
41+
WeekStartDay = time.Wednesday
42+
if New(n).BeginningOfWeek().Format(format) != "2013-11-13 00:00:00" {
43+
t.Errorf("BeginningOfWeek, FirstDayWednesday")
44+
}
45+
46+
WeekStartDay = time.Thursday
47+
if New(n).BeginningOfWeek().Format(format) != "2013-11-14 00:00:00" {
48+
t.Errorf("BeginningOfWeek, FirstDayThursday")
49+
}
50+
51+
WeekStartDay = time.Friday
52+
if New(n).BeginningOfWeek().Format(format) != "2013-11-15 00:00:00" {
53+
t.Errorf("BeginningOfWeek, FirstDayFriday")
54+
}
55+
56+
WeekStartDay = time.Saturday
57+
if New(n).BeginningOfWeek().Format(format) != "2013-11-16 00:00:00" {
58+
t.Errorf("BeginningOfWeek, FirstDaySaturday")
59+
}
60+
61+
WeekStartDay = time.Sunday
62+
if New(n).BeginningOfWeek().Format(format) != "2013-11-17 00:00:00" {
63+
t.Errorf("BeginningOfWeek, FirstDaySunday")
64+
}
4065

4166
if New(n).BeginningOfMonth().Format(format) != "2013-11-01 00:00:00" {
4267
t.Errorf("BeginningOfMonth")
@@ -74,14 +99,39 @@ func TestEndOf(t *testing.T) {
7499
t.Errorf("EndOfDay")
75100
}
76101

77-
FirstDayMonday = true
102+
WeekStartDay = time.Monday
78103
if New(n).EndOfWeek().Format(format) != "2013-11-24 23:59:59.999999999" {
79104
t.Errorf("EndOfWeek, FirstDayMonday")
80105
}
81106

82-
FirstDayMonday = false
107+
WeekStartDay = time.Tuesday
108+
if New(n).EndOfWeek().Format(format) != "2013-11-18 23:59:59.999999999" {
109+
t.Errorf("EndOfWeek, FirstDayTuesday")
110+
}
111+
112+
WeekStartDay = time.Wednesday
113+
if New(n).EndOfWeek().Format(format) != "2013-11-19 23:59:59.999999999" {
114+
t.Errorf("EndOfWeek, FirstDayWednesday")
115+
}
116+
117+
WeekStartDay = time.Thursday
118+
if New(n).EndOfWeek().Format(format) != "2013-11-20 23:59:59.999999999" {
119+
t.Errorf("EndOfWeek, FirstDayThursday")
120+
}
121+
122+
WeekStartDay = time.Friday
123+
if New(n).EndOfWeek().Format(format) != "2013-11-21 23:59:59.999999999" {
124+
t.Errorf("EndOfWeek, FirstDayFriday")
125+
}
126+
127+
WeekStartDay = time.Saturday
128+
if New(n).EndOfWeek().Format(format) != "2013-11-22 23:59:59.999999999" {
129+
t.Errorf("EndOfWeek, FirstDaySaturday")
130+
}
131+
132+
WeekStartDay = time.Sunday
83133
if New(n).EndOfWeek().Format(format) != "2013-11-23 23:59:59.999999999" {
84-
t.Errorf("EndOfWeek")
134+
t.Errorf("EndOfWeek, FirstDaySunday")
85135
}
86136

87137
if New(n).EndOfMonth().Format(format) != "2013-11-30 23:59:59.999999999" {
@@ -143,7 +193,7 @@ func TestMondayAndSunday(t *testing.T) {
143193
t.Errorf("BeginningOfWeek, FirstDayMonday")
144194
}
145195

146-
FirstDayMonday = true
196+
WeekStartDay = time.Monday
147197
if New(n).BeginningOfWeek().Format(format) != "2013-11-18 00:00:00" {
148198
t.Errorf("BeginningOfWeek, FirstDayMonday")
149199
}
@@ -250,22 +300,22 @@ func Example() {
250300
BeginningOfDay() // 2013-11-18 00:00:00 Mon
251301
BeginningOfWeek() // 2013-11-17 00:00:00 Sun
252302

253-
FirstDayMonday = true // Set Monday as first day
254-
BeginningOfWeek() // 2013-11-18 00:00:00 Mon
255-
BeginningOfMonth() // 2013-11-01 00:00:00 Fri
256-
BeginningOfQuarter() // 2013-10-01 00:00:00 Tue
257-
BeginningOfYear() // 2013-01-01 00:00:00 Tue
303+
WeekStartDay = time.Monday // Set Monday as first day
304+
BeginningOfWeek() // 2013-11-18 00:00:00 Mon
305+
BeginningOfMonth() // 2013-11-01 00:00:00 Fri
306+
BeginningOfQuarter() // 2013-10-01 00:00:00 Tue
307+
BeginningOfYear() // 2013-01-01 00:00:00 Tue
258308

259309
EndOfMinute() // 2013-11-18 17:51:59.999999999 Mon
260310
EndOfHour() // 2013-11-18 17:59:59.999999999 Mon
261311
EndOfDay() // 2013-11-18 23:59:59.999999999 Mon
262312
EndOfWeek() // 2013-11-23 23:59:59.999999999 Sat
263313

264-
FirstDayMonday = true // Set Monday as first day
265-
EndOfWeek() // 2013-11-24 23:59:59.999999999 Sun
266-
EndOfMonth() // 2013-11-30 23:59:59.999999999 Sat
267-
EndOfQuarter() // 2013-12-31 23:59:59.999999999 Tue
268-
EndOfYear() // 2013-12-31 23:59:59.999999999 Tue
314+
WeekStartDay = time.Monday // Set Monday as first day
315+
EndOfWeek() // 2013-11-24 23:59:59.999999999 Sun
316+
EndOfMonth() // 2013-11-30 23:59:59.999999999 Sat
317+
EndOfQuarter() // 2013-12-31 23:59:59.999999999 Tue
318+
EndOfYear() // 2013-12-31 23:59:59.999999999 Tue
269319

270320
// Use another time
271321
t := time.Date(2013, 02, 18, 17, 51, 49, 123456789, time.UTC)

0 commit comments

Comments
 (0)