Date¶
The Date class provides date-only operations without time components.
carbonic.core.date.Date
dataclass
¶
Immutable date object with fluent API and comprehensive date operations.
The Date class provides a modern, type-safe wrapper around Python's date with additional functionality for date manipulation and formatting. All operations return new instances, maintaining immutability.
Attributes:
| Name | Type | Description |
|---|---|---|
_date |
date
|
Internal datetime.date object storing the actual date value |
Examples:
Initialize a new Date instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
year
|
int
|
Year (e.g., 2024) |
required |
month
|
int
|
Month (1-12) |
required |
day
|
int
|
Day of month (1-31) |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If any date component is invalid |
Attributes¶
Functions¶
tomorrow
classmethod
¶
yesterday
classmethod
¶
next
classmethod
¶
Get a date in the future relative to today.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
unit
|
str
|
Time unit ("day", "week", "month", "quarter", "year") |
required |
count
|
int
|
Number of units to add (default: 1) |
1
|
Returns:
| Type | Description |
|---|---|
Date
|
Date object in the future |
Examples:
previous
classmethod
¶
Get a date in the past relative to today.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
unit
|
str
|
Time unit ("day", "week", "month", "quarter", "year") |
required |
count
|
int
|
Number of units to subtract (default: 1) |
1
|
Returns:
| Type | Description |
|---|---|
Date
|
Date object in the past |
Examples:
parse
classmethod
¶
Parse a date string into a Date object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s
|
str
|
The date string to parse. Supports: - ISO date formats (2024-01-15) - Custom formats when fmt is provided |
required |
fmt
|
str | None
|
Optional format string. If None, auto-detect format. Supports both strftime (%Y-%m-%d) and Carbon (Y-m-d) formats. |
None
|
Returns:
| Type | Description |
|---|---|
Date
|
Date object |
Raises:
| Type | Description |
|---|---|
ParseError
|
If the string cannot be parsed |
Examples:
weekday ¶
Return day of week where Monday=0, Sunday=6 (datetime.date compatibility).
isoweekday ¶
ISO weekday where Monday=1, Sunday=7 (datetime.date compatibility).
isoformat ¶
Return ISO 8601 format string (YYYY-MM-DD) for datetime.date compatibility.
isocalendar ¶
Return (year, week, weekday) tuple for datetime.date compatibility.
replace ¶
Return a Date with one or more components replaced (datetime.date compatibility).
add ¶
Add years, months, and/or days to this date.
subtract ¶
Subtract years, months, and/or days from this date.
diff ¶
add_duration ¶
subtract_duration ¶
start_of ¶
Return the start of the specified time period.
end_of ¶
Return the end of the specified time period.
add_business_days ¶
Add business days to this date, skipping weekends.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
days
|
int
|
Number of business days to add (can be negative) |
required |
Returns:
| Type | Description |
|---|---|
Date
|
New Date with business days added |
Examples:
Date(2023, 12, 25).add_business_days(1) # Monday -> Tuesday Date(2023, 12, 29).add_business_days(1) # Friday -> Monday (skip weekend)
subtract_business_days ¶
Subtract business days from this date, skipping weekends.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
days
|
int
|
Number of business days to subtract (can be negative) |
required |
Returns:
| Type | Description |
|---|---|
Date
|
New Date with business days subtracted |
Examples:
Date(2023, 12, 26).subtract_business_days(1) # Tuesday -> Monday Date(2024, 1, 1).subtract_business_days(1) # Monday -> Friday (skip weekend)
to_datetime ¶
Convert to datetime.datetime with timezone (default UTC).
format ¶
Format date using Carbon-style format string.
Uses Carbon-style tokens for flexible date formatting. Escape characters with backslash () to include them literally.
Format Tokens¶
| Token | Description | Example |
|---|---|---|
Y |
4-digit year | 2024 |
y |
2-digit year | 24 |
m |
Month with leading zero | 01, 12 |
n |
Month without leading zero | 1, 12 |
d |
Day with leading zero | 01, 31 |
j |
Day without leading zero | 1, 31 |
S |
Ordinal suffix | st, nd, rd, th |
F |
Full month name (localized) | January, enero |
M |
Short month name (localized) | Jan, ene |
l |
Full day name (localized) | Monday, lunes |
D |
Short day name (localized) | Mon, lun |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fmt
|
str
|
Carbon-style format string with tokens above |
required |
locale
|
str | None
|
Locale code for localized month/day names (default: English) Supported: en, pl, es, fr, de, pt |
None
|
Returns:
| Type | Description |
|---|---|
str
|
Formatted date string |
Examples:
to_datetime_string ¶
Return date with default time (YYYY-MM-DD 00:00:00).