Consider the program below.

I want to compute the duration between a time that I put in the 
"old_time_str" variable and the current time. If I put the current time in 
a string, the program shows the correct duration. If I get the current time 
using time.Now() then the duration shown is 7 hours too long (I'm in PDST). 
I'd like both methods to return the correct duration. Let's presume that  
both times are in the same time zone.

What am I doing wrong?

Cordially,
Jon Forrest

----------------------------
package main

import (
        "fmt"
        "time"
)

var old_time_str string = "Thu Apr 20 20:26:40 2023"
var new_time_str string = "Fri Apr 21 17:07:51 2023"
var time_format string = `Mon Jan 02 15:04:05 2006`

func main() {
        var new_time_1 time.Time
        var new_time_2 time.Time
        var old_time time.Time
        var diff time.Duration

        old_time, _ = time.Parse(time_format, old_time_str)
        new_time_1, _ = time.Parse(time_format, new_time_str)

        fmt.Printf("Old time is %s\n", old_time.Format(time_format))
        fmt.Printf("New time 1 is %s\n", new_time_1.Format(time_format))

        diff = new_time_1.Sub(old_time)
        fmt.Printf("diff 1: %s\n\n", diff.String())

        new_time_2 = time.Now()
        fmt.Printf("Old time is still %s\n", old_time.Format(time_format))
        fmt.Printf("New time 2 is %s\n", new_time_2.Format(time_format))

        diff = new_time_2.Sub(old_time)
        fmt.Printf("diff 2: %s\n", diff.String())
}

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/38c5c578-f540-4ad4-8b8d-9f5f85ea5ef1n%40googlegroups.com.

Reply via email to