AddBusinessDays and GetBusinessDays
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
AddBusinessDays and GetBusinessDays are common date utilities used in invoicing, shipping, payroll, and scheduling systems. They sound simple until weekends, holidays, negative offsets, and inclusive-versus-exclusive counting rules enter the picture. A solid implementation starts by defining what counts as a business day and then applying that rule consistently in both directions.
Define the Business-Day Rule First
Before writing code, decide exactly what your application means by a business day.
Typical rules:
- Monday through Friday are working days.
- Saturday and Sunday are excluded.
- A configurable holiday set is also excluded.
- The start day may or may not count, depending on the API contract.
If two functions use slightly different rules, your system produces inconsistent due dates and date differences.
Example Implementation in Python
The simplest approach is to move one day at a time and skip non-business dates.
This is easy to reason about and correct for moderate workloads.
Count Business Days Between Two Dates
GetBusinessDays usually means counting working days in a date range. You need to define whether the count includes the start date, the end date, or both.
One clear contract is: count business days from start up to but not including end.
If your application needs a different inclusion rule, change the contract and document it explicitly.
Holidays Should Be Data, Not Hardcoded Logic
Weekend logic is stable, but holidays vary by country, business unit, and year. Put holidays in data structures, configuration files, or a database table rather than scattering them across conditionals.
Example JSON-like source:
That makes testing easier and allows business users to update schedules without rewriting date logic.
Performance Considerations
Day-by-day iteration is usually fine for due dates and short ranges, but it becomes expensive for large date spans or batch calculations.
Practical options:
- keep the simple loop for correctness-first code
- precompute holiday sets for fast lookup
- optimize with week-based arithmetic only after tests exist
The most common mistake is writing a clever optimized formula before confirming the business rules.
Negative Offsets and Reverse Ranges
Real systems often need to go backward as well as forward.
Examples:
- subtract 5 business days from a settlement date
- compute business days remaining until a deadline
- compare SLA dates in reverse order
If your code handles only positive increments, it will break once backdating or rollback features appear. Build reverse traversal into the function from the beginning.
Testing Edge Cases
Business-day utilities should always have tests around boundaries.
Weekend crossings, holiday crossings, and zero-length ranges are the first cases to lock down.
Common Pitfalls
- Not defining whether the start date counts. Fix: document the counting contract before writing code.
- Hardcoding holiday rules in logic. Fix: treat holidays as data input.
- Supporting only positive day offsets. Fix: handle negative values with the same function.
- Optimizing too early with complicated arithmetic. Fix: start with a correct loop and profile later.
- Forgetting to test month-end and year-end boundaries. Fix: add explicit regression tests around weekends and holidays.
Summary
- Business-day utilities depend on clear counting rules.
- Keep weekend logic separate from holiday data.
- Support both forward and backward movement through dates.
- Start with a correct implementation before optimizing.
- Test inclusion rules and calendar boundaries explicitly.

