# Daydate Library

Date arithmetic and temporal logic for legal deadlines and time-based rules. Follows ISO 8601 conventions.
Can be imported into L4 files with `IMPORT daydate`. Automatically imports `date-compat` for backwards-compatible DATE accessors.

### Location

[jl4-core/libraries/daydate.l4](https://github.com/smucclaw/l4-ide/blob/main/jl4-core/libraries/daydate.l4)

### Features

- Date construction from day/month/year, serial numbers, strings, or DATETIME extraction
- Date arithmetic (add/subtract days, weeks, months, years)
- Weekday and weekend detection
- Week-of-year calculations
- Month and year helpers
- Leap year detection
- String parsing for JSON interoperability
- DATETIME date extraction

### Key Functions

**Constants:**

- `Monday` through `Sunday` (0-6)
- `January` through `December` (1-12)
- `Days in a week`, `Days in a year`, etc.

**Date Construction:**

- `Date day month year` - Create date from components
- `Date serialNumber` - Create date from serial number
- `Date str` - Parse date string (ISO format, ignores time/timezone if present), returns MAYBE DATE
- `Date dt` - Extract date from DATETIME (ignores time and timezone)
- `Week weekNumber year` - First day of week
- `Month month year` - First day of month
- `Year year` - First day of year

**Date Arithmetic:**

- `date PLUS days` - Add days to date
- `date MINUS days` - Subtract days from date
- `the day after`, `the week before`, etc.

**Queries:**

- `Weekday of date` - Get weekday (0-6)
- `Week of the year date` - Get ISO week number
- `is weekend`, `is weekday` - Check day type
- `is leap year` - Check leap year

**Comparators:**

- `the earlier of` d1 d2
- `the later of` d1 d2
- d1 `is before` d2, d1 `is after` d2
- Standard comparison operators: LESS THAN, GREATER THAN, AT MOST, AT LEAST

### Example: Date Calculations



```l4-file
-- Daydate Example: Date calculations and temporal logic
IMPORT daydate

-- Date construction from components
DECIDE `Christmas 2024` IS Date 25 12 2024
DECIDE `New Year 2025` IS Date 1 1 2025

#EVAL `Christmas 2024`
#EVAL `New Year 2025`

-- Date from ISO string (returns MAYBE DATE, ignores time/timezone if present)
DECIDE `from iso date` IS Date "2025-06-15"
DECIDE `from iso datetime` IS Date "2025-06-15T10:30:00Z"
#EVAL `from iso date`       -- JUST OF (DATE OF 15, 6, 2025)
#EVAL `from iso datetime`   -- JUST OF (DATE OF 15, 6, 2025)

-- Date arithmetic
DECIDE `week later` IS `Christmas 2024` PLUS 7
#EVAL `week later`  -- January 1, 2025

DECIDE `week before` IS `Christmas 2024` MINUS 7
#EVAL `week before`  -- December 18, 2024

-- Date components
#EVAL DATE_DAY `Christmas 2024`    -- 25
#EVAL DATE_MONTH `Christmas 2024`  -- 12
#EVAL DATE_YEAR `Christmas 2024`   -- 2024

-- Weekday calculations
DECIDE `day of week` IS `Weekday of` `Christmas 2024`
#EVAL `day of week`  -- Wednesday (3)

#EVAL `Name of weekday` `Christmas 2024`  -- "Wednesday"

-- Weekend checks
#EVAL `is weekend` `Christmas 2024`   -- FALSE
#EVAL `is weekday` `Christmas 2024`   -- TRUE

DECIDE `last Saturday in 2024` IS Date 28 12 2024
#EVAL `is weekend` `last Saturday in 2024` -- TRUE

-- Week of year
#EVAL `Week of the year` `Christmas 2024`  -- 52

-- Leap year
#EVAL `is leap year` 2024  -- TRUE
#EVAL `is leap year` 2023  -- FALSE

-- Relative time phrases
DECIDE `tomorrow` IS `the day after` `Christmas 2024`
#EVAL `tomorrow`

DECIDE `next week` IS `the week after` `Christmas 2024`
#EVAL `next week`


-- Date comparison (via operator overloads)
#EVAL `Christmas 2024` < `New Year 2025`   -- TRUE
#EVAL `Christmas 2024` >= `New Year 2025`  -- FALSE

-- Readable comparisons
DECIDE `christmas is before new year` IS `Christmas 2024` `is before` `New Year 2025`
DECIDE `new year is after christmas` IS `New Year 2025` `is after` `Christmas 2024`
#EVAL `christmas is before new year`  -- TRUE
#EVAL `new year is after christmas`   -- TRUE

-- Month helpers
DECIDE `July 4th` IS July 4 2024
#EVAL `July 4th`
#EVAL `Name of month` `July 4th`  -- "July"
```



**See [daydate.l4](https://github.com/smucclaw/l4-ide/blob/main/jl4-core/libraries/daydate.l4) source for all functions.**
