time_conversion#

Methods for converting between Python datetimes, timestamps and ISO 8061 strings.

hsr4hci.time_conversion.date_string_to_datetime(date_string, tzinfo=timezone.utc)[source]#

Convert a datetime from string (usually ISO 8061 for FITS files) to a Python datetime object.

Parameters:
  • date_string (Union[str, bytes_]) โ€“ A datetime string (e.g., โ€œ2007-04-05T14:30โ€).

  • tzinfo (timezone) โ€“ A timezone object. Usually, everything should be in UTC.

Returns:

A datetime object that matches the given date_string.

Return type:

datetime

hsr4hci.time_conversion.date_string_to_timestamp(date_string, tzinfo=timezone.utc)[source]#

Convert a date_string (usually ISO 8061 for FITS files) to a UNIX timestamp (i.e., seconds since January 1, 1970).

Parameters:
  • date_string (Union[str, bytes_]) โ€“ A datetime string (e.g., โ€œ2007-04-05T14:30โ€).

  • tzinfo (timezone) โ€“ A timezone object. Usually, everything should be in UTC.

Returns:

A timestamp (as a float) that matches the given date_string.

Return type:

float

hsr4hci.time_conversion.round_minutes(dt, direction, resolution=5)[source]#

Auxiliary function to round the minutes of a given datetime dt to the desired resolution (e.g., closest 5 minutes). Seconds and milliseconds are discarded.

Parameters:
  • dt (datetime) โ€“ A datetime object.

  • direction (str) โ€“ Either โ€œupโ€ or โ€œdownโ€ (direction of rounding).

  • resolution (float) โ€“ Resolution (round to closest resolution minutes).

Returns:

The given datetime dt rounded to the target resolution.

Return type:

datetime

hsr4hci.time_conversion.timestamp_to_date_string(timestamp, tzinfo=timezone.utc, include_timezone=False)[source]#

Convert a given UNIX timestamp to a ISO 8061-formatted string.

Parameters:
  • timestamp (float) โ€“ A UNIX timestamp (as a float).

  • tzinfo (timezone) โ€“ A timezone object. Usually, everything should be in UTC.

  • include_timezone (bool) โ€“ Whether to include the time zone information in the string (e.g., โ€œ+00:00โ€).

Returns:

An ISO 8061-formatted string that matches the given timestamp.

Return type:

str

hsr4hci.time_conversion.timestamp_to_datetime(timestamp, tzinfo=timezone.utc)[source]#

Convert a given UNIX timestamp to a Python datetime object.

Parameters:
  • timestamp (float) โ€“ A UNIX timestamp (as a float).

  • tzinfo (timezone) โ€“ A timezone object. Usually, everything should be in UTC.

Returns:

A Python datetime object that matches the given timestamp.

Return type:

datetime