Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

time: Time.MarshalBinary loses monotonic component #20255

Closed
ggaaooppeenngg opened this issue May 5, 2017 · 1 comment
Closed

time: Time.MarshalBinary loses monotonic component #20255

ggaaooppeenngg opened this issue May 5, 2017 · 1 comment
Labels
FrozenDueToAge NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made.
Milestone

Comments

@ggaaooppeenngg
Copy link
Contributor

ggaaooppeenngg commented May 5, 2017

Please answer these questions before submitting your issue. Thanks!

What version of Go are you using (go version)?

go version devel +8db4d02 Fri Apr 28 07:27:25 2017 +0000 darwin/amd64

What operating system and processor architecture are you using (go env)?

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/gaopeng/Workplace/GOPATH"
GORACE=""
GOROOT="/Users/gaopeng/Workplace/go"
GOTOOLDIR="/Users/gaopeng/Workplace/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/mf/smd0sz4s6b13k6fg88_nc1240000gn/T/go-build220820873=/tmp/go-build -gno-record-gcc-switches -fno-common"
CXX="clang++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"

What did you do?

By adding patch in src/time/time.go

+func (t Time) GetWallAndExt() (uint64, int64) {
+       return t.wall, t.ext
+}
+
+func (t *Time) SetWallAndExt(wall uint64, ext int64) {
+       t.wall = wall
+       t.ext = ext
+}

I run this program

package main

import (
	"bytes"
	"encoding/gob"
	"fmt"
	"time"
)

// 13707589861482070995 464861
// 610436051 63629562089
func main() {
	var buf bytes.Buffer
	t := time.Now()
	t.SetWallAndExt(13707589861482070995, 464861)
	fmt.Println(t)
	fmt.Println(gob.NewEncoder(&buf).Encode(t))
	var t1 time.Time
	fmt.Println(gob.NewDecoder(&buf).Decode(&t1))

	w, e := t.GetWallAndExt()
	fmt.Printf("0x%b 0x%b\n", w, e)
	fmt.Println(t)

	w, e = t1.GetWallAndExt()
	fmt.Printf("0x%b 0x%b\n", w, e)
	fmt.Println(t1)
}

What did you expect to see?

0x1011111000111011001001000101101001100100011000101000001111010011 0x1110001011111011101
2017-05-05 14:21:29.610436051 +0800 CST m=+0.000464861
0x100100011000101000001111010011 0x111011010000100111100001000011101001
2017-05-05 14:21:29.610436051 +0800 CST m=+0.000464861

What did you see instead?

0x1011111000111011001001000101101001100100011000101000001111010011 0x1110001011111011101
2017-05-05 14:21:29.610436051 +0800 CST m=+0.000464861
0x100100011000101000001111010011 0x111011010000100111100001000011101001
2017-05-05 14:21:29.610436051 +0800 CST

BTW, I found related issue #12914 and #19502, and I propose to rm final field m=±<value> in Time.String(), since we should follow the way wall clock is used to show time, and monotonic clock is used to comparison.

@ggaaooppeenngg ggaaooppeenngg changed the title gob encode encode monotonic time to wall time time: BinaryMarshal encodes monotonic time to wall time May 5, 2017
@bradfitz bradfitz changed the title time: BinaryMarshal encodes monotonic time to wall time time: BinaryMarshal loses monotonic component of Time May 5, 2017
@bradfitz bradfitz added this to the Go1.9 milestone May 5, 2017
@bradfitz bradfitz added the NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. label May 5, 2017
@bradfitz bradfitz changed the title time: BinaryMarshal loses monotonic component of Time time: Time.MarshalBinary loses monotonic component May 5, 2017
@bradfitz
Copy link
Contributor

bradfitz commented May 5, 2017

Actually, this is intentional. The monotonic component of a Time value is only meaningful within a current process for comparison with other such Times. It should not be serialized and read later.

@bradfitz bradfitz closed this as completed May 5, 2017
@golang golang locked and limited conversation to collaborators May 5, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made.
Projects
None yet
Development

No branches or pull requests

3 participants