Home > Software engineering >  Time Duration In A Year In Go
Time Duration In A Year In Go

Time:12-07

I have made a function to check when the refresh token has expired. Within the function I also check if the refresh token has been more then a year.

// RefreshTokenExpired :nodoc:
func (s *Session) RefreshTokenExpired() bool {
    // if refresh token has been or more than a year since it was created.
    if s.RefreshTokenExpiredAt.UTC().Sub(s.CreatedAt) >= 365*24*time.Hour { <----
        return true
    }

    ...
}

Is there a better way to write this 365*24*time.Hour?

CodePudding user response:

Use the following code to determine whether s.RefreshTokenExpiredAt is more than a year after s.Created:

 if s.RefreshTokenExpiredAt.After(s.CreatedAt.UTC().Add(1, 0, 0)) {
    return true
}
  •  Tags:  
  • go
  • Related