API reference#
earthcarekit.utils.time
Datetime utilities.
Notes#
This module depends on other internal modules:
format_time_range_text
#
format_time_range_text(
st: TimestampLike, et: TimestampLike, show_date: bool = True, show_time: bool = True
) -> str
Returns a formatted text label (str) describing the time range given by the start time st and end time et.
Source code in earthcarekit/utils/time/_time.py
get_day_night_mask
#
get_day_night_mask(
time: DatetimeIndex | ArrayLike,
lats: ArrayLike,
lons: ArrayLike,
sun_altitude_threshold: float = 0.0,
) -> NDArray[bool_]
Calculates a day/night mask from UTC timestamps and lat/lon positions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
times
|
UTC Timestamps. |
required | |
lats
|
ArrayLike
|
Latitude(s) in degrees (scalar or same length as |
required |
lons
|
ArrayLike
|
Longitude(s) in degrees, positive east (scalar or same length as |
required |
sun_altitude_threshold
|
float
|
Threshold in degrees for "day" (0 = horizon, -6 = civil twilight, -12 = nautical twilight, -18 = astronomical twilight) |
0.0
|
Returns:
| Type | Description |
|---|---|
NDArray[bool_]
|
Day/night mask (True = day, False = night) |
References
- NOAA Global Monitoring Laboratory. Low Accuracy Equations. NOAA. Retrieved January 21, 2025 from https://gml.noaa.gov/grad/solcalc/sollinks.html
- Wikipedia contributors (2025). Hour angle. Wikipedia. Retrieved January 21, 2025 from https://en.wikipedia.org/wiki/Hour_angle
Source code in earthcarekit/utils/time/_day_night.py
get_time_range
#
get_time_range(
start_time: TimestampLike,
end_time: TimestampLike,
freq: str | None = None,
periods: int | None = None,
) -> DatetimeIndex
Generates timestamps based on frequency or number of periods.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
start_time
|
TimestampLike
|
Start timestamp. |
required |
end_time
|
TimestampLike
|
End timestamp. |
required |
freq
|
str | None
|
Time frequency string (e.g., "1H", "30min"); rounds to nearest. |
None
|
periods
|
int | None
|
Number of evenly spaced timestamps. |
None
|
Returns:
| Type | Description |
|---|---|
DatetimeIndex
|
A |
Source code in earthcarekit/utils/time/_time.py
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 | |
lookup_value_by_timestamp
#
Returns the value corresponding to the closest timestamp in a monotonic series.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
t
|
TimestampLike
|
Timestamp to look up. |
required |
times
|
NDArray
|
Monotonically increasing timestamps. |
required |
values
|
NDArray[Any]
|
Values corresponding to |
required |
Returns:
| Type | Description |
|---|---|
Any
|
Value from |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in earthcarekit/utils/time/_time.py
num_to_time
#
Converts numerical time values back to datetime values relative to a given epoch.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num
|
NDArray | Iterable
|
Array or iterable of numeric values representing time offsets. |
required |
epoch
|
TimestampLike
|
Reference time to which the numeric values are relative. |
required |
format
|
TimeUnit
|
Time unit of the numeric values (e.g., "s", "ms", "us", "ns"); defaults to "ns". |
'ns'
|
Returns:
| Type | Description |
|---|---|
NDArray
|
An array of datetime64 values corresponding to the numeric offsets from the epoch. |
Source code in earthcarekit/utils/time/_time.py
time_to_iso
#
time_to_iso(
time: TimestampLike, format: Literal["datetime", "date", "time"] | str = "datetime"
) -> str
Converts timestamp to an ISO-formatted string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
time
|
TimestampLike
|
Input timestamp. |
required |
format
|
Literal['datetime', 'date', 'time'] | str
|
The type of ISO string to return
|
'datetime'
|
Returns:
| Type | Description |
|---|---|
str
|
An ISO-formatted time string. |
Source code in earthcarekit/utils/time/_time.py
time_to_num
#
time_to_num(
time: NDArray | Iterable[TimestampLike], epoch: TimestampLike, format: TimeUnit = "ns"
) -> NDArray
Converts datetime-like values to numerical values relative to a given epoch.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
time
|
NDArray | Iterable[TimestampLike]
|
Array or iterable of datetime-like values. |
required |
epoch
|
TimestampLike
|
Reference time from which the numerical values are computed. |
required |
format
|
TimeUnit
|
Time unit for conversion (e.g., "s", "ms", "us", "ns"); defaults to "ns". |
'ns'
|
Returns:
| Type | Description |
|---|---|
NDArray
|
An array of floats representing the time difference from the epoch in the given unit. |
Source code in earthcarekit/utils/time/_time.py
time_to_str
#
Converts timestamp to a formatted string (defaults to YYYY-MM-DDTHH:MM:SS).
Source code in earthcarekit/utils/time/_time.py
times_to_iso
#
times_to_iso(
times: Iterable[TimestampLike], format: Literal["datetime", "date", "time"] | str = "datetime"
) -> list[str]
Converts timestamps to a list of ISO-formatted strings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
times
|
Iterable[TimestampLike]
|
Input timestamps. |
required |
format
|
Literal['datetime', 'date', 'time'] | str
|
The type of ISO strings to return
|
'datetime'
|
Returns:
| Type | Description |
|---|---|
list[str]
|
A list of ISO-formatted time strings. |
Source code in earthcarekit/utils/time/_time.py
times_to_str
#
Converts timestamps to a list of formatted string (defaults to YYYY-MM-DDTHH:MM:SS).
Source code in earthcarekit/utils/time/_time.py
to_timedelta
#
Converts input to pandas.Timedelta.
Source code in earthcarekit/utils/time/_time.py
to_timedeltas
#
Converts inputs to pandas.Timedeltas and returns them as pandas.TimedeltaIndex.
Source code in earthcarekit/utils/time/_time.py
to_timestamp
#
Converts input to pandas.Timestamp.
Source code in earthcarekit/utils/time/_time.py
to_timestamps
#
Converts inputs to pandas.Timestamps and returns them as pandas.DatetimeIndex.
Source code in earthcarekit/utils/time/_time.py
validate_time_range
#
Checks if given time_range is valid, if True returns is as a tuple
of two pandas.Timestamps, if False raises ValueError.