-
-
Notifications
You must be signed in to change notification settings - Fork 234
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit d0df93d
Showing
5 changed files
with
271 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
guard 'gotest' do | ||
watch(%r{\.go$}) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
= Now | ||
|
||
Now is a time toolkit for golang | ||
|
||
== Why the project named Now? | ||
|
||
```go | ||
now.BeginningOfDay() | ||
``` | ||
`now` is quite readable, aha? | ||
|
||
== But `now` is so common I can't find it with my favorite search engine | ||
|
||
* Star it in github [https://github.com/jinzhu/now](https://github.com/jinzhu/now) | ||
* Search it with [http://godoc.org](http://godoc.org) | ||
|
||
== Usage | ||
|
||
```go | ||
time.Now() // 2013-11-18 17:51:49.123456789 Mon | ||
|
||
now.BeginningOfMinute() // 2013-11-18 17:51:00 Mon | ||
now.BeginningOfHour() // 2013-11-18 17:00:00 Mon | ||
now.BeginningOfDay() // 2013-11-18 00:00:00 Mon | ||
now.BeginningOfWeek() // 2013-11-17 00:00:00 Sun | ||
|
||
now.FirstDayMonday = true // Set Monday as first day | ||
now.BeginningOfWeek() // 2013-11-18 00:00:00 Mon | ||
now.BeginningOfMonth() // 2013-11-01 00:00:00 Fri | ||
now.BeginningOfYear() // 2013-01-01 00:00:00 Tue | ||
|
||
now.EndOfMinute() // 2013-11-18 17:51:59.999999999 Mon | ||
now.EndOfHour() // 2013-11-18 17:59:59.999999999 Mon | ||
now.EndOfDay() // 2013-11-18 23:59:59.999999999 Mon | ||
now.EndOfWeek() // 2013-11-23 23:59:59.999999999 Sat | ||
|
||
now.FirstDayMonday = true // Set Monday as first day | ||
now.EndOfWeek() // 2013-11-24 23:59:59.999999999 Sun | ||
now.EndOfMonth() // 2013-11-30 23:59:59.999999999 Sat | ||
now.EndOfYear() // 2013-12-31 23:59:59.999999999 Tue | ||
|
||
// Use another time | ||
t := time.Date(2013, 02, 18, 17, 51, 49, 123456789, time.Now().Location()) | ||
now.New(t).EndOfMonth() // 2013-02-28 23:59:59.999999999 Thu | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Package now is a time toolkit for golang. | ||
package now | ||
|
||
import "time" | ||
|
||
var FirstDayMonday bool | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong. |
||
|
||
func init() { | ||
FirstDayMonday = false | ||
} | ||
|
||
type Now struct { | ||
time time.Time | ||
format string | ||
} | ||
|
||
func New(t time.Time) *Now { | ||
return &Now{time: t} | ||
} | ||
|
||
func BeginningOfMinute() time.Time { | ||
return New(time.Now()).BeginningOfMinute() | ||
} | ||
|
||
func BeginningOfHour() time.Time { | ||
return New(time.Now()).BeginningOfHour() | ||
} | ||
|
||
func BeginningOfDay() time.Time { | ||
return New(time.Now()).BeginningOfDay() | ||
} | ||
|
||
func BeginningOfWeek() time.Time { | ||
return New(time.Now()).BeginningOfWeek() | ||
} | ||
|
||
func BeginningOfMonth() time.Time { | ||
return New(time.Now()).BeginningOfMonth() | ||
} | ||
|
||
func BeginningOfYear() time.Time { | ||
return New(time.Now()).BeginningOfYear() | ||
} | ||
|
||
func EndOfMinute() time.Time { | ||
return New(time.Now()).EndOfMinute() | ||
} | ||
|
||
func EndOfHour() time.Time { | ||
return New(time.Now()).EndOfHour() | ||
} | ||
|
||
func EndOfDay() time.Time { | ||
return New(time.Now()).EndOfDay() | ||
} | ||
|
||
func EndOfWeek() time.Time { | ||
return New(time.Now()).EndOfWeek() | ||
} | ||
|
||
func EndOfMonth() time.Time { | ||
return New(time.Now()).EndOfMonth() | ||
} | ||
|
||
func EndOfYear() time.Time { | ||
return New(time.Now()).EndOfYear() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package now | ||
|
||
import "time" | ||
|
||
func (now *Now) BeginningOfMinute() time.Time { | ||
return now.time.Truncate(time.Minute) | ||
} | ||
|
||
func (now *Now) BeginningOfHour() time.Time { | ||
return now.time.Truncate(time.Hour) | ||
} | ||
|
||
func (now *Now) BeginningOfDay() time.Time { | ||
d := time.Duration(-now.time.Hour()) * time.Hour | ||
return now.time.Truncate(time.Hour).Add(d) | ||
} | ||
|
||
func (now *Now) BeginningOfWeek() time.Time { | ||
t := now.BeginningOfDay() | ||
weekday := int(t.Weekday()) | ||
if FirstDayMonday { | ||
if weekday == 0 { | ||
weekday = 7 | ||
} | ||
weekday = weekday - 1 | ||
} | ||
|
||
d := time.Duration(-weekday) * 24 * time.Hour | ||
return t.Truncate(time.Hour).Add(d) | ||
} | ||
|
||
func (now *Now) BeginningOfMonth() time.Time { | ||
t := now.BeginningOfDay() | ||
d := time.Duration(-int(t.Day())+1) * 24 * time.Hour | ||
return t.Truncate(time.Hour).Add(d) | ||
} | ||
|
||
func (now *Now) BeginningOfYear() time.Time { | ||
t := now.BeginningOfDay() | ||
d := time.Duration(-int(t.YearDay())+1) * 24 * time.Hour | ||
return t.Truncate(time.Hour).Add(d) | ||
} | ||
|
||
func (now *Now) EndOfMinute() time.Time { | ||
return now.BeginningOfMinute().Add(time.Minute - time.Nanosecond) | ||
} | ||
|
||
func (now *Now) EndOfHour() time.Time { | ||
return now.BeginningOfHour().Add(time.Hour - time.Nanosecond) | ||
} | ||
|
||
func (now *Now) EndOfDay() time.Time { | ||
return now.BeginningOfDay().Add(24*time.Hour - time.Nanosecond) | ||
} | ||
|
||
func (now *Now) EndOfWeek() time.Time { | ||
return now.BeginningOfWeek().AddDate(0, 0, 7).Add(-time.Nanosecond) | ||
} | ||
|
||
func (now *Now) EndOfMonth() time.Time { | ||
return now.BeginningOfMonth().AddDate(0, 1, 0).Add(-time.Nanosecond) | ||
} | ||
|
||
func (now *Now) EndOfYear() time.Time { | ||
return now.BeginningOfYear().AddDate(1, 0, 0).Add(-time.Nanosecond) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package now | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
) | ||
|
||
var format string | ||
|
||
func init() { | ||
format = "2006-01-02 15:04:05.999999999" | ||
} | ||
|
||
func TestBeginningOf(t *testing.T) { | ||
n := time.Date(2013, 11, 18, 17, 51, 49, 123456789, time.Now().Location()) | ||
|
||
if New(n).BeginningOfMinute().Format(format) != "2013-11-18 17:51:00" { | ||
t.Errorf("BeginningOfMinute") | ||
} | ||
|
||
if New(n).BeginningOfHour().Format(format) != "2013-11-18 17:00:00" { | ||
t.Errorf("BeginningOfHour") | ||
} | ||
|
||
if New(n).BeginningOfDay().Format(format) != "2013-11-18 00:00:00" { | ||
t.Errorf("BeginningOfDay") | ||
} | ||
|
||
FirstDayMonday = true | ||
if New(n).BeginningOfWeek().Format(format) != "2013-11-18 00:00:00" { | ||
t.Errorf("BeginningOfWeek, FirstDayMonday") | ||
} | ||
|
||
FirstDayMonday = false | ||
if New(n).BeginningOfWeek().Format(format) != "2013-11-17 00:00:00" { | ||
t.Errorf("BeginningOfWeek") | ||
} | ||
|
||
if New(n).BeginningOfMonth().Format(format) != "2013-11-01 00:00:00" { | ||
t.Errorf("BeginningOfMonth") | ||
} | ||
|
||
if New(n).BeginningOfYear().Format(format) != "2013-01-01 00:00:00" { | ||
t.Errorf("BeginningOfYear") | ||
} | ||
} | ||
|
||
func TestEndOf(t *testing.T) { | ||
n := time.Date(2013, 11, 18, 17, 51, 49, 123456789, time.Now().Location()) | ||
|
||
if New(n).EndOfMinute().Format(format) != "2013-11-18 17:51:59.999999999" { | ||
t.Errorf("EndOfMinute") | ||
} | ||
|
||
if New(n).EndOfHour().Format(format) != "2013-11-18 17:59:59.999999999" { | ||
t.Errorf("EndOfHour") | ||
} | ||
|
||
if New(n).EndOfDay().Format(format) != "2013-11-18 23:59:59.999999999" { | ||
t.Errorf("EndOfDay") | ||
} | ||
|
||
FirstDayMonday = true | ||
if New(n).EndOfWeek().Format(format) != "2013-11-24 23:59:59.999999999" { | ||
t.Errorf("EndOfWeek, FirstDayMonday") | ||
} | ||
|
||
FirstDayMonday = false | ||
if New(n).EndOfWeek().Format(format) != "2013-11-23 23:59:59.999999999" { | ||
t.Errorf("EndOfWeek") | ||
} | ||
|
||
if New(n).EndOfMonth().Format(format) != "2013-11-30 23:59:59.999999999" { | ||
t.Errorf("EndOfMonth") | ||
} | ||
|
||
if New(n).EndOfYear().Format(format) != "2013-12-31 23:59:59.999999999" { | ||
t.Errorf("EndOfYear") | ||
} | ||
|
||
n1 := time.Date(2013, 02, 18, 17, 51, 49, 123456789, time.Now().Location()) | ||
if New(n1).EndOfMonth().Format(format) != "2013-02-28 23:59:59.999999999" { | ||
t.Errorf("EndOfMonth for 2013/02") | ||
} | ||
|
||
n2 := time.Date(1900, 02, 18, 17, 51, 49, 123456789, time.Now().Location()) | ||
if New(n2).EndOfMonth().Format(format) != "1900-02-28 23:59:59.999999999" { | ||
t.Errorf("EndOfMonth") | ||
} | ||
} |
Just use "var FirstDayMondy = false" and then you don't need the init(). And you don't need to initialize it at all, because the zero value of a bool is false.