Skip to content

Period

The Period class provides named time periods and weekday constants.

carbonic.core.period.Period dataclass

Period(
    name: str,
    _type: Literal["minute", "hour", "day", "week", "month", "quarter", "year"],
)

Represents a named time period for semantic datetime operations.

Provides named constants like Period.DAY, Period.MONTH, etc. for more readable and semantic datetime arithmetic operations.

Examples:

Period.MONTH.add_to(date) # Add 1 month to date Period.WEEK.subtract_from(date) # Subtract 1 week from date Period.QUARTER.start_of(date) # Start of current quarter Period.YEAR.end_of(date) # End of current year Period.DAY.add_to(date, count=5) # Add 5 days to date

Functions

add_to

add_to(dt: Date | DateTime, *, count: int = 1) -> Date | DateTime

Add this period to a Date or DateTime.

Parameters:

Name Type Description Default
dt Date | DateTime

The Date or DateTime to add to

required
count int

Number of periods to add (default: 1)

1

Returns:

Type Description
Date | DateTime

New Date or DateTime with the period added

subtract_from

subtract_from(dt: Date | DateTime, *, count: int = 1) -> Date | DateTime

Subtract this period from a Date or DateTime.

Parameters:

Name Type Description Default
dt Date | DateTime

The Date or DateTime to subtract from

required
count int

Number of periods to subtract (default: 1)

1

Returns:

Type Description
Date | DateTime

New Date or DateTime with the period subtracted

start_of

start_of(dt: Date | DateTime) -> Date | DateTime

Get the start of this period for the given date/datetime.

Parameters:

Name Type Description Default
dt Date | DateTime

The Date or DateTime to get the period start for

required

Returns:

Type Description
Date | DateTime

New Date or DateTime at the start of the period

end_of

end_of(dt: Date | DateTime) -> Date | DateTime

Get the end of this period for the given date/datetime.

Parameters:

Name Type Description Default
dt Date | DateTime

The Date or DateTime to get the period end for

required

Returns:

Type Description
Date | DateTime

New Date or DateTime at the end of the period