sentinelhub.time_utils

Module with useful time/date functions

sentinelhub.time_utils.is_valid_time(time)[source]

Check if input string represents a valid time/date stamp

Parameters:

time (str) – A string containing a time/date.

Returns:

True is string is a valid time/date, False otherwise.

Return type:

bool

sentinelhub.time_utils.parse_time(time_input: None | str | date, *, force_datetime: Literal[False] = False, allow_undefined: Literal[False] = False, **kwargs: Any) date[source]
sentinelhub.time_utils.parse_time(time_input: None | str | date, *, force_datetime: Literal[True], allow_undefined: Literal[False] = False, **kwargs: Any) datetime
sentinelhub.time_utils.parse_time(time_input: None | str | date, *, force_datetime: Literal[False] = False, allow_undefined: bool = False, **kwargs: Any) date | None
sentinelhub.time_utils.parse_time(time_input: None | str | date, *, force_datetime: Literal[True], allow_undefined: bool = False, **kwargs: Any) datetime | None

Parse input time/date string

Parameters:
  • time_input – An input representation of a time.

  • force_datetime – If True it will always return datetime.datetime object, if False it can also return only datetime.date object if only date is provided as input.

  • allow_undefined – Flag to allow parsing None or ‘..’ into None.

  • kwargs – Keyword arguments to be passed to dateutil.parser.parse. Example: ignoretz=True.

Returns:

A parsed datetime representing the time.

sentinelhub.time_utils.parse_time_interval(time, allow_undefined=False, **kwargs)[source]

Parse input into an interval of two times, specifying start and end time, into datetime objects.

The input time can have the following formats, which will be parsed as:

  • YYYY-MM-DD -> [YYYY-MM-DD:T00:00:00, YYYY-MM-DD:T23:59:59]

  • YYYY-MM-DDThh:mm:ss -> [YYYY-MM-DDThh:mm:ss, YYYY-MM-DDThh:mm:ss]

  • list or tuple of two dates in form YYYY-MM-DD -> [YYYY-MM-DDT00:00:00, YYYY-MM-DDT23:59:59]

  • list or tuple of two dates in form YYYY-MM-DDThh:mm:ss -> [YYYY-MM-DDThh:mm:ss, YYYY-MM-DDThh:mm:ss]

All input times can also be specified as datetime objects. Instances of datetime.date will be treated as YYYY-MM-DD and instance of datetime.datetime will be treated as YYYY-MM-DDThh:mm:ss.

Parameters:
  • time (None | str | date | Tuple[None | str | date, None | str | date]) – An input representation of a time interval.

  • allow_undefined (bool) – Boolean flag controls if None or ‘..’ are allowed.

  • kwargs (Any) – Keyword arguments to be passed to parse_time function.

Returns:

A pair of datetime objects defining the time interval.

Raises:

ValueError

Return type:

tuple[datetime.datetime | None, datetime.datetime | None]

sentinelhub.time_utils.serialize_time(timestamp_input: date | None, *, use_tz: bool = False) str[source]
sentinelhub.time_utils.serialize_time(timestamp_input: Iterable[date | None], *, use_tz: bool = False) tuple[str, ...]

Transforms datetime objects into ISO 8601 strings.

Parameters:
  • timestamp_input – A datetime object or a tuple of datetime objects.

  • use_tz – If True it will ensure that the serialized string contains a timezone information (typically with Z at the end instead of +00:00). If False it will make sure to remove any timezone information.

Returns:

Timestamp(s) serialized into string(s).

sentinelhub.time_utils.date_to_datetime(date, time=None)[source]

Converts a date object into datetime object.

Parameters:
  • date (date) – A date object.

  • time (time | None) – An option time object, if not provided it will replace it with 00:00:00.

Returns:

A datetime object derived from date and time.

Return type:

datetime

sentinelhub.time_utils.filter_times(timestamps, time_difference)[source]

Filters out timestamps within time_difference, preserving only the oldest timestamp.

Parameters:
  • timestamps (Iterable[TimeType]) – A list of timestamps.

  • time_difference (timedelta) – A time difference threshold.

Returns:

An ordered list of timestamps d_1 <= d_2 <= … <= d_n such that d_(i+1)-d_i > time_difference.

Return type:

list[~TimeType]