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

Add Before() and After() methods to Date type. #4

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

pnawalramka
Copy link
Contributor

@pnawalramka pnawalramka commented May 23, 2023

Adds helpful utility methods Before() and After() to Date type. They are meant to provide similar functionality as standard library's time.Before and time.After methods.

Added tests!

@mpoindexter
Copy link

Do Before and After really need to operate on time.Times? I feel like you could do a lexical comparison given the date format and this would mean that you could compare without returning an error object because you can skip the time parse step.

I also don't love the addition of the Time method - logically speaking a Date encodes a calendar date, not a specific moment in time. For example, once you convert it to a Time, it's now a fixed instant, that may actually be different dates in different timezones. This is not wrong, but I've found that converting between instants and calendar dates to be a good source of errors, so if this is not needed except for the Before and After methods I would leave it out given the above comment.

@pnawalramka
Copy link
Contributor Author

Good point regarding dependency on time.Time. I converted the methods to perform lexical comparison. The error condition still holds though, because there is no format validation on instantiation.

Regarding the Time() method, I found that most users of the library end up converting the Date instance to time.Time anyway. That said, I guess the motivation is to use functionalities similar to those being added in this PR. I think it's reasonable to not make assumptions around timezone and location and leave it up to the caller to make necessary conversions. Removed the method from this PR.

@pnawalramka pnawalramka changed the title Add Time(), Before() and After() methods to Date type. Add Before() and After() methods to Date type. May 24, 2023
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

Successfully merging this pull request may close these issues.

2 participants