
Guides
Part of How to calculate working hours between two timestamps in Excel and Google Sheets
Designing a working hours API: duration, add, is_open and next_open
A working hours endpoint answers four questions: duration, add, is_open and next_open. Edge rules, a request and response shape with segments, a holiday feed, and test vectors.
A working-hours endpoint is judged at its edges: a moment outside the window, a moment during a break, a span over a weekend or a clock change. Four operations cover the questions callers ask. Each needs its edge cases decided and written down.
The arithmetic is short. Getting two callers to the same answer is the design problem, and the contract solves it, not the code.
No standard defines this endpoint. Editorial suggestion: read the edge rules below as one consistent design, chosen so that the four operations never disagree about the same minute, and not as a specification you must follow. The timestamp, time zone and holiday-feed facts are taken from the cited sources.
What to take away
- Four operations cover the questions
duration,add,is_openandnext_open. All four read one schedule object. - Treat each open interval as including its start instant and excluding its end instant. That one rule settles
is_openat 17:00,next_openat closing, andaddwhen a budget ends exactly at closing. - Require an RFC 3339 timestamp with an offset on every request, and keep the zone in the schedule, as an IANA name.
- Return the segments with every answer, plus the schedule id and version.
- A holiday feed is input data, not the schedule. Filter it, de-duplicate it, and store the fetch date with the version.
The four operations and their edge rules
| Operation | Question | Edge rule to write down |
|---|---|---|
duration | How many open hours lie between two moments? | Clip both ends to the open intervals. to before from is an error. |
add | What moment is N open hours after this one? | Start counting at the next opening if the start is closed. A budget that ends exactly at closing returns the closing instant. |
is_open | Is the schedule open at this moment? | True at an opening instant, false at a closing instant, false during a break. |
next_open | When does the schedule next open? | Returns the input if it is open, otherwise the start of the next interval. |
duration and add are inverses only where the endpoints are inside an interval. Adding zero hours to a closed moment has two defensible answers: the moment itself, or the next opening. The table picks the next opening. Whichever you pick, publish it and test it.
A break is a gap between two intervals, so is_open returns false during one. If it returned true, duration and is_open would disagree about the same minute.
Editorial suggestion: reject negative hours in add and offer a separate subtract operation. A signed argument hides a different set of boundary cases, such as whether a start exactly at opening belongs to the previous day. The day-level version of this design question is covered in adding business days.
The schedule object
Model the week as a list of intervals per weekday, not as one open time and one close time. A lunch break, a split shift and a short Friday are then the same feature.
schedule:
id, version
zone: "America/Chicago"
weekly:
mon: [ ["09:00","17:00"] ] (tue to fri the same)
sat: []
sun: []
exceptions: keyed by plain date
"2027-03-10": [ ["09:00","12:00"] ] a short day
"2027-03-11": [] closed
An empty list is a closed day, so Sunday and a public holiday take the same code path. An exception replaces the weekly pattern for its date completely. A half day is easier to state as its own interval than as an instruction to subtract part of the usual ones. Where closure dates come from and who publishes them is covered in country calendars.
Local times in the schedule are wall-clock times in its zone. The service turns them into instants with the zone's rules and counts elapsed time between instants. That is how a day with a clock change is handled.
Timestamps, zones and dates
Take moments as RFC 3339 timestamps, the Internet profile of ISO 8601. Its grammar requires a T, seconds, and an offset that is either Z or a signed hour and minute, as in 2027-03-05T15:00:00-06:00. A local time with no offset is not in the grammar, so the service cannot tell which moment it means. Editorial suggestion: answer with a 400 error.
The zone belongs to the schedule, not the caller. Use a name from the IANA time zone database, such as America/Chicago. IANA's theory document says a zone's offset from UTC can change over time and that a zone can have more than two clock changes a year. A schedule pinned to an offset drifts when the rules change. The offsets in the examples below, -06:00 and -05:00, are what that zone uses in March 2027.
Echo timestamps in the schedule's zone. Editorial suggestion: you may append the zone name in the format of RFC 9557, as in 2027-03-08T11:00:00-06:00[America/Chicago], so a reader sees the rule behind the offset. Say in the contract whether clients may rely on it.
Exceptions are keyed by a plain date in YYYY-MM-DD form. That is a calendar date, not a moment. Mixing the two is a common source of off-by-one errors, and the difference is explained in the date difference formula.
Request and response shape
A duration request and its response:
POST /v1/working-hours/duration
{ "schedule": { "id": "support-us", "version": "2026-10-25.1" },
"from": "2027-03-05T15:00:00-06:00",
"to": "2027-03-08T11:00:00-06:00" }
200
{ "operation": "duration",
"result": { "value": 4, "unit": "business_hours" },
"applied": { "schedule_id": "support-us", "schedule_version": "2026-10-25.1",
"zone": "America/Chicago",
"from": "2027-03-05T15:00:00-06:00", "to": "2027-03-08T11:00:00-06:00" },
"segments": [
{ "date": "2027-03-05", "start": "2027-03-05T15:00:00-06:00",
"end": "2027-03-05T17:00:00-06:00", "hours": 2 },
{ "date": "2027-03-08", "start": "2027-03-08T09:00:00-06:00",
"end": "2027-03-08T11:00:00-06:00", "hours": 2 } ] }
The id and version shown are illustrative. add takes start and hours and returns a timestamp in result. is_open returns a boolean. next_open returns a timestamp. All four return applied and segments, so a caller can tell a wrong window from a wrong span without reimplementing the service.
Return the segments on every call, not behind a flag. Accept a schedule inline or by id and version, and echo which one was used. The version is what makes a reply reproducible months later.
Editorial suggestion: the status codes and error names below are examples for this design, not values from a standard.
Loading closures from a public holiday API
Keep closures in the schedule's exceptions, fed from a published list. Nager.Date is a public holiday API. Its GitHub project shows the v3 record, in its client examples, as date, localName, name, countryCode, fixed, global, counties and types, and the live response also carries launchYear. The request is GET /api/v3/PublicHolidays/{year}/{countryCode}. One US record from the 2027 response, retrieved 25 October 2026:
{"date":"2027-06-18","localName":"Juneteenth National Independence Day",
"name":"Juneteenth National Independence Day","countryCode":"US","fixed":false,
"global":true,"counties":null,"launchYear":null,"types":["Public","Bank"]}
Three points matter when mapping this to a schedule.
- The list is wider than a closure list.The same response holds rows with
global: falseand acountieslist, and types such asObservance,Optional,SchoolandAuthorities. The newer v4 documentation lists the types as Public, Bank, School, Authorities, Optional and Observance, and renames the fields (nationalHoliday,subdivisionCodes,holidayTypes). The older host, date.nager.at, now redirects its documentation page to that site. - One date can appear several times.For 2027-10-11 the US response has three rows: Columbus Day for a list of states, Indigenous Peoples' Day for another list, and a
globalColumbus Day row typedBank. De-duplicate by date before writing exceptions. - Dates are observed dates.Keeping only
global: truerows and de-duplicating gave eleven dates: 1 Jan, 18 Jan, 15 Feb, 31 May, 18 Jun, 5 Jul, 6 Sep, 11 Oct, 11 Nov, 25 Nov and 24 Dec 2027. They match the 2027 federal holiday schedule from OPM, including Friday 18 June, Monday 5 July and Friday 24 December, which are observed days. That check covers the US federal list only. A private employer's closures are its own decision.
Editorial suggestion: write each kept date as exceptions[date] = [], and put the source, the filter and the fetch date in the schedule's version notes. A later fetch may change the list, and the version is how you know which list an old answer used.
Test vectors
Schedule A is Monday to Friday, 09:00 to 17:00, America/Chicago, no exceptions. Clocks in the US move forward at 2 a.m. on Sunday 14 March 2027, under 15 U.S.C. 260a, so dates before it use -06:00 and dates after it use -05:00.
duration on schedule A
| From | To | Expect |
|---|---|---|
| 2027-03-05T15:00:00-06:00 | 2027-03-08T11:00:00-06:00 | 4 |
| 2027-03-05T17:00:00-06:00 | 2027-03-08T09:00:00-06:00 | 0 |
| 2027-03-06T10:00:00-06:00 | 2027-03-07T16:00:00-06:00 | 0 |
| 2027-03-04T08:00:00-06:00 | 2027-03-04T18:00:00-06:00 | 8 |
| 2027-03-05T12:15:00-06:00 | 2027-03-09T10:45:00-06:00 | 14.5 |
| 2027-03-01T09:00:00-06:00 | 2027-03-31T17:00:00-05:00 | 184 |
| 2027-03-12T15:00:00-06:00 | 2027-03-15T11:00:00-05:00 | 4 |
| 2027-03-05T21:00:00Z | 2027-03-08T17:00:00Z | 4 |
A nonzero result on row two means the ends are not clipped. Row six is 23 weekdays times eight. Row seven spans the clock change and still gives 4, though the elapsed time is 67 hours. Row eight is row one written in UTC and must match it.
add, is_open and next_open on schedule A
| Operation | Input | Expect |
|---|---|---|
add | 4 h from 2027-03-05T16:30:00-06:00 | 2027-03-08T12:30:00-06:00 |
add | 8 h from 2027-03-06T10:00:00-06:00 | 2027-03-08T17:00:00-06:00 |
add | 0 h from 2027-03-06T10:00:00-06:00 | 2027-03-08T09:00:00-06:00 |
add | 0 h from 2027-03-04T10:00:00-06:00 | 2027-03-04T10:00:00-06:00 |
add | 8 h from 2027-03-12T15:00:00-06:00 | 2027-03-15T15:00:00-05:00 |
is_open | 2027-03-04T09:00:00-06:00 | true |
is_open | 2027-03-04T16:59:59-06:00 | true |
is_open | 2027-03-04T17:00:00-06:00 | false |
is_open | 2027-03-06T10:00:00-06:00 | false |
next_open | 2027-03-05T18:00:00-06:00 | 2027-03-08T09:00:00-06:00 |
next_open | 2027-03-04T10:00:00-06:00 | 2027-03-04T10:00:00-06:00 |
Schedule B adds a break: 09:00 to 12:30 and 13:30 to 17:00 on weekdays.
| Operation | Input | Expect |
|---|---|---|
duration | 2027-03-04T08:00:00-06:00 to 18:00:00-06:00 | 7 |
duration | 2027-03-04T11:00:00-06:00 to 15:00:00-06:00 | 3 |
duration | 2027-03-05T12:15:00-06:00 to 2027-03-09T10:45:00-06:00 | 12.5 |
add | 4 h from 2027-03-04T11:00:00-06:00 | 2027-03-04T16:00:00-06:00 |
is_open | 2027-03-04T12:30:00-06:00 | false |
is_open | 2027-03-04T13:30:00-06:00 | true |
next_open | 2027-03-04T12:45:00-06:00 | 2027-03-04T13:30:00-06:00 |
Schedule C has exceptions 2027-03-10: [["09:00","12:00"]] and 2027-03-11: [] on top of schedule A. duration from 2027-03-10T09:00:00-06:00 to 2027-03-12T17:00:00-06:00 is 11. add 8 h from 2027-03-10T09:00:00-06:00 gives 2027-03-12T14:00:00-06:00.
Schedule D is open all day, every day: ["00:00","24:00"]. duration from 2027-03-14T00:00:00-06:00 to 2027-03-15T00:00:00-05:00 is 23, because the local day is an hour short. From 2027-11-07T00:00:00-05:00 to 2027-11-08T00:00:00-06:00 it is 25. add 24 h from 2027-03-13T12:00:00-06:00 gives 2027-03-14T13:00:00-05:00.
Run the same cases through the spreadsheet method in counting working hours to get a second opinion on the duration rows. For contract clocks built on these operations, see SLA deadline calculators. Where the sheet and the service must agree, spreadsheet and API tools sets out the parity checks.
Sources
- [IETF RFC 3339, Date and Time on the InternetTimestamps](https://www.rfc-editor.org/rfc/rfc3339)
- [IETF RFC 9557, Date and Time on the InternetTimestamps with Additional Information](https://www.rfc-editor.org/rfc/rfc9557)







