Time (pyobs.utils.time)

TODO: write doc

Time is a thin subclass of astropy.time.Time used throughout pyobs instead of the astropy original. It adds two things: hashability (so Time objects can be used in sets and as dict keys) and a testable clock via now().

Always use pyobs.utils.time.Time rather than astropy.time.Time in pyobs code — it is a drop-in replacement and accepts all the same constructor arguments:

from pyobs.utils.time import Time

now = Time.now()
t = Time("2024-06-01T22:00:00")
t = Time(some_datetime_object, format="datetime", scale="utc")

Simulated time

In simulation or testing scenarios, set_offset_to_now() shifts what Time.now() returns without affecting the system clock:

from astropy.time import TimeDelta
import astropy.units as u

# make Time.now() return a time 2 hours in the past
Time.set_offset_to_now(TimeDelta(-2 * u.hour))

This is used by SimWorld to run the simulator at an arbitrary point in time.

API reference

class Time(val, val2=None, format=None, scale=None, precision=None, in_subfmt=None, out_subfmt=None, location=None, copy=False)[source]

Bases: Time

Hashable Time class.

night_obs(observer: Observer) date[source]

Returns the night for this time, i.e. the date of the start of the current night.

Parameters:

observer – Observer object to use.

Returns:

Night for this time.

classmethod now() Time[source]

Creates a new object corresponding to the instant in time this method is called.

Note

“Now” is determined using the ~datetime.datetime.utcnow function, so its accuracy and precision is determined by that function. Generally that means it is set by the accuracy of your system clock.

Returns:

A new Time object (or a subclass of Time if this is called from such a subclass) at the current time.