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

valast: feature Request: custom time.Time/datatype result generation and comparison #27

Open
kokizzu opened this issue Oct 21, 2022 · 1 comment
Labels

Comments

@kokizzu
Copy link

kokizzu commented Oct 21, 2022

Background: saving to database and generating the result giving different diff because of timezone difference

minimal code to reproduce:

package foo
import "time"
import "github.com/hexops/autogold"
import "testing"
func TestFoo(t *testing.T) {
        const layout = "Jan 2, 2006 at 3:04pm (MST)"
        tm, _ := time.Parse(layout, "Feb 4, 2014 at 6:05pm (PST)")
        t2 := tm.UTC() // assuming this result queried from database from column that doesn't store timezone

        println(tm.UnixNano(), t2.UnixNano(), tm.UnixNano() == t2.UnixNano()) // both are inherently equal
       
        want := autogold.Want("internally equal", t2)
        want.Equal(t, tm)
}

it shows:

1391537100000000000 1391537100000000000 true
--- FAIL: TestFoo (0.06s)
    want.go:200: mismatch (-want +got):
        --- want
        +++ got
        @@ -1 +1,10 @@
        -time.Time{ext: 63527133900}
        \ No newline at end of file
        +time.Time{ext: 63527133900, loc: &time.Location{
        +       name: "PST",
        +       zone: []time.zone{time.zone{
        +               name: "PST",
        +       }},
        +       tx:         []time.zoneTrans{time.zoneTrans{when: -9223372036854775808}},
        +       cacheStart: -9223372036854775808,
        +       cacheEnd:   9223372036854775807,
        +       cacheZone:  &time.zone{name: "PST"},
        +}}
        \ No newline at end of file

FAIL
FAIL    command-line-arguments  0.060s
FAIL

where it inherently the same.

  1. Is there a way to customize the generated struct to be using time.Unix(sec, nanosec) instead of full struct since &time.Time{ext:123} has unexported field?
  2. Is there a way to customize the comparison of value so it would consider inherently equal time ignoring the timezone as equal?
@slimsag
Copy link
Member

slimsag commented Oct 21, 2022

This is a bug in valast, which is what does the struct formatting: hexops/valast#11 (comment)

In practice, though, I suggest that when your data structures become this complex you bail out of the default formatting and instead create a simpler struct (or string) just for passing to valast.

@slimsag slimsag added the valast label Dec 20, 2022
@slimsag slimsag changed the title Feature Request: custom time.Time/datatype result generation and comparison valast: feature Request: custom time.Time/datatype result generation and comparison Dec 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants