Commit bac6c953 authored by Kirill Smelkov's avatar Kirill Smelkov

go/zodb: Tid connection with time

Since in ZODB TIDs are corresponding to time, provide functionality to
convert a tid to timestamp. Do so in exactly the same way as ZODB/py
does for interoperability.
parent 3d13a276
// Copyright (C) 2017 Nexedi SA and Contributors.
// Kirill Smelkov <kirr@nexedi.com>
//
// This program is free software: you can Use, Study, Modify and Redistribute
// it under the terms of the GNU General Public License version 3, or (at your
// option) any later version, as published by the Free Software Foundation.
//
// You can also Link and Combine this program with other software covered by
// the terms of any of the Free Software licenses or any of the Open Source
// Initiative approved licenses and Convey the resulting work. Corresponding
// source of such a combination shall include the source code for all other
// software used.
//
// This program is distributed WITHOUT ANY WARRANTY; without even the implied
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//
// See COPYING file for full licensing terms.
// See https://www.nexedi.com/licensing for rationale and options.
package zodb
// tid connection with time
import (
"time"
)
// TimeStamp is the same as time.Time only .String() is adjusted to be the same as in ZODB/py.
//
// XXX get rid eventually of this and just use time.Time.
type TimeStamp struct {
time.Time
}
func (t TimeStamp) String() string {
return string(t.XFmtString(nil))
}
func (t TimeStamp) XFmtString(b []byte) []byte {
// NOTE UTC() in case we get TimeStamp with modified from-outside location
return t.UTC().AppendFormat(b, "2006-01-02 15:04:05.000000")
}
// Time converts tid to time
func (tid Tid) Time() TimeStamp {
// the same as _parseRaw in TimeStamp/py
// https://github.com/zopefoundation/persistent/blob/aba23595/persistent/timestamp.py#L75
a := uint64(tid) >> 32
b := uint64(tid) & (1 << 32 - 1)
min := a % 60
hour := a / 60 % 24
day := a / (60 * 24) % 31 + 1
month := a / (60 * 24 * 31) % 12 + 1
year := a / (60 * 24 * 31 * 12) + 1900
sec := b * 60 / (1 << 32)
nsec := (b * 60 - (sec << 32)) * 1E9 / (1 << 32)
t := time.Date(
int(year),
time.Month(month),
int(day),
int(hour),
int(min),
int(sec),
int(nsec),
time.UTC)
// round to microsecond: zodb/py does this, and without rounding it is sometimes
// not exactly bit-to-bit the same in text output compared to zodb/py. Example:
// 037969f722a53488: timeStr = "2008-10-24 05:11:08.119999" ; want "2008-10-24 05:11:08.120000"
t = t.Round(time.Microsecond)
return TimeStamp{t}
}
// TODO TidFromTime()
// TODO TidFromTimeStamp()
// TODO TidForNow() ?
// Copyright (C) 2017 Nexedi SA and Contributors.
// Kirill Smelkov <kirr@nexedi.com>
//
// This program is free software: you can Use, Study, Modify and Redistribute
// it under the terms of the GNU General Public License version 3, or (at your
// option) any later version, as published by the Free Software Foundation.
//
// You can also Link and Combine this program with other software covered by
// the terms of any of the Free Software licenses or any of the Open Source
// Initiative approved licenses and Convey the resulting work. Corresponding
// source of such a combination shall include the source code for all other
// software used.
//
// This program is distributed WITHOUT ANY WARRANTY; without even the implied
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//
// See COPYING file for full licensing terms.
// See https://www.nexedi.com/licensing for rationale and options.
package zodb
import "testing"
func TestTidTime(t *testing.T) {
var testv = []struct {tid Tid; timeStr string} {
{0x0000000000000000, "1900-01-01 00:00:00.000000"},
{0x0285cbac258bf266, "1979-01-03 21:00:08.800000"},
{0x0285cbad27ae14e6, "1979-01-03 21:01:09.300001"},
{0x037969f722a53488, "2008-10-24 05:11:08.120000"},
{0x03b84285d71c57dd, "2016-07-01 09:41:50.416574"},
}
for _, tt := range testv {
timeStr := tt.tid.Time().String()
if timeStr != tt.timeStr {
t.Errorf("%v: timeStr = %q ; want %q", tt.tid, timeStr, tt.timeStr)
}
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment