|
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
metadata
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.
Rule
The task's own files and stated rules always win over this page; use this page only for what the task leaves open.
- Always compute dates in a script. Mental calendar arithmetic and remembered weekdays are unreliable.
- 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.
- Calendar days count every day. Business days are what the task defines; usually Monday to Friday except the holidays in the task's list.
- 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.
- 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.
- 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.
- Days between two dates = later minus earlier.
- "Today" is the date the task gives (case date, as-of date, period end), never the machine's clock.
- Write full dates with the year. A period that passes December changes the year.
Procedure
- 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.
- Load the task's holiday list into a set of dates. Check what its columns are before parsing.
- Compute with the helper that fits the unit (see the example).
- Roll only if the task says so for this kind of deadline.
- Print the result with its weekday name. If it must be a business day, confirm that it is.
- 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.
Worked example
Illustration only, with invented data. Your task's data differs: take no fact, count or expected result from this example.
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.
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.