doushan2311 2016-04-03 10:47
浏览 214
已采纳

转到:为什么time.Now()。Hour / Minute / Second返回六位数?

I am learning how to code in Go and trying to create a simple reminder function.

I want to display the current time as a regular 24 hour clock, XX.XX (hours, minutes).

I have saved the current time in a variable t and when I print it I find out that the time is 23.00 early November 2009. Fine, but when I print t.Hour and t.Minute the result is 132288.132480.

It is something similar when I print t.Seconds. I have not been able to figure out why this happens.

Roughly 2000 days have passed since but that is only 48k hours and 2880k minutes so the small difference between the hours and minutes in my result hints that the issue is something else.

I am running the code in the go playground.

My code:

package main

import (
    "fmt"
    "time"
    )

func main() {
    Remind("It's time to eat")
}


func Remind(text string) {
    t := time.Now()
    fmt.Println(t)
    fmt.Printf("The time is %d.%d: ", t.Hour, t.Minute)
    fmt.Printf(text)
    fmt.Println()
}
  • 写回答

1条回答 默认 最新

  • dpvmjk0479 2016-04-03 11:06
    关注

    You need to call t.Hour() instead of using it as a value.

    Check out the source of time package here: https://golang.org/src/time/time.go?s=12994:13018#L390

       399  // Hour returns the hour within the day specified by t, in the range [0, 23].
       400  func (t Time) Hour() int {
       401      return int(t.abs()%secondsPerDay) / secondsPerHour
       402  }
       403  
    

    When in doubt, you can quickly find an explanation by reading specific package source from official go packages page.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办