How can I compare Firestore time with Go time.Now()?

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

How can I compare Firestore time with Go time.Now()?

问题

我使用Go构建我的游戏服务器。我打算比较time.Now()返回的时间和Firestore字段中的时间。在Go中,我该如何做到这一点?

  1. playerDataSnap, err := Instace.FireStoreClient.Collection("PlayerData").Doc(playerUID).Get(Instace.Context)
  2. if err != nil {
  3. log.Printf("EventModeFee Get PlayerData Fail: %v", err)
  4. return
  5. }
  6. playerData := playerDataSnap.Data()
  7. if value, exist := playerData["EventPlayTimes"]; exist {
  8. eventPlayTimes = value.(int64)
  9. }
  10. if discount_Subscribe > 0 {
  11. if value, exist := playerData["SubscriptionExpiredDate"]; exist { //获取订阅到期时间
  12. var expireTimeStamp = value // <-------------来自Firestore字段的时间戳
  13. if time.Now().Before(expireTimeStamp) { //<---------------expireTimeStamp不是有效的类型,但我该如何将其转换为有效的类型
  14. isSubscribed = true
  15. }
  16. }
  17. }

expireTimeStamp不是有效的类型,但我该如何将其转换为有效的类型?

英文:

I use Go to build my game server. I am going to compare a time from time.Now() and one from a Firestore field. How can I do that in Go?

  1. playerDataSnap, err := Instace.FireStoreClient.Collection(&quot;PlayerData&quot;).Doc(playerUID).Get(Instace.Context)
  2. if err != nil {
  3. log.Printf(&quot;EventModeFee Get PlayerData Fail: %v&quot;, err)
  4. return
  5. }
  6. playerData := playerDataSnap.Data()
  7. if value, exist := playerData[&quot;EventPlayTimes&quot;]; exist {
  8. eventPlayTimes = value.(int64)
  9. }
  10. if discount_Subscribe &gt; 0 {
  11. if value, exist := playerData[&quot;SubscriptionExpiredDate&quot;]; exist { //Get Subscribe expired time
  12. var expireTimeStamp = value //&lt;-------------timestamp from firestore field
  13. if time.Now().Before(expireTimeStamp) {//&lt;---------------expireTimeStamp is not a valid type, but How can I convert it to a valid type
  14. isSubscribed = true
  15. }
  16. }
  17. }

expireTimeStamp is not a valid type, but how can I convert it to a valid one?

答案1

得分: 2

原文翻译如下:

原来我可以使用以下代码将 Firestore 字段的时间戳直接断言为 time.Time

  1. if time.Now().Before(expireTimeStamp.(time.Time)) {
  2. isSubscribed = true
  3. }
英文:

It turns out that I can directly assert the timestamp from Firestore field to time.Time with the following code:

  1. if time.Now().Before(expireTimeStamp.(time.Time)) {
  2. isSubscribed = true
  3. }

huangapple
  • 本文由 发表于 2022年4月22日 11:11:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/71963068.html
匿名

发表评论

匿名网友

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

确定