2

I'm writing a test in Golang which verifies that a journal on disk is properly managed. When I look at the file I see the correct date, but when I look at the date in the test and the date I reload from JSON, they look different:

Timestamp I got with time.Now():

2019-08-06 00:17:46.033527441 -0700 PDT m=+2.582718548

Timestamp I reloaded from the JSON:

2019-08-06 00:17:46.033527441 -0700 PDT

If I change the date to UTC(), it all works as expected (i.e. start with time.Now().UTC()).

I understand that the location is different, I'm just not too sure what the m=... parameter stands for and why would it not be present in the date I reloaded from JSON since it is the exact same date.

So... What is that field?

Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156
  • 1
    It's the [monotonic clock reading](https://golang.org/pkg/time/#hdr-Monotonic_Clocks). Use [Time.Equal](https://golang.org/pkg/time/#Time.Equal) to compare Time values. See [this blog post by Cloudflare](https://new.blog.cloudflare.com/how-and-why-the-leap-second-affected-cloudflare-dns/) which describes the problem that prompted this addition to Go. – Peter Aug 06 '19 at 07:56
  • So... `Time.Equal()` ignores the monotonic clock? I guess I hit a form of a bug in the assert library then... Interesting read about Cloudflare although I knew time can backward since like forever (i.e. I've changed my clocks to a date in the past and caused total mayhem on some computers...) – Alexis Wilke Aug 06 '19 at 08:08
  • 1
    "Time.Equal() ignores the monotonic clock?" Yes. Time.Equal is only concerned with equal instants in time and the monotonic clock reading is irrelevant for telling the time of day (useless, even). It is only useful to measure *durations*, i.e the difference between two time measurements (values acquired with time.Now() or equivalent). – Peter Aug 06 '19 at 08:40

1 Answers1

3
fmt.Println(time.Now().String())

String returns the time formatted using the format string "2006-01-02 15:04:05.999999999 -0700 MST" If the time has a monotonic clock reading, the returned string includes a final field "m=±", where value is the monotonic clock reading formatted as a decimal number of seconds. The returned string is meant for debugging; for a stable serialized representation, use t.MarshalText, t.MarshalBinary, or t.Format with an explicit format string.

Reference


Monotonic Clocks

Operating systems provide both a “wall clock,” which is subject to changes for clock synchronization, and a “monotonic clock,” which is not. The general rule is that the wall clock is for telling time and the monotonic clock is for measuring time. Rather than split the API, in this package the Time returned by time.Now contains both a wall clock reading and a monotonic clock reading; later time-telling operations use the wall clock reading, but later time-measuring operations, specifically comparisons and subtractions, use the monotonic clock reading.

Peter
  • 29,454
  • 5
  • 48
  • 60
wasmup
  • 14,541
  • 6
  • 42
  • 58