Skip to content

time: Time.MarshalBinary loses monotonic component #20255

@ggaaooppeenngg

Description

@ggaaooppeenngg

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsDecisionFeedback is required from experts, contributors, and/or the community before a change can be made.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions