|
Download calculation/dates.md from swedishembedded/knowledge: direct link, hf CLI and curl.
- Browser
- Download file 4.37 kB
-
https://huggingface.co/swedishembedded/knowledge/resolve/main/calculation/dates.md
- Command line
-
hf download hf://swedishembedded/knowledge/calculation/dates.md
-
curl -L -o dates.md https://huggingface.co/swedishembedded/knowledge/resolve/main/calculation/dates.md
4.37 kB
| # SPDX-License-Identifier: CC-BY-4.0 | |
| # Copyright (c) 2026 Martin Schröder <info@swedishembedded.com> | |
| title: Dates - days, business days, months, years, rolling | |
| tiers: rule 1, procedure 2, example 3, mistakes 2 | |
| # Dates: days, business days, months, years, rolling | |
| For any day count or deadline. The task's own rule table and holiday list decide. | |
| <!-- tier: 1 --> | |
| ## Rule | |
| **The task's own files and stated rules always win over this page; use this page only for what | |
| the task leaves open.** | |
| 1. **Always compute dates in a script.** Mental calendar arithmetic and remembered weekdays are | |
| unreliable. | |
| 2. **Unless the task says otherwise, a period starts the day after its trigger.** "Within N days after X" ends on X + N; that | |
| last day is still in time. It is never X - N. | |
| 3. **Calendar days** count every day. **Business days** are what the task defines; usually | |
| Monday to Friday except the holidays in the task's list. | |
| 4. **N business days after X:** step forward a day at a time, count only business days, stop at | |
| the N-th. The trigger day is not counted. | |
| 5. **N months after X:** the same day number N months later, or the month's last day if it has no | |
| such day. **N years after X:** same month and day, N years later; not N x 365 days. | |
| 6. **Rolling:** a deadline falling on a non-business day is carried forward to the following business day, but only for | |
| the kinds of deadline the task says roll. A date that is a fact (a payment, a delivery) never | |
| rolls. | |
| 7. **Days between** two dates = later minus earlier. | |
| 8. **"Today"** is the date the task gives (case date, as-of date, period end), never the | |
| machine's clock. | |
| 9. **Write full dates with the year.** A period that passes December changes the year. | |
| <!-- tier: 2 --> | |
| ## Procedure | |
| 1. Read from the task: the trigger date, the length of the period, its unit (days, business | |
| days, months, years), and whether this kind of deadline rolls. | |
| 2. Load the task's holiday list into a set of dates. Check what its columns are before parsing. | |
| 3. Compute with the helper that fits the unit (see the example). | |
| 4. Roll only if the task says so for this kind of deadline. | |
| 5. Print the result with its weekday name. If it must be a business day, confirm that it is. | |
| 6. For each new question start again from that question's own dates. | |
| A notice period "counted from the first day of the following month": month 1 is the month after | |
| the notice date; the period ends on the last day of month N. | |
| <!-- tier: 3 --> | |
| ## Worked example | |
| Illustration only, with invented data. Your task's data differs: take no fact, count or | |
| expected result from this example. | |
| ```python | |
| import calendar | |
| from datetime import date, timedelta | |
| closed_days = {date(2031, 3, 10)} | |
| def is_business(d): | |
| return d not in closed_days and d.weekday() < 5 | |
| def add_business_days(d, n): | |
| while n > 0: | |
| d += timedelta(days=1) | |
| if is_business(d): | |
| n -= 1 | |
| return d | |
| def roll(d): | |
| while not is_business(d): | |
| d += timedelta(days=1) | |
| return d | |
| def add_months(d, n): | |
| m = d.month - 1 + n | |
| y, m = d.year + m // 12, m % 12 + 1 | |
| return date(y, m, min(d.day, calendar.monthrange(y, m)[1])) | |
| def end_of_month(y, m): | |
| return date(y, m, calendar.monthrange(y, m)[1]) | |
| ``` | |
| - **5 business days after Thursday 2031-03-06:** Fri 7 (1), weekend skipped, Mon 10 skipped | |
| (holiday), Tue 11 (2), Wed 12 (3), Thu 13 (4), Fri 14 (5). Answer 2031-03-14. | |
| - **10 days after Wednesday 2031-10-08, rolling:** 2031-10-18, a Saturday; rolled to Monday | |
| 2031-10-20. | |
| - **1 month after 2031-01-31:** 2031-02-28. | |
| - **3 years after 2031-04-12:** 2034-04-12. | |
| - **Notice of 3 months given 2031-11-20, counted from the next month:** month 1 December 2031, | |
| month 2 January 2032, month 3 February 2032; ends 2032-02-29 (a leap year, and a Sunday). If | |
| the task says the end of a notice period does not roll, it stays. | |
| - **Days late:** due 2031-06-02, paid 2031-06-16: 14 days. | |
| <!-- tier: 2 --> | |
| ## Common mistakes | |
| - Counting the trigger day as day 1. | |
| - Subtracting the period from the trigger instead of adding it. | |
| - Treating "N days" as business days, or "N business days" as calendar days. | |
| - Rolling a kind of deadline the task exempts, or rolling a date that is a fact. | |
| - Not rolling a due date that the task does roll, and then counting days late from the wrong day. | |
| - Adding 365 x N days for N years. | |