英文:
How to Make the Specific Block Expired once the validity Period of particular Block is Crossed in Go Language?
问题
mins, _ := time.ParseDuration(fmt.Sprintf("%dmins", v.SalePeriod))
local, _ := time.LoadLocation("Local")
t, _ := time.ParseInLocation("2006-01-02 15:04:05", v.CreateTime, local)
vTime := t.Add(time.Minute * mins)
t.Add(time.Minute * mins) 不起作用,更新的时间没有得到更新
我尝试执行这个操作:vTime := t.Add(time.Minute * mins)
// 但是这个代码块没有过期
if time.Now().Local().After(vTime) {
// 将状态更改为过期
var bodyBytes [][]byte
bodyBytes = append(bodyBytes, []byte(v.ObjectOfSale))
bodyBytes = append(bodyBytes, []byte(v.Seller))
bodyBytes = append(bodyBytes, []byte(v.Buyer))
bodyBytes = append(bodyBytes, []byte("expired"))
// 调用智能合约
resp, err := bc.ChannelExecute("updateSelling", bodyBytes)
if err != nil {
return
}
var data map[string]interface{}
if err = json.Unmarshal(bytes.NewBuffer(resp.Payload).Bytes(), &data); err != nil {
return
}
fmt.Println(data)
}
英文:
mins, _ := time.ParseDuration(fmt.Sprintf("%dmins", v.SalePeriod))
local, _ := time.LoadLocation("Local")
t, _ := time.ParseInLocation("2006-01-02 15:04:05", v.CreateTime, local)
vTime := t.Add(time.Minute * mins)
t.Add(time.Minute * mins) is not working and the updated time is not getting updated
I Tried to Perform this operation : vTime := t.Add(time.Minute * mins)
// But the Piece of Block is not getting Expired
if time.Now().Local().After(vTime) {
//Change status to expired
var bodyBytes [][]byte
bodyBytes = append(bodyBytes, []byte(v.ObjectOfSale))
bodyBytes = append(bodyBytes, []byte(v.Seller))
bodyBytes = append(bodyBytes, []byte(v.Buyer))
bodyBytes = append(bodyBytes, []byte("expired"))
//call smart contract
resp, err := bc.ChannelExecute("updateSelling", bodyBytes)
if err != nil {
return
}
var data map[string]interface{}
if err = json.Unmarshal(bytes.NewBuffer(resp.Payload).Bytes(), &data); err != nil {
return
}
fmt.Println(data)
}
答案1
得分: 1
函数time.ParseDuration
期望一个格式为?m
的字符串,其中?
代表一个数字。
你需要在你的格式字符串中将%dmins
改为%dm
,以使程序正常工作。
链接:
time.ParseDuration
文档:https://pkg.go.dev/time#ParseDuration- 在go playground上的示例解决方案:https://go.dev/play/p/DoChgPHMgN-
编辑
正如@Peter指出的,你还在time.Minute
上有多余的乘法。
英文:
The function time.ParseDuration
expects a string of format ?m
where ?
stands for a number.
You need to change %dmins
to %dm
in your format string to get a program working.
Links:
time.ParseDuration
doc: https://pkg.go.dev/time#ParseDuration- Example solution on go playground: https://go.dev/play/p/DoChgPHMgN-
Edit
As @Peter noted, you also have redundant multiplication on time.Minute
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论