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

Create service to consolidate date ranges #110

Merged
merged 4 commits into from
Feb 9, 2023

Conversation

StuAA78
Copy link
Contributor

@StuAA78 StuAA78 commented Feb 9, 2023

https://eaflood.atlassian.net/browse/WATER-3895

As part of our work to create transaction lines for sroc, we need to be able to take a set of dates ranges and consolidate them into a set of non-overlapping ranges. This PR implements ConsolidateDateRangesService to do this.

The service takes an array of date ranges, where each date range is an object with a startDate and endDate (each of which is a regular Date object). For example:

[
  { startDate: 2023-01-01, endDate: 2023-03-01 }, // Range 1
  { startDate: 2023-02-01, endDate: 2023-04-01 }, // Range 2
  { startDate: 2023-06-01, endDate: 2023-07-01 }  // Range 3
]

It merges any overlapping date ranges, leaves alone any which don't overlap, and returns an array of the resulting ranges. So the date ranges above consolidate to:

[
  { startDate: 2023-01-01, endDate: 2023-04-01 }, // Ranges 1 & 2 merged
  { startDate: 2023-06-01, endDate: 2023-07-01 }  // Range 3 unchanged
]

Note that if a range starts on the same day the previous range ends, that is classed as one continuous range. But a range that starts the day after the previous range ends is classed as a separate range to that previous one. For example:

[
  { startDate: 2023-01-01, endDate: 2023-04-01 }, // Range 1, ending on 1st April
  { startDate: 2023-04-01, endDate: 2023-07-01 }, // Range 2, starting on 1st April
  { startDate: 2023-10-01, endDate: 2023-10-31 }, // Range 3, ending on 31st October
  { startDate: 2023-11-01, endDate: 2023-12-01 }  // Range 4, starting on 1st November
]

Consolidates to:

[
  { startDate: 2023-01-01, endDate: 2023-07-01 }, // Ranges 1 & 2 merged
  { startDate: 2023-10-01, endDate: 2023-10-31 }, // Range 3 unchanged
  { startDate: 2023-11-01, endDate: 2023-12-01 }  // Range 4 unchanged
]

One last thing to note is that, due to the way the merging has been implemented, the returned array of date ranges is sorted in order from earliest start date to latest start date.

https://eaflood.atlassian.net/browse/WATER-3895

As part of our work to create transaction lines for sroc, we need to be able to take a set of dates ranges and consolidate them into a set of non-overlapping ranges.
@StuAA78 StuAA78 added the enhancement New feature or request label Feb 9, 2023
@StuAA78 StuAA78 self-assigned this Feb 9, 2023
@StuAA78 StuAA78 marked this pull request as ready for review February 9, 2023 15:31
Copy link
Member

@Cruikshanks Cruikshanks left a comment

Choose a reason for hiding this comment

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

Amazing turnaround!

@StuAA78 StuAA78 merged commit 698a56d into main Feb 9, 2023
@StuAA78 StuAA78 deleted the create-consolidate-dates-service branch February 9, 2023 21:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants