Skip to content
This repository was archived by the owner on Jul 22, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 26 additions & 15 deletions hashstructure.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ type HashOptions struct {
// precedence (meaning that if the type doesn't implement fmt.Stringer, we
// panic)
UseStringer bool

// IgnoreTimeLocation produces the same hash for timestamps regardless of the location.
IgnoreTimeLocation bool
}

// Format specifies the hashing process used. Different formats typically
Expand Down Expand Up @@ -117,25 +120,27 @@ func Hash(v interface{}, format Format, opts *HashOptions) (uint64, error) {

// Create our walker and walk the structure
w := &walker{
format: format,
h: opts.Hasher,
tag: opts.TagName,
zeronil: opts.ZeroNil,
ignorezerovalue: opts.IgnoreZeroValue,
sets: opts.SlicesAsSets,
stringer: opts.UseStringer,
format: format,
h: opts.Hasher,
tag: opts.TagName,
zeronil: opts.ZeroNil,
ignorezerovalue: opts.IgnoreZeroValue,
sets: opts.SlicesAsSets,
stringer: opts.UseStringer,
ignoretimelocation: opts.IgnoreTimeLocation,
}
return w.visit(reflect.ValueOf(v), nil)
}

type walker struct {
format Format
h hash.Hash64
tag string
zeronil bool
ignorezerovalue bool
sets bool
stringer bool
format Format
h hash.Hash64
tag string
zeronil bool
ignorezerovalue bool
sets bool
stringer bool
ignoretimelocation bool
}

type visitOpts struct {
Expand Down Expand Up @@ -207,7 +212,13 @@ func (w *walker) visit(v reflect.Value, opts *visitOpts) (uint64, error) {
switch v.Type() {
case timeType:
w.h.Reset()
b, err := v.Interface().(time.Time).MarshalBinary()

timeVal := v.Interface().(time.Time)
if w.ignoretimelocation {
timeVal = timeVal.In(time.UTC)
}

b, err := timeVal.MarshalBinary()
if err != nil {
return 0, err
}
Expand Down
40 changes: 40 additions & 0 deletions hashstructure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,46 @@ func TestHash_hashable(t *testing.T) {
}
}

func TestHash_timestamp(t *testing.T) {
nowUTC := time.Now().In(time.UTC)

asiaTZ, err := time.LoadLocation("Asia/Shanghai")
if err != nil {
t.Fatalf("failed to load location: %s", err)
}
nowAsia := nowUTC.In(asiaTZ)

// compare the same timestamps with different timezones
hashUTC, err := Hash(nowUTC, FormatV2, nil)
if err != nil {
t.Fatalf("failed to hash %#v: %s", nowUTC, err)
}
hashAsia, err := Hash(nowAsia, FormatV2, nil)
if err != nil {
t.Fatalf("failed to hash %#v: %s", nowUTC, err)
}
if hashUTC == hashAsia {
t.Fatalf("bad, expected hashes to be different: %d - %d", hashUTC, hashAsia)
}

// compare the same timestamps with different timezones and ignore timezone
hashUTC, err = Hash(nowUTC, FormatV2, &HashOptions{
IgnoreTimeLocation: true,
})
if err != nil {
t.Fatalf("failed to hash %#v: %s", nowUTC, err)
}
hashAsia, err = Hash(nowAsia, FormatV2, &HashOptions{
IgnoreTimeLocation: true,
})
if err != nil {
t.Fatalf("failed to hash %#v: %s", nowUTC, err)
}
if hashUTC != hashAsia {
t.Fatalf("bad, expected hashes to be the same: %d - %d", hashUTC, hashAsia)
}
}

type testIncludable struct {
Value string
Ignore string
Expand Down