Time Duration In A Year In Go

huangapple go评论81阅读模式
英文:

Time Duration In A Year In Go

问题

在这段代码中,365*24*time.Hour 表示一年的时间。你可以使用time.Duration来表示这个时间间隔,以使代码更加清晰和易读。下面是修改后的代码:

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

	...
}

这样修改后的代码更易于理解,同时也减少了硬编码的部分。

英文:

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?

答案1

得分: 1

使用以下代码来确定s.RefreshTokenExpiredAt是否在s.Created之后一年以上:

if s.RefreshTokenExpiredAt.After(s.CreatedAt.UTC().Add(1, 0, 0)) {
    return true
}

请注意,这段代码是用Go语言编写的。它通过比较s.RefreshTokenExpiredAts.CreatedAt.UTC().Add(1, 0, 0)来判断是否超过了一年。如果超过了一年,则返回true

英文:

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
}

huangapple
  • 本文由 发表于 2021年12月7日 10:55:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/70254400.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定