class Zakuro::Calculation::Cycle::AbstractRemainder
大余小余(時刻情報) @abstract 大余小余計算に必要な処理を行う、暦に依存しない汎用的なクラス
-
十干十二支(60日)を上限とした「日時分秒」の情報で、日付(date)/時刻(time)と部分的に重なる概念
-
「15日1012分5秒」のような形式で表される
-
分の上限で大余に繰り上げる
-
秒の上限で1分に繰り上げる
Constants
- LIMIT
@return [Integer] 大余上限
Attributes
@return [Integer] 1大余に必要な小余(暦によって基数が異なる)
@return [Integer] 繰り上げなしの小余
@return [Integer] 1小余に必要な秒(暦によって基数が異なる)
@return [Integer] 大余(“日”に相当)
繰り上げ有無 @return [True] 繰り上げあり @return [False] 繰り上げなし
@return [Integer] 小余(“分”に相当)
@return [Integer] 秒
Public Class Methods
初期化
@param [Integer] base_day
1大余に必要な小余(暦によって基数が異なる) @param [Integer] base_mitune 1小余に必要な秒(暦によって基数が異なる) @param [Integer] day 大余(“日”に相当) @param [Integer] minute 小余(“分”に相当) @param [Integer] second 秒 @param [Integer] total 繰り上げなしの小余
# File lib/zakuro/calculation/cycle/abstract_remainder.rb, line 52 def initialize(base_day: -1, base_mitune: -1, day: -1, minute: -1, second: -1, total: -1) @base_limit = LIMIT @base_day = base_day @base_minute = base_mitune @limited = true set(day: day, minute: minute, second: second, total: total) end
Public Instance Methods
大小比較(!=)
@param [AbstractRemainder] other 他の大余小余
@return [True] 等しくない @return [False] 等しい
# File lib/zakuro/calculation/cycle/abstract_remainder.rb, line 279 def !=(other) !eql?(other) end
大小比較(<)
@param [AbstractRemainder] other 他の大余小余
@return [True] より小さい @return [False] 以上
# File lib/zakuro/calculation/cycle/abstract_remainder.rb, line 243 def <(other) down?(other) end
大小比較(<=)
@param [AbstractRemainder] other 他の大余小余
@return [True] 以下 @return [False] より大きい
# File lib/zakuro/calculation/cycle/abstract_remainder.rb, line 255 def <=(other) down?(other) || eql?(other) end
大小比較(==)
@param [AbstractRemainder] other 他の大余小余
@return [True] 等しい @return [False] 等しくない
# File lib/zakuro/calculation/cycle/abstract_remainder.rb, line 267 def ==(other) eql?(other) end
大小比較(>)
@param [AbstractRemainder] other 他の大余小余
@return [True] より大きい @return [False] 以下
# File lib/zakuro/calculation/cycle/abstract_remainder.rb, line 219 def >(other) up?(other) end
大小比較(>=)
@param [AbstractRemainder] other 他の大余小余
@return [True] 以上 @return [False] より小さい
# File lib/zakuro/calculation/cycle/abstract_remainder.rb, line 231 def >=(other) up?(other) || eql?(other) end
(非破壊的に)加算する
@param [AbstractRemainder] other 他の大余小余
@return [AbstractRemainder] 加算結果
# File lib/zakuro/calculation/cycle/abstract_remainder.rb, line 152 def add(other) invalid?(param: other) day = @day + other.day minute = @minute + other.minute second = @second + other.second day, minute, second = carry(day, minute, second) clone.set(day: day, minute: minute, second: second) end
(破壊的に)加算する
@param [AbstractRemainder] other 他の大余小余
@return [AbstractRemainder] 加算結果
# File lib/zakuro/calculation/cycle/abstract_remainder.rb, line 169 def add!(other) result = add(other) @day = result.day @minute = result.minute @second = result.second self end
繰り上げる
@return [AbstractRemainder] 繰り上げ結果
# File lib/zakuro/calculation/cycle/abstract_remainder.rb, line 397 def carry! @day, @minute, @second = carry(@day, @minute, @second) self end
秒を含めた小余を返す
@return [Float] 小余
# File lib/zakuro/calculation/cycle/abstract_remainder.rb, line 354 def float_minute @minute + @second.to_f / @base_minute end
小余(秒切り捨て)を返す
@note 切り捨て前に繰り上げる
@return [Integer] 小余(秒切り捨て)
# File lib/zakuro/calculation/cycle/abstract_remainder.rb, line 344 def floor_minute result = float_minute result.floor end
特定の文字フォーマットにして出力する
@param [String] form フォーマット(大余、小余、秒それぞれを%dで指定する)
@return [String] フォーマットした結果
# File lib/zakuro/calculation/cycle/abstract_remainder.rb, line 386 def format(form: '%d-%d') return '' if invalid? super(form, @day, @minute, @second) end
無効かどうか
@param [AbstractRemainder] param 自クラスのインスタンス(デフォルトは自身の参照)
@return [True] 無効 @return [False] 有効
# File lib/zakuro/calculation/cycle/abstract_remainder.rb, line 116 def invalid?(param: self) raise ArgumentError, 'unmatch parameter type' unless param.is_a?(AbstractRemainder) param.day == -1 || param.minute == -1 || param.second == -1 end
繰り上げ処理を外す
@return [AbstractRemainder] 自身の参照
# File lib/zakuro/calculation/cycle/abstract_remainder.rb, line 91 def lift_limit @limited = false self end
大余に四捨五入した結果を返す(秒は除外する)
@return [AbstractRemainder] 大余
# File lib/zakuro/calculation/cycle/abstract_remainder.rb, line 363 def round day = @day day += 1 if @minute >= (@base_day / 2) initialize(day, 0, 0) end
日が同じ十干かどうか(大小の月判定)
@param [Integer] other 大余
@return [True] 当月朔日と翌月朔日が同じ十干(「大」の月) @return [False] 当月朔日と翌月朔日が異なる十干(「小」の月)
# File lib/zakuro/calculation/cycle/abstract_remainder.rb, line 139 def same_remainder_divided_by_ten?(other:) day = @day % LIMIT (day % 10) == (other % 10) end
値を設定する
@param [Integer] day 大余(“日”に相当) @param [Integer] minute 小余(“分”に相当) @param [Integer] second 秒 @param [Integer] total 繰り上げなしの小余
@return [AbstractRemainder] 自身の参照
# File lib/zakuro/calculation/cycle/abstract_remainder.rb, line 72 def set(day: -1, minute: -1, second: -1, total: -1) @day = day @minute = minute @second = second if total != -1 @day = (total.to_f / @base_day).floor @minute = (total % @base_day) @second = 0 end self end
繰り上げ処理を設定する
@return [AbstractRemainder] 自身の参照
# File lib/zakuro/calculation/cycle/abstract_remainder.rb, line 102 def set_limit @limited = true self end
(非破壊的に)除算する
@param [AbstractRemainder] other 他の大余小余
@return [AbstractRemainder] 除算結果
# File lib/zakuro/calculation/cycle/abstract_remainder.rb, line 185 def sub(other) invalid?(param: other) day = @day - other.day minute = @minute - other.minute second = @second - other.second day, minute, second = carry(day, minute, second) clone.set(day: day, minute: minute, second: second) end
(破壊的に)除算する
@param [AbstractRemainder] other 他の大余小余
@return [AbstractRemainder] 除算結果
# File lib/zakuro/calculation/cycle/abstract_remainder.rb, line 202 def sub!(other) result = sub(other) @day = result.day @minute = result.minute @second = result.second self end
小余に揃えた結果を返す(秒は除外する)
@return [Integer] 小余
# File lib/zakuro/calculation/cycle/abstract_remainder.rb, line 333 def to_minute @day * @base_day + @minute end
文字化する
@return [String] 文字化
# File lib/zakuro/calculation/cycle/abstract_remainder.rb, line 375 def to_s "大余(日): #{@day}, 小余(分): #{@minute}, 小余(秒): #{@second}" end
(非破壊的に)進朔する
@see eco.mtk.nao.ac.jp/koyomi/wiki/C2C0B1A2C2C0CDDBCEF12FBFCABAF3.html
進朔とは、朔の瞬間が1日の3/4すなわち夕方以降となる場合に、その日ではなく1日進めて翌日を1日目にする方法のことです。 進朔するかどうかの基準時刻を進朔限といいます。宣明暦では1日8400分ですから、進朔限は6300分で、それ以降だと進朔されます。
@return [AbstractRemainder] 進朔結果
# File lib/zakuro/calculation/cycle/abstract_remainder.rb, line 298 def up_on_new_moon cloned = clone limit = (base_day * 3) / 4 if @minute >= limit day, minute, second = carry((@day + 1), @minute, @second) return cloned.set(day: day, minute: minute, second: second) end cloned end
(破壊的に)進朔する
@see eco.mtk.nao.ac.jp/koyomi/wiki/C2C0B1A2C2C0CDDBCEF12FBFCABAF3.html
進朔とは、朔の瞬間が1日の3/4すなわち夕方以降となる場合に、その日ではなく1日進めて翌日を1日目にする方法のことです。 進朔するかどうかの基準時刻を進朔限といいます。宣明暦では1日8400分ですから、進朔限は6300分で、それ以降だと進朔されます。
@return [AbstractRemainder] 進朔結果
# File lib/zakuro/calculation/cycle/abstract_remainder.rb, line 319 def up_on_new_moon! result = up_on_new_moon @day = result.day @minute = result.minute @second = result.second self end
十干十二支名
@return [String] 十干十二支名
# File lib/zakuro/calculation/cycle/abstract_remainder.rb, line 127 def zodiac_name Cycle::Zodiac.day_name(day: @day) end
Private Instance Methods
繰り上げ、繰り下げ
# File lib/zakuro/calculation/cycle/abstract_remainder.rb, line 406 def carry(param_day, param_minute, param_second) # NOTE: 計算方法としてマイナスでも徐算・剰余算の結果をプラス同様に扱う minute, second = carry_second(param_minute, param_second) day, minute = carry_minute(param_day, minute) day = carry_day(day, @limited) [day, minute, second] end
# File lib/zakuro/calculation/cycle/abstract_remainder.rb, line 446 def carry_day(day, limited) sign = day.negative? ? -1 : 1 abs = sign * day carried = sign * (abs % @base_limit) if limited carried += @base_limit if sign.negative? carried end
# File lib/zakuro/calculation/cycle/abstract_remainder.rb, line 431 def carry_minute(param_day, param_minute) sign = param_minute.negative? ? -1 : 1 abs = sign * param_minute day = param_day + (sign * (abs / @base_day).floor) minute = sign * (abs % @base_day) if sign.negative? minute += @base_day day -= 1 end [day, minute] end
# File lib/zakuro/calculation/cycle/abstract_remainder.rb, line 417 def carry_second(param_minute, param_second) sign = param_second.negative? ? -1 : 1 abs = sign * param_second minute = param_minute + (sign * (abs / @base_minute).floor) second = sign * (abs % @base_minute) if sign.negative? second += @base_minute minute -= 1 end [minute, second] end
# File lib/zakuro/calculation/cycle/abstract_remainder.rb, line 471 def down?(other) invalid?(param: other) day = other.day minute = other.float_minute return true if @day < day return false if @day > day float_minute < minute end
# File lib/zakuro/calculation/cycle/abstract_remainder.rb, line 465 def eql?(other) invalid?(param: other) (@day == other.day && float_minute == other.float_minute) end
# File lib/zakuro/calculation/cycle/abstract_remainder.rb, line 454 def up?(other) invalid?(param: other) day = other.day minute = other.float_minute return true if @day > day return false if @day < day float_minute > minute end