time_utils

Module with useful time/date functions

sentinelhub.time_utils.get_dates_in_range(start_date, end_date)[source]

Get all dates within input start and end date in ISO 8601 format

Parameters:
  • start_date (str) – start date in ISO 8601 format
  • end_date (str) – end date in ISO 8601 format
Returns:

list of dates between start_date and end_date in ISO 8601 format

Return type:

list of str

sentinelhub.time_utils.next_date(date)[source]

Get date of day after input date in ISO 8601 format

For instance, if input date is '2017-03-12', the function returns '2017-03-13'

Parameters:date (str) – input date in ISO 8601 format
Returns:date after input date in ISO 8601 format
Return type:str
sentinelhub.time_utils.prev_date(date)[source]

Get date of day previous to input date in ISO 8601 format

For instance, if input date is '2017-03-12', the function returns '2017-03-11'

Parameters:date (str) – input date in ISO 8601 format
Returns:date previous to input date in ISO 8601 format
Return type:str
sentinelhub.time_utils.iso_to_datetime(date)[source]

Convert ISO 8601 time format to datetime format

This function converts a date in ISO format, e.g. 2017-09-14 to a datetime instance, e.g. datetime.datetime(2017,9,14,0,0)

Parameters:date (str) – date in ISO 8601 format
Returns:datetime instance
Return type:datetime
sentinelhub.time_utils.datetime_to_iso(date, only_date=True)[source]

Convert datetime format to ISO 8601 time format

This function converts a date in datetime instance, e.g. datetime.datetime(2017,9,14,0,0) to ISO format, e.g. 2017-09-14

Parameters:
  • date (datetime) – datetime instance to convert
  • only_date (bool) – whether to return date only or also time information. Default is True
Returns:

date in ISO 8601 format

Return type:

str

sentinelhub.time_utils.get_current_date()[source]

Get current date in ISO 8601 format

Returns:current date in ISO 8601 format
Return type:str
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 stamp
Returns:True is string is a valid time/date stamp, False otherwise
Return type:bool
sentinelhub.time_utils.parse_time(time_input, *, force_datetime=False, allow_undefined=False, **kwargs)[source]

Parse input time/date string

Parameters:
  • time_input (str or datetime.date or datetime.datetime) – time/date to parse
  • force_datetime (bool) – 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
  • allow_undefined – bool (default is False)
  • kwargs – Any keyword arguments to be passed to dateutil.parser.parse. Example: ignoretz=True
Returns:

A datetime object

Return type:

datetime.datetime or datetime.date

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:
Returns:

interval of start and end date of the form YYYY-MM-DDThh:mm:ss

Return type:

(datetime.datetime, datetime.datetime)

Raises:

ValueError

sentinelhub.time_utils.serialize_time(timestamp_input, *, use_tz=False)[source]

Transforms datetime objects into ISO 8601 strings

Parameters:
  • timestamp_input (datetime.date or datetime.datetime or tuple(datetime.date or datetime.datetime)) – A datetime object or a tuple of datetime objects
  • use_tz (bool) – 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)

Return type:

str or tuple(str)

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

Converts a date object into datetime object

Parameters:
  • date (datetime.date) – a date object
  • time (datetime.time) – 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.datetime

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

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

Parameters:
Returns:

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

Return type:

list(datetime.datetime)