Go: error getting difference between two time

Ulug'bek Ro'zimboyev :

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\n", 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 ?

peterSO :

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\n", 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

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Error getting the string between two patterns

分類Dev

Find Time Difference between two dd/mm/yyyy hh:mm

分類Dev

Strange difference in time between add and extend two lists

分類Dev

Finding difference between time

分類Dev

Difference between two lists

分類Dev

operator= between two std::chrono::time_point cause error

分類Dev

Getting everything between two characters

分類Dev

How can I avoid the difference of milliseconds between two Chronometers start time?

分類Dev

How to get the difference between two date and times and show the time running down

分類Dev

Find closest timestamps between two dataframes and merge different columns when time difference is < 60s

分類Dev

I need to get the difference between two time stamps when I post comment on the page

分類Dev

Difference between two pointer variables

分類Dev

Difference between two Sorted Arrays

分類Dev

Is there any difference between these two ways?

分類Dev

Difference between two tables as a table

分類Dev

What is the difference between these two instances?

分類Dev

What is the difference between these two examples?

分類Dev

Difference between two dates on Linux

分類Dev

What is the difference between these two syntax?

分類Dev

What is the difference between these two commands?

分類Dev

Aggregate function, group by clause error when summing difference between two columns

分類Dev

Difference between plain go func and for loop in go func

分類Dev

Difference between defining multiple variable at a time and individually

分類Dev

Finding the difference between "pwdLastSet" and current date/time

分類Dev

how to calculate difference between time in pandas

分類Dev

Postgresql calculating minimal difference in time between records

分類Dev

calculate time difference between certain events in R

分類Dev

Getting the common character count between two strings

分類Dev

Flutter: Finding Difference Between Two Dates

Related 関連記事

  1. 1

    Error getting the string between two patterns

  2. 2

    Find Time Difference between two dd/mm/yyyy hh:mm

  3. 3

    Strange difference in time between add and extend two lists

  4. 4

    Finding difference between time

  5. 5

    Difference between two lists

  6. 6

    operator= between two std::chrono::time_point cause error

  7. 7

    Getting everything between two characters

  8. 8

    How can I avoid the difference of milliseconds between two Chronometers start time?

  9. 9

    How to get the difference between two date and times and show the time running down

  10. 10

    Find closest timestamps between two dataframes and merge different columns when time difference is < 60s

  11. 11

    I need to get the difference between two time stamps when I post comment on the page

  12. 12

    Difference between two pointer variables

  13. 13

    Difference between two Sorted Arrays

  14. 14

    Is there any difference between these two ways?

  15. 15

    Difference between two tables as a table

  16. 16

    What is the difference between these two instances?

  17. 17

    What is the difference between these two examples?

  18. 18

    Difference between two dates on Linux

  19. 19

    What is the difference between these two syntax?

  20. 20

    What is the difference between these two commands?

  21. 21

    Aggregate function, group by clause error when summing difference between two columns

  22. 22

    Difference between plain go func and for loop in go func

  23. 23

    Difference between defining multiple variable at a time and individually

  24. 24

    Finding the difference between "pwdLastSet" and current date/time

  25. 25

    how to calculate difference between time in pandas

  26. 26

    Postgresql calculating minimal difference in time between records

  27. 27

    calculate time difference between certain events in R

  28. 28

    Getting the common character count between two strings

  29. 29

    Flutter: Finding Difference Between Two Dates

ホットタグ

アーカイブ