Module: Astronoby::Util::Time

Defined in:
lib/astronoby/util/time.rb

Class Method Summary collapse

Class Method Details

.decimal_hour_to_time(date, utc_offset, decimal) ⇒ ::Time

Returns Date and time.

Parameters:

  • date (Date)
  • decimal (Numeric)

    Hour of the day, in decimal hours

Returns:

  • (::Time)

    Date and time



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/astronoby/util/time.rb', line 11

def self.decimal_hour_to_time(date, utc_offset, decimal)
  absolute_hour = decimal.abs
  hour = absolute_hour.floor

  unless hour.between?(0, Constants::HOURS_PER_DAY)
    raise(
      IncompatibleArgumentsError,
      "Hour must be between 0 and #{Constants::HOURS_PER_DAY.to_i}, got #{hour}"
    )
  end

  decimal_minute = Constants::MINUTES_PER_HOUR * (absolute_hour - hour)
  absolute_decimal_minute = (
    Constants::MINUTES_PER_HOUR * (absolute_hour - hour)
  ).abs
  minute = decimal_minute.floor
  second = Constants::SECONDS_PER_MINUTE *
    (absolute_decimal_minute - absolute_decimal_minute.floor)

  date_in_local_time = ::Time
    .utc(date.year, date.month, date.day, hour, minute, second)
    .getlocal(utc_offset)
    .to_date

  if date_in_local_time < date
    date = date.next_day
  elsif date_in_local_time > date
    date = date.prev_day
  end

  ::Time.utc(date.year, date.month, date.day, hour, minute, second).round
end

.terrestrial_universal_time_delta(instant) ⇒ Numeric

Returns Delta T (TT - UT1) in seconds for the given instant.

Parameters:

  • instant (Numeric, Time, Date, DateTime)

Returns:

  • (Numeric)

    Delta T (TT - UT1) in seconds for the given instant



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/astronoby/util/time.rb', line 46

def self.terrestrial_universal_time_delta(instant)
  case instant
  when Numeric
    IERS::DeltaT.at(jd: instant).delta_t
  when ::Time, ::Date, ::DateTime
    IERS::DeltaT.at(instant).delta_t
  else
    raise IncompatibleArgumentsError,
      "Expected a Numeric, Time, Date or DateTime object, got #{instant.class}"
  end
rescue IERS::OutOfRangeError => e
  if e.available_range
    IERS::DeltaT.at(mjd: e.available_range.end).delta_t
  else
    0
  end
end