dongwen2794 2015-11-12 05:54
浏览 36
已采纳

去:错误得到两次之间的差异

I have one headache with time. I have above code:

// ticker := time.NewTicker(time.Minute * 1)
    ticker := time.NewTicker(time.Second * 1)
    defer ticker.Stop()

    // new version
    for t := range ticker.C {

        // get duration

        go func() {
            now := time.Now()
            const layout = "2006-01-02 15:04:05"

            endDateStr := fmt.Sprintf("%04d-%02d-%02d 23:45:00", now.Year(), now.Month(), now.Day())
            endDate, _ := time.Parse(layout, endDateStr)

            duration := endDate.Sub(now)

            drHours := (duration.Minutes() - math.Mod(duration.Minutes(), 60)) / 60
            drMinutes := math.Mod(duration.Minutes(), 60)
            //fmt.Println(duration.Hours())

            fmt.Println(now)
            fmt.Println(endDate)

            durStr := fmt.Sprintf("%d:%d:00", uint64(drHours), uint64(drMinutes))
            fmt.Printf("duration: %s
", durStr)
        }()
        fmt.Println(t)
    }

At my comp now and endDate have different time zone:

2015-11-12 10:33:53.9298552 +0500 UZT  // now
2015-11-12 23:45:00 +0000 UTC          // endDate

That's why get wrong duration. How can I set same time zone to both of them ? And a problem again, pay attantion when ticker tik first and second duration has differens 5 hour. Why those problems have occured. What am I doing wrong? Do you have any idea ? Made me happe any hint from you ?

  • 写回答

2条回答 默认 最新

  • doucuo8618 2015-11-12 06:12
    关注

    To avoid DST (Daylight Savings Time) and other errors use UTC for the duration. For example,

    package main
    
    import (
        "fmt"
        "math"
        "time"
    )
    
    func main() {
        ticker := time.NewTicker(time.Second * 1)
        defer ticker.Stop()
    
        for t := range ticker.C {
            go func() {
                now := time.Now().UTC()
    
                const layout = "2006-01-02 15:04:05"
                endDateStr := fmt.Sprintf("%04d-%02d-%02d 23:45:00", now.Year(), now.Month(), now.Day())
                endDate, _ := time.Parse(layout, endDateStr)
    
                duration := endDate.Sub(now)
                drMinutes := math.Mod(duration.Minutes(), 60)
                drHours := (duration.Minutes() - drMinutes) / 60
    
                fmt.Println(now)
                fmt.Println(endDate)
    
                durStr := fmt.Sprintf("%d:%d:00", uint64(drHours), uint64(drMinutes))
                fmt.Printf("duration: %s
    ", durStr)
            }()
            fmt.Println(t.UTC())
        }
    }
    

    Output:

    2015-11-12 06:41:40.123232567 +0000 UTC
    2015-11-12 06:41:40.123409615 +0000 UTC
    2015-11-12 23:45:00 +0000 UTC
    duration: 17:3:00
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配