如何在Go语言中使特定块在其有效期过期后失效?

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

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:


Edit

As @Peter noted, you also have redundant multiplication on time.Minute

huangapple
  • 本文由 发表于 2022年11月26日 17:56:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/74581191.html
匿名

发表评论

匿名网友

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

确定