英文:
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("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 { //Get Subscribe expired time
var expireTimeStamp = value //<-------------timestamp from firestore field
if time.Now().Before(expireTimeStamp) {//<---------------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
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论