|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module Astronoby |
| 4 | + class Duration |
| 5 | + include Comparable |
| 6 | + |
| 7 | + class << self |
| 8 | + # @return [Astronoby::Duration] a zero duration |
| 9 | + def zero |
| 10 | + new(0) |
| 11 | + end |
| 12 | + |
| 13 | + # @param seconds [Numeric] the duration in seconds |
| 14 | + # @return [Astronoby::Duration] a new Duration |
| 15 | + def from_seconds(seconds) |
| 16 | + new(seconds) |
| 17 | + end |
| 18 | + |
| 19 | + # @param minutes [Numeric] the duration in minutes |
| 20 | + # @return [Astronoby::Duration] a new Duration |
| 21 | + def from_minutes(minutes) |
| 22 | + seconds = minutes * Constants::SECONDS_PER_MINUTE |
| 23 | + from_seconds(seconds) |
| 24 | + end |
| 25 | + |
| 26 | + # @param hours [Numeric] the duration in hours |
| 27 | + # @return [Astronoby::Duration] a new Duration |
| 28 | + def from_hours(hours) |
| 29 | + seconds = hours * Constants::SECONDS_PER_HOUR |
| 30 | + from_seconds(seconds) |
| 31 | + end |
| 32 | + |
| 33 | + # @param days [Numeric] the duration in days |
| 34 | + # @return [Astronoby::Duration] a new Duration |
| 35 | + def from_days(days) |
| 36 | + seconds = days * Constants::SECONDS_PER_DAY |
| 37 | + from_seconds(seconds) |
| 38 | + end |
| 39 | + end |
| 40 | + |
| 41 | + # @return [Numeric] the duration in seconds |
| 42 | + attr_reader :seconds |
| 43 | + |
| 44 | + # @param seconds [Numeric] the duration in seconds |
| 45 | + def initialize(seconds) |
| 46 | + @seconds = seconds |
| 47 | + freeze |
| 48 | + end |
| 49 | + |
| 50 | + # @return [Float] the duration in minutes |
| 51 | + def minutes |
| 52 | + @seconds / Constants::SECONDS_PER_MINUTE |
| 53 | + end |
| 54 | + |
| 55 | + # @return [Float] the duration in hours |
| 56 | + def hours |
| 57 | + @seconds / Constants::SECONDS_PER_HOUR |
| 58 | + end |
| 59 | + |
| 60 | + # @return [Float] the duration in days |
| 61 | + def days |
| 62 | + @seconds / Constants::SECONDS_PER_DAY |
| 63 | + end |
| 64 | + |
| 65 | + # @param other [Astronoby::Duration] duration to add |
| 66 | + # @return [Astronoby::Duration] the sum |
| 67 | + def +(other) |
| 68 | + self.class.from_seconds(@seconds + other.seconds) |
| 69 | + end |
| 70 | + |
| 71 | + # @param other [Astronoby::Duration] duration to subtract |
| 72 | + # @return [Astronoby::Duration] the difference |
| 73 | + def -(other) |
| 74 | + self.class.from_seconds(@seconds - other.seconds) |
| 75 | + end |
| 76 | + |
| 77 | + # @return [Astronoby::Duration] the negated duration |
| 78 | + def -@ |
| 79 | + self.class.from_seconds(-@seconds) |
| 80 | + end |
| 81 | + |
| 82 | + # @return [Astronoby::Duration] the absolute duration |
| 83 | + def abs |
| 84 | + self.class.from_seconds(@seconds.abs) |
| 85 | + end |
| 86 | + |
| 87 | + # @return [Boolean] true if the duration is positive |
| 88 | + def positive? |
| 89 | + @seconds > 0 |
| 90 | + end |
| 91 | + |
| 92 | + # @return [Boolean] true if the duration is negative |
| 93 | + def negative? |
| 94 | + @seconds < 0 |
| 95 | + end |
| 96 | + |
| 97 | + # @return [Boolean] true if the duration is zero |
| 98 | + def zero? |
| 99 | + @seconds.zero? |
| 100 | + end |
| 101 | + |
| 102 | + # @return [Integer] hash value |
| 103 | + def hash |
| 104 | + [@seconds, self.class].hash |
| 105 | + end |
| 106 | + |
| 107 | + # @param other [Astronoby::Duration] duration to compare with |
| 108 | + # @return [Integer, nil] -1, 0, or 1; nil if not comparable |
| 109 | + def <=>(other) |
| 110 | + return unless other.is_a?(self.class) |
| 111 | + |
| 112 | + seconds <=> other.seconds |
| 113 | + end |
| 114 | + alias_method :eql?, :== |
| 115 | + end |
| 116 | +end |
0 commit comments