Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change time.Now() to configurable for easier to test #58

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions clock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package now

import "time"

// Clock
type Clock interface {
// Now returns the current time
Now() time.Time
}

// TimeClock a clock implemention of time.Time
type TimeClock struct { }

func (*TimeClock) Now() time.Time {
return time.Now()
}
62 changes: 35 additions & 27 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,18 @@ func (config *Config) With(t time.Time) *Now {
// Parse parse string to time based on configuration
func (config *Config) Parse(strs ...string) (time.Time, error) {
if config.TimeLocation == nil {
return config.With(time.Now()).Parse(strs...)
return config.With(DefaultClock.Now()).Parse(strs...)
} else {
return config.With(time.Now().In(config.TimeLocation)).Parse(strs...)
return config.With(DefaultClock.Now().In(config.TimeLocation)).Parse(strs...)
}
}

// MustParse must parse string to time or will panic
func (config *Config) MustParse(strs ...string) time.Time {
if config.TimeLocation == nil {
return config.With(time.Now()).MustParse(strs...)
return config.With(DefaultClock.Now()).MustParse(strs...)
} else {
return config.With(time.Now().In(config.TimeLocation)).MustParse(strs...)
return config.With(DefaultClock.Now().In(config.TimeLocation)).MustParse(strs...)
}
}

Expand Down Expand Up @@ -83,118 +83,126 @@ func New(t time.Time) *Now {
return With(t)
}

// DefaultClock with TimeClock
var DefaultClock Clock = &TimeClock{}

// ResetClock reset default clock to TimeClock
func ResetClock() {
DefaultClock = &TimeClock{}
}

// BeginningOfMinute beginning of minute
func BeginningOfMinute() time.Time {
return With(time.Now()).BeginningOfMinute()
return With(DefaultClock.Now()).BeginningOfMinute()
}

// BeginningOfHour beginning of hour
func BeginningOfHour() time.Time {
return With(time.Now()).BeginningOfHour()
return With(DefaultClock.Now()).BeginningOfHour()
}

// BeginningOfDay beginning of day
func BeginningOfDay() time.Time {
return With(time.Now()).BeginningOfDay()
return With(DefaultClock.Now()).BeginningOfDay()
}

// BeginningOfWeek beginning of week
func BeginningOfWeek() time.Time {
return With(time.Now()).BeginningOfWeek()
return With(DefaultClock.Now()).BeginningOfWeek()
}

// BeginningOfMonth beginning of month
func BeginningOfMonth() time.Time {
return With(time.Now()).BeginningOfMonth()
return With(DefaultClock.Now()).BeginningOfMonth()
}

// BeginningOfQuarter beginning of quarter
func BeginningOfQuarter() time.Time {
return With(time.Now()).BeginningOfQuarter()
return With(DefaultClock.Now()).BeginningOfQuarter()
}

// BeginningOfYear beginning of year
func BeginningOfYear() time.Time {
return With(time.Now()).BeginningOfYear()
return With(DefaultClock.Now()).BeginningOfYear()
}

// EndOfMinute end of minute
func EndOfMinute() time.Time {
return With(time.Now()).EndOfMinute()
return With(DefaultClock.Now()).EndOfMinute()
}

// EndOfHour end of hour
func EndOfHour() time.Time {
return With(time.Now()).EndOfHour()
return With(DefaultClock.Now()).EndOfHour()
}

// EndOfDay end of day
func EndOfDay() time.Time {
return With(time.Now()).EndOfDay()
return With(DefaultClock.Now()).EndOfDay()
}

// EndOfWeek end of week
func EndOfWeek() time.Time {
return With(time.Now()).EndOfWeek()
return With(DefaultClock.Now()).EndOfWeek()
}

// EndOfMonth end of month
func EndOfMonth() time.Time {
return With(time.Now()).EndOfMonth()
return With(DefaultClock.Now()).EndOfMonth()
}

// EndOfQuarter end of quarter
func EndOfQuarter() time.Time {
return With(time.Now()).EndOfQuarter()
return With(DefaultClock.Now()).EndOfQuarter()
}

// EndOfYear end of year
func EndOfYear() time.Time {
return With(time.Now()).EndOfYear()
return With(DefaultClock.Now()).EndOfYear()
}

// Monday monday

func Monday(strs ...string) time.Time {
return With(time.Now()).Monday(strs...)
return With(DefaultClock.Now()).Monday(strs...)
}

// Sunday sunday
func Sunday(strs ...string) time.Time {
return With(time.Now()).Sunday(strs...)
return With(DefaultClock.Now()).Sunday(strs...)
}

// EndOfSunday end of sunday
func EndOfSunday() time.Time {
return With(time.Now()).EndOfSunday()
return With(DefaultClock.Now()).EndOfSunday()
}

// Quarter returns the yearly quarter
func Quarter() uint {
return With(time.Now()).Quarter()
return With(DefaultClock.Now()).Quarter()
}

// Parse parse string to time
func Parse(strs ...string) (time.Time, error) {
return With(time.Now()).Parse(strs...)
return With(DefaultClock.Now()).Parse(strs...)
}

// ParseInLocation parse string to time in location
func ParseInLocation(loc *time.Location, strs ...string) (time.Time, error) {
return With(time.Now().In(loc)).Parse(strs...)
return With(DefaultClock.Now().In(loc)).Parse(strs...)
}

// MustParse must parse string to time or will panic
func MustParse(strs ...string) time.Time {
return With(time.Now()).MustParse(strs...)
return With(DefaultClock.Now()).MustParse(strs...)
}

// MustParseInLocation must parse string to time in location or will panic
func MustParseInLocation(loc *time.Location, strs ...string) time.Time {
return With(time.Now().In(loc)).MustParse(strs...)
return With(DefaultClock.Now().In(loc)).MustParse(strs...)
}

// Between check now between the begin, end time or not
func Between(time1, time2 string) bool {
return With(time.Now()).Between(time1, time2)
return With(DefaultClock.Now()).Between(time1, time2)
}
13 changes: 13 additions & 0 deletions now_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,19 @@ func TestQuarter(t *testing.T) {
}
}

type MockClock struct {}

func (*MockClock) Now() time.Time {
return time.Date(2022, time.January, 1, 10, 0, 0, 0, time.Local)
}

func TestClock(t *testing.T) {
assert := assertT(t)

DefaultClock = &MockClock{}
assert(BeginningOfMinute(), "2022-01-01 10:00:00", "TestClock - BeginningOfMinute")
}

func Example() {
time.Now() // 2013-11-18 17:51:49.123456789 Mon

Expand Down