Skip to content

Commit

Permalink
refactor(helpers): construct custom clock based on env. use UTC as ti…
Browse files Browse the repository at this point in the history
…mezone in production and Local in dev
  • Loading branch information
jeamon committed Nov 11, 2023
1 parent 9270785 commit dffe346
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
4 changes: 2 additions & 2 deletions app.core.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ func NewApp(start time.Time) (AppProvider, error) {
redisQueue := NewRedisQueue(redisClient)
boltDBConsumer := NewBoltDBConsumer(logger, redisQueue, boltBookStorage)

bookService := NewBookService(logger, config, NewClock(), redisBookStorage, boltBookStorage, redisQueue)
bookService := NewBookService(logger, config, NewClock(config.IsProduction), redisBookStorage, boltBookStorage, redisQueue)
stats := NewStatistics(config.GitTag, config.GitCommit, runtime.Version(), runtime.GOOS+"/"+runtime.GOARCH, IsAppRunningInDocker(), start)
apiService := NewAPIHandler(logger, config, stats, NewClock(), NewIDsHandler(), bookService)
apiService := NewAPIHandler(logger, config, stats, NewClock(config.IsProduction), NewIDsHandler(), bookService)

// Build the map of middlewares stacks.
middlewaresPublic, middlewaresOps := apiService.MiddlewaresStacks()
Expand Down
16 changes: 11 additions & 5 deletions helpers.clock.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,20 @@ type Clocker interface {
}

// Clock implements the Clocker interface.
type Clock struct{}
type Clock struct {
tz *time.Location
}

// NewClock returns a ready to use Clock.
func NewClock() *Clock {
return &Clock{}
// NewClock returns a ready to use Clock with timezone sets
// to UTC in production environment and Local in dev env.
func NewClock(isProd bool) *Clock {
if isProd {
return &Clock{time.UTC}
}
return &Clock{time.Local}
}

// Now provides current clock time.
func (ck *Clock) Now() time.Time {
return time.Now()
return time.Now().In(ck.tz)
}

0 comments on commit dffe346

Please sign in to comment.