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

NewDateTime returns a unix 0, but IsZero checks for zero time.Time #141

Open
prashantv opened this issue Jan 10, 2025 · 0 comments
Open

Comments

@prashantv
Copy link

Repro:

dt := strfmt.NewDateTime()
fmt.Println("iszero", dt.IsZero())

Output:

iszero false

Want:

iszero true

The NewDateTime documentation says:

NewDateTime is a representation of zero value for DateTime type

and IsZero says:

IsZero returns whether the date time is a zero value

Given these docs, I expected that NewDateTime() would match the zero value (e.g., var d DateTime), and that IsZero() would be true for it. However, that's not the case as NewDateTime() returns Unix(0, 0) which is different from a zero DateTime.

A couple of approaches to fix this:

Approach 1: return time.Time{} in NewDateTime
Rather than initializing using time.Unix(0, 0), NewDateTime could return time.Time{}, which would better align with the docs, as it suggests that NewDateTime() should match var d strfmt.DateTime which it doesn't currently.

However, this likely has a larger impact for users of NewDateTime since the value returned is changing.

IMO this is the more correct fix.

Approach 2: Update IsZero
IsZero could be updated to check for IsUnixZero() in addition to checking for zero time.Time.

I would also recommend the documentation for NewDateTime is updated to indicate that it uses Unix(0, 0) rather than matching a zero DateTime.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant