Skip to content

Instantly share code, notes, and snippets.

@mohae
Last active October 19, 2016 17:48
Show Gist options
  • Save mohae/b3078498205b314daede9e98242a1dc6 to your computer and use it in GitHub Desktop.
Save mohae/b3078498205b314daede9e98242a1dc6 to your computer and use it in GitHub Desktop.
deepcopy test time copy
type T struct {
time.Time
}
func TestTimeCopy(t *testing.T) {
tests := []struct{
Y int
M time.Month
D int
h int
m int
s int
nsec int
TZ string
}{
{2016, time.July, 4, 23, 11, 33, 3000, "America/New_York"},
{2015, time.October, 31, 9, 44, 23, 45935, "UTC"},
{2014, time.May, 5, 22, 01, 50, 219300, "Europe/Prague"},
}
for i, test := range tests {
l, err := time.LoadLocation(test.TZ)
if err != nil {
t.Errorf("%d: unexpected error: %s", i, err)
continue
}
var x T
x.Time = time.Date(test.Y, test.M, test.D, test.h, test.m, test.s, test.nsec, l)
c := Copy(x).(T)
if fmt.Sprintf("%p", &c) == fmt.Sprintf("%p", &x) {
t.Errorf("%d: expected the copy to have a different address than the original value; they were the same: %p %p", i, &c, &x)
continue
}
if x.UnixNano() != c.UnixNano() {
t.Errorf("%d: nanotime: got %v; want %v", i, c.UnixNano(), x.UnixNano())
continue
}
if x.Location() != c.Location() {
t.Errorf("%d: location: got %q; want %q", i, c.Location(), x.Location())
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment