Skip to content

Time

TimeRange defines the period and output times of a run.

time_units module-attribute

time_units = {
    "h": "hours",
    "d": "days",
    "w": "weeks",
    "m": "minutes",
    "s": "seconds",
    "y": "years",
    "H": "hours",
    "D": "days",
    "W": "weeks",
    "M": "minutes",
    "S": "seconds",
    "Y": "years",
}

TimeRange pydantic-model

Bases: BaseModel

A time range object

Examples

from rompy import TimeRange tr = TimeRange(start="2020-01-01", end="2020-01-02") tr TimeRange(start=datetime.datetime(2020, 1, 1, 0, 0), end=datetime.datetime(2020, 1, 2, 0, 0), duration=None, interval=None, include_end=True) tr = TimeRange(start="2020-01-01", duration="1d") tr TimeRange(start=datetime.datetime(2020, 1, 1, 0, 0), end=datetime.datetime(2020, 1, 2, 0, 0), duration=timedelta(days=1), interval=None, include_end=True) tr = TimeRange(start="2020-01-01", duration="1d", interval="1h") tr TimeRange(start=datetime.datetime(2020, 1, 1, 0, 0), end=None, duration=timedelta(days=1), interval=timedelta(hours=1), include_end=True)

Config:

  • validate_default: True

Fields:

Validators:

start pydantic-field

start: Optional[datetime] = None

The start date of the time range

end pydantic-field

end: Optional[datetime] = None

The end date of the time range

duration pydantic-field

duration: Optional[Union[str, timedelta]] = None

The duration of the time range

interval pydantic-field

interval: Optional[Union[str, timedelta]] = '1h'

The frequency of the time range

include_end pydantic-field

include_end: bool = True

Determines if the end date should be included in the range

date_range property

date_range: list[datetime]

valid_duration_interval pydantic-validator

valid_duration_interval(v)

validate_start_end pydantic-validator

validate_start_end(v)

validate_start_end_duration pydantic-validator

validate_start_end_duration(data: Any) -> Any

parse_start_end_duration pydantic-validator

parse_start_end_duration() -> TimeRange

serialize_model

serialize_model() -> dict

contains

contains(date: datetime) -> bool

contains_range

contains_range(date_range: TimeRange) -> bool

common_times

common_times(date_range: TimeRange) -> list[datetime]

format_duration

format_duration(duration: timedelta) -> str

Format a timedelta object as a human-readable string.

This method formats a timedelta in a way that's suitable for display in logs and other output.

Parameters:

Name Type Description Default
duration timedelta

The timedelta object to format

required

Returns:

Type Description
str

A formatted string representation of the duration