rhannequin / astronoby

Ruby library based on astronomy and astrometry books
https://dev.to/rhannequin/series/17782
MIT License
59 stars 2 forks source link

Add GreenwichSiderealTime and LocalSiderealTime #36

Closed rhannequin closed 8 months ago

rhannequin commented 8 months ago

Time conversion between local civil time (LCT), coordinated universal time (UTC), Greenwich sidereal time (GST) and local sidereal time (LST) were confusing. They were made out of utility functions with a blurry public API due to a mix of exposure and refactoring.

This introduces GreenwichSiderealTime and LocalSiderealTime, objects that can hold information about time related to the sidereal time.

With a clear public API, it makes it easier to convert times and understand what is manipulated.

utc_time = Time.utc(2024, 3, 14, 1, 2, 3)
longitude = Astronoby::Angle.as_degrees(2.3522)

lst = Astronoby::GreenwichSiderealTime
  .from_utc(utc_time)
  .to_lst(longitude: longitude)

lst.time.to_f
# => 12.667470435200839

utc = Astronoby::LocalSiderealTime.new(
  date: utc_time.to_date,
  time: lst.time,
  longitude: longitude
).to_gst.to_utc

utc.round
# => 2024-03-14 01:02:03 UTC