Billing anchor dates
MediumOnline Assessment
List the dates on which a subscription is actually charged within a reporting period.
A customer subscribes on start ("YYYY-MM-DD") with a free trial of trial_days days. The first charge is scheduled for start plus trial_days days. That day of the month is the customer's anchor day. Every later charge is scheduled on the anchor day of each following month; if a month is too short to have that day, the charge is scheduled on its last day instead. The schedule always goes back to the anchor day afterwards: an anchor of 31 gives Jan 31, Feb 28 (or 29 in a leap year), Mar 31, Apr 30, and so on.
A charge scheduled on a Saturday or Sunday is collected on the following Monday. This only moves that one collection date, never the schedule.
cancel_on is either an empty string (never cancelled) or a date: a charge is made only if its scheduled date is on or before cancel_on; later scheduled dates are therefore not charged either.
Input: start and cancel_on as above, trial_days (an integer, 0 or more), and from_date and to_date ("YYYY-MM-DD", from_date <= to_date).
Output: a list of dates ("YYYY-MM-DD"), in ascending order, of every charge whose collection date lies between from_date and to_date inclusive. If there are none, return [].
Example 1
- Input:
- ["2024-01-17",14,"","2024-01-01","2024-05-31"]
- Output:
- ["2024-01-31","2024-02-29","2024-04-01","2024-04-30","2024-05-31"]
- Explanation
- A 14-day trial from 2024-01-17 makes the first charge 2024-01-31, so the anchor day is 31. The schedule is Jan 31, Feb 29 (2024 is a leap year), Mar 31, Apr 30 and May 31. Mar 31 2024 is a Sunday, so that charge is collected on Monday April 1. All five charges are collected inside the period.
Example 2
- Input:
- ["2023-01-30",0,"2023-05-29","2023-01-01","2023-12-31"]
- Output:
- ["2023-01-30","2023-02-28","2023-03-30","2023-05-01"]
- Explanation
- The anchor is the 30th: Jan 30, Feb 28 (clamped), then Mar 30 is back on the anchor. Apr 30 2023 is a Sunday and is collected on Monday May 1. The customer cancelled on 2023-05-29, before the charge scheduled for May 30, so that one and everything after it is not made.
Example 3
- Input:
- ["2022-05-10",0,"2022-07-10","2022-06-01","2022-12-31"]
- Output:
- ["2022-06-10","2022-07-11"]
- Explanation
- The first charge is 2022-05-10 (before the period). The charge scheduled for Sunday July 10 is collected on Monday July 11. The customer cancelled on July 10, which is on or before the scheduled date, so that charge is still made; the August charge is scheduled after the cancellation and does not happen.
Constraints
- All dates are valid calendar dates between
2000-01-01and2099-12-31 0 <= trial_days <= 400to_dateis at most 10 years afterfrom_date