8

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?

kolypto
  • 31,774
  • 17
  • 105
  • 99
Isaac
  • 12,042
  • 16
  • 52
  • 116
  • @Manjunath: but of all the variant I can't seems to find what is m=+xxxxxx – Isaac Jul 04 '18 at 04:21
  • @Isaac as other answers its location and clock.. for formating you can check this link which has examples https://gobyexample.com/time-formatting-parsing and for formats you can refer link before – MJN Jul 04 '18 at 05:06
  • See also: https://stackoverflow.com/questions/52809661/what-is-m-in-go-time-string – kolypto Aug 10 '23 at 11:10

3 Answers3

9

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.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
6

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
peterSO
  • 158,998
  • 31
  • 281
  • 276
1

The +08 is the string returned by t.Location().String(). Locations are given a string on creation which is used to identify it. It could be IST, or it can be "+08" or any other string you can think of.

The m=+0.002000201 is the monotonic clock. It is used for more accurate durations. For more information on Go's monotonic clock implementations, see https://golang.org/pkg/time/#hdr-Monotonic_Clocks.

As for the reason the monotonic clock shows up in t.String():

For debugging, the result of t.String does include the monotonic clock reading if present. If t != u because of different monotonic clock readings, that difference will be visible when printing t.String() and u.String().

Stephen Weinberg
  • 51,320
  • 14
  • 134
  • 113
  • More about monotonic clocks: https://stackoverflow.com/questions/3523442/difference-between-clock-realtime-and-clock-monotonic – P Ackerman Jul 04 '18 at 04:35
  • @StephenWeinberg: The reason for this question is that I was following the tutorial from udemy and the instructor's `time.now()` is different from my output. – Isaac Jul 04 '18 at 05:35
  • Correct me if im wrong, since both of us did not specify any specific format, I was expecting a `standard/base` format by `date.Now()` – Isaac Jul 04 '18 at 05:37