Skip to content

Commit

Permalink
Refactored some if statements
Browse files Browse the repository at this point in the history
  • Loading branch information
SerhiiCho committed Oct 9, 2022
1 parent 23d1542 commit 8952a9a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
8 changes: 8 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,11 @@ func SetConfig(conf Config) {

config = conf
}

func locationIsSet() bool {
return config.Location != ""
}

func locationIsNotSet() bool {
return !locationIsSet()
}
14 changes: 7 additions & 7 deletions timeago.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ func Parse(datetime interface{}, options ...string) string {
return process(input)
}

func parseStringInputIntoTime(date string) time.Time {
if config.Location == "" {
parsedTime, _ := time.Parse("2006-01-02 15:04:05", date)
func parseStringInputIntoTime(datetime string) time.Time {
if locationIsNotSet() {
parsedTime, _ := time.Parse("2006-01-02 15:04:05", datetime)
return parsedTime
}

Expand All @@ -44,15 +44,15 @@ func parseStringInputIntoTime(date string) time.Time {
log.Fatalf("Error in timeago package: %v", err)
}

parsedTime, _ := time.ParseInLocation("2006-01-02 15:04:05", date, location)
parsedTime, _ := time.ParseInLocation("2006-01-02 15:04:05", datetime, location)

return parsedTime
}

func process(datetime time.Time) string {
now := time.Now()

if config.Location != "" {
if locationIsSet() {
now = applyLocationToTime(now)
}

Expand All @@ -73,14 +73,14 @@ func process(datetime time.Time) string {
return calculateTheResult(int(seconds))
}

func applyLocationToTime(date time.Time) time.Time {
func applyLocationToTime(datetime time.Time) time.Time {
location, err := time.LoadLocation(config.Location)

if err != nil {
log.Fatalf("Location error in timeago package: %v\n", err)
}

return date.In(location)
return datetime.In(location)
}

func calculateTheResult(seconds int) string {
Expand Down

0 comments on commit 8952a9a

Please sign in to comment.