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

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

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

问题

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

playerDataSnap, err := Instace.FireStoreClient.Collection("PlayerData").Doc(playerUID).Get(Instace.Context)
if err != nil {
    log.Printf("EventModeFee Get PlayerData Fail: %v", err)
    return
}
playerData := playerDataSnap.Data()
if value, exist := playerData["EventPlayTimes"]; exist {
    eventPlayTimes = value.(int64)
}

if discount_Subscribe > 0 {
    if value, exist := playerData["SubscriptionExpiredDate"]; exist { //获取订阅到期时间
        var expireTimeStamp = value // <-------------来自Firestore字段的时间戳

        if time.Now().Before(expireTimeStamp) { //<---------------expireTimeStamp不是有效的类型,但我该如何将其转换为有效的类型
            isSubscribed = true
        }
    }
}

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?

		playerDataSnap, err := Instace.FireStoreClient.Collection(&quot;PlayerData&quot;).Doc(playerUID).Get(Instace.Context)
	if err != nil {
		log.Printf(&quot;EventModeFee Get PlayerData Fail: %v&quot;, err)
		return
	}
	playerData := playerDataSnap.Data()
	if value, exist := playerData[&quot;EventPlayTimes&quot;]; exist {
		eventPlayTimes = value.(int64)
	}

	if discount_Subscribe &gt; 0 {
		if value, exist := playerData[&quot;SubscriptionExpiredDate&quot;]; exist { //Get Subscribe expired time
			var expireTimeStamp = value //&lt;-------------timestamp from firestore field

			if time.Now().Before(expireTimeStamp) {//&lt;---------------expireTimeStamp is not a valid type, but How can I convert it to a valid type
				isSubscribed = true
			}
		}
	}

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

答案1

得分: 2

原文翻译如下:

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

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

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

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

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:

确定