Sha256: 4aaa55df7c835252c5242f3ee56ba6a4339036576fd78cd1f2c921c10738fc41

Contents?: true

Size: 818 Bytes

Versions: 124

Compression:

Stored size: 818 Bytes

Contents

# frozen_string_literal: true

class ReeDatetime::MonthsDiff
  include Ree::FnDSL

  fn :months_diff do
    link :round_helper, from: :ree_number, import: -> { ROUND_MODES }
    link :slice, from: :ree_hash
  end

  MONTH_SECONDS = 60.0 * 60 * 24 * 30

  doc("Returns time difference in month (30 days range)")
  contract(
    Or[Date, DateTime, Time],
    Or[Date, DateTime, Time],
    Ksplat[
      round_mode?: Or[*ROUND_MODES]
    ] => Integer).throws(ArgumentError)
  def call(start_time, end_time, **opts)
    s_delta = end_time.to_time.to_i - start_time.to_time.to_i

    raise ArgumentError, "start_time bigger than end_time" if s_delta < 0

    m_delta = s_delta / MONTH_SECONDS
    opts[:precision] = 0

    round_helper(
      m_delta,
      **slice(opts, [:precision, :round_mode])
    ).to_i
  end
end



Version data entries

124 entries across 124 versions & 1 rubygems

Version Path
ree_lib-1.0.64 lib/ree_lib/packages/ree_datetime/package/ree_datetime/functions/months_diff.rb
ree_lib-1.0.63 lib/ree_lib/packages/ree_datetime/package/ree_datetime/functions/months_diff.rb
ree_lib-1.0.62 lib/ree_lib/packages/ree_datetime/package/ree_datetime/functions/months_diff.rb
ree_lib-1.0.61 lib/ree_lib/packages/ree_datetime/package/ree_datetime/functions/months_diff.rb
ree_lib-1.0.60 lib/ree_lib/packages/ree_datetime/package/ree_datetime/functions/months_diff.rb
ree_lib-1.0.59 lib/ree_lib/packages/ree_datetime/package/ree_datetime/functions/months_diff.rb
ree_lib-1.0.58 lib/ree_lib/packages/ree_datetime/package/ree_datetime/functions/months_diff.rb
ree_lib-1.0.57 lib/ree_lib/packages/ree_datetime/package/ree_datetime/functions/months_diff.rb
ree_lib-1.0.56 lib/ree_lib/packages/ree_datetime/package/ree_datetime/functions/months_diff.rb
ree_lib-1.0.55 lib/ree_lib/packages/ree_datetime/package/ree_datetime/functions/months_diff.rb
ree_lib-1.0.54 lib/ree_lib/packages/ree_datetime/package/ree_datetime/functions/months_diff.rb
ree_lib-1.0.53 lib/ree_lib/packages/ree_datetime/package/ree_datetime/functions/months_diff.rb
ree_lib-1.0.52 lib/ree_lib/packages/ree_datetime/package/ree_datetime/functions/months_diff.rb
ree_lib-1.0.51 lib/ree_lib/packages/ree_datetime/package/ree_datetime/functions/months_diff.rb
ree_lib-1.0.50 lib/ree_lib/packages/ree_datetime/package/ree_datetime/functions/months_diff.rb
ree_lib-1.0.49 lib/ree_lib/packages/ree_datetime/package/ree_datetime/functions/months_diff.rb
ree_lib-1.0.48 lib/ree_lib/packages/ree_datetime/package/ree_datetime/functions/months_diff.rb
ree_lib-1.0.47 lib/ree_lib/packages/ree_datetime/package/ree_datetime/functions/months_diff.rb
ree_lib-1.0.46 lib/ree_lib/packages/ree_datetime/package/ree_datetime/functions/months_diff.rb
ree_lib-1.0.45 lib/ree_lib/packages/ree_datetime/package/ree_datetime/functions/months_diff.rb