Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unexpected output from time.Time

Tags:

go

I just started to learn Go by following a tutorial video on Udemy, and I tried to print the current time as below

import (
  "fmt" 
  "time"
)

func main(){
  t := time.Now()

  fmt.Println(t) 
}

And I get a very long text as the output as below

2018-07-04 12:03:07.2911671 +0800 +08 m=+0.002000201

I was expecting to get only the +0800 followed by a timeZone and that should be the end of it. The expected output is shown below and as it was shown in the tutorial video, too. But for me, the result is in much longer form.

2018-07-04 12:03:07.2911671 +0530 IST

The question is, why does the same command date.Now() return different formats between the instructor's program and mine? Why is there no specific format being set, shouldn't a standardize/base format being returned instead?

like image 808
Isaac Avatar asked Dec 14 '22 15:12

Isaac


2 Answers

The question is, why the same command date.Now() is returning different format between the instructor's program and mine?

Because the tutorial was created before the release of Go 1.9. As of Go 1.9, monotonic clock support was added to the time.Time struct, which added those extra fields.

For normal usage, you should always output time using the Format function, rather than outputting the raw data. This will produce more useful output, and be protected against any future additions to the underlying type.

like image 85
Flimzy Avatar answered Jan 14 '23 11:01

Flimzy


Your Udemy tutorial video is out-of-date. Go is continually updated. For example, a monotonic clock bug fix:

Go 1.9 Release Notes (August 2017)

Transparent Monotonic Time support

The time package now transparently tracks monotonic time in each Time value, making computing durations between two Time values a safe operation in the presence of wall clock adjustments. See the package docs and design document for details.

As always, there are various minor changes and updates to the library, made with the Go 1 promise of compatibility in mind.

time

If a Time value has a monotonic clock reading, its string representation (as returned by String) now includes a final field "m=±value", where value is the monotonic clock reading formatted as a decimal number of seconds.


Package time

import "time"

The Time returned by time.Now contains a monotonic clock reading. If Time t has a monotonic clock reading, t.Add adds the same duration to both the wall clock and monotonic clock readings to compute the result. Because t.AddDate(y, m, d), t.Round(d), and t.Truncate(d) are wall time computations, they always strip any monotonic clock reading from their results. Because t.In, t.Local, and t.UTC are used for their effect on the interpretation of the wall time, they also strip any monotonic clock reading from their results. The canonical way to strip a monotonic clock reading is to use t = t.Round(0).


fmt.Println(t) uses a debugging format so it prints all the underlying time.Time fields.

The canonical way to strip a monotonic clock reading is to use t = t.Round(0).

For example,

package main

import (
    "fmt"
    "time"
)

func main() {
    t := time.Now()
    fmt.Println(t)
    fmt.Println(t.Round(0))

    t2 := time.Now().Round(0)
    fmt.Println(t2)
}

Playground: https://play.golang.org/p/p_pjRWRB8_y

Output:

2009-11-10 23:00:00 +0000 UTC m=+0.000000001
2009-11-10 23:00:00 +0000 UTC
2009-11-10 23:00:00 +0000 UTC
like image 25
peterSO Avatar answered Jan 14 '23 13:01

peterSO