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

HTML-797 Add Velocity date addition and utility functions #252

Merged
merged 4 commits into from Nov 17, 2021

Conversation

ssmusoke
Copy link
Contributor

@ssmusoke ssmusoke commented Nov 17, 2021

Description of what I changed

Add date Velocity addition and utility functions

Issue I worked on

see https://issues.openmrs.org/browse/HTML-797

Checklist: I completed these to help reviewers :)

  • My pull request only contains ONE single commit
    (the number above, next to the 'Commits' tab is 1).

    No? -> read here on how to squash multiple commits into one

  • My IDE is configured to follow the code style of this project.

Yes

  • I have added tests to cover my changes. (If you refactored
    existing code that was well tested you do not have to add tests)

Yes

  • I ran mvn clean package right before creating this pull request and
    added all formatting changes to my commit.

Yes

  • All new and existing tests passed.

Yes

  • My pull request is based on the latest changes of the master branch.

Yes

/**
* Date utility functions useful for HTML forms
*/
public Date yesterday(String date) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ssmusoke I'm not sure what field "6" refers to, but I don't think it's the correct one (at least from a quick look here): https://docs.oracle.com/javase/7/docs/api/constant-values.html#java.util.Calendar.DATE. I would imagine if you added a unit test that spanned a calendar year (eg. "2021-01-03" minus 7 days) it would not pass.

I don't love using the value like this here. I'd rather we just support a couple of general purpose methods. ie. instead of a generic "addInterval", just have an "addDays" and "addMonths" function. Those 2 functions probably cover 99% of all use cases you'll run into. You could also add "addSeconds" if you want to retain more flexibility as a catch-all, as this can be used for addMinutes, addHours, addDays, etc just with simple math. The addMonths function can be used to do addYears just with multiplying by 12. But the String interval is a bit too abstract.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense to me, thanks @mseaton !

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mseaton Thanks for the feedback let me update the PR - makes alot of sense

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mseaton Requested changes added to the PR, I actually feel much better about it now

public Date addDays(Date date, String value) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about this? org.joda.time.LocalDate.fromDateFields(date).plusDays(Integer.parseInt(value))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dkayiwa Works, I had not looked into the Joda time option now I see its an included dependency & much cleaner

Calendar cal = Calendar.getInstance();
cal.setTime(date);

cal.add(Calendar.MONTH, Integer.parseInt(value));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about this? org.joda.time.LocalDate.fromDateFields(date).plusMonths(Integer.parseInt(value))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dkayiwa Thanks update made

Copy link
Member

@mseaton mseaton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Thanks @ssmusoke !

@mseaton mseaton merged commit 9b55c25 into openmrs:master Nov 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants