Sha256: 15bcb64134970fe9d6f430b9bb236798ff8e481b155935287bdd950100bb72de

Contents?: true

Size: 802 Bytes

Versions: 124

Compression:

Stored size: 802 Bytes

Contents

# frozen_string_literal: true

class ReeDatetime::YearsDiff
  include Ree::FnDSL

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

  YEAR_SECONDS = 60.0 * 60 * 24 * 365

  doc("Returns time difference in years")
  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

    y_delta = s_delta / YEAR_SECONDS
    opts[:precision] = 0

    round_helper(
      y_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.84 lib/ree_lib/packages/ree_datetime/package/ree_datetime/functions/years_diff.rb
ree_lib-1.0.83 lib/ree_lib/packages/ree_datetime/package/ree_datetime/functions/years_diff.rb
ree_lib-1.0.82 lib/ree_lib/packages/ree_datetime/package/ree_datetime/functions/years_diff.rb
ree_lib-1.0.81 lib/ree_lib/packages/ree_datetime/package/ree_datetime/functions/years_diff.rb
ree_lib-1.0.80 lib/ree_lib/packages/ree_datetime/package/ree_datetime/functions/years_diff.rb
ree_lib-1.0.79 lib/ree_lib/packages/ree_datetime/package/ree_datetime/functions/years_diff.rb
ree_lib-1.0.78 lib/ree_lib/packages/ree_datetime/package/ree_datetime/functions/years_diff.rb
ree_lib-1.0.77 lib/ree_lib/packages/ree_datetime/package/ree_datetime/functions/years_diff.rb
ree_lib-1.0.76 lib/ree_lib/packages/ree_datetime/package/ree_datetime/functions/years_diff.rb
ree_lib-1.0.75 lib/ree_lib/packages/ree_datetime/package/ree_datetime/functions/years_diff.rb
ree_lib-1.0.74 lib/ree_lib/packages/ree_datetime/package/ree_datetime/functions/years_diff.rb
ree_lib-1.0.73 lib/ree_lib/packages/ree_datetime/package/ree_datetime/functions/years_diff.rb
ree_lib-1.0.72 lib/ree_lib/packages/ree_datetime/package/ree_datetime/functions/years_diff.rb
ree_lib-1.0.71 lib/ree_lib/packages/ree_datetime/package/ree_datetime/functions/years_diff.rb
ree_lib-1.0.70 lib/ree_lib/packages/ree_datetime/package/ree_datetime/functions/years_diff.rb
ree_lib-1.0.69 lib/ree_lib/packages/ree_datetime/package/ree_datetime/functions/years_diff.rb
ree_lib-1.0.68 lib/ree_lib/packages/ree_datetime/package/ree_datetime/functions/years_diff.rb
ree_lib-1.0.67 lib/ree_lib/packages/ree_datetime/package/ree_datetime/functions/years_diff.rb
ree_lib-1.0.66 lib/ree_lib/packages/ree_datetime/package/ree_datetime/functions/years_diff.rb
ree_lib-1.0.65 lib/ree_lib/packages/ree_datetime/package/ree_datetime/functions/years_diff.rb