Sha256: b8fe4d40b96e7d9e760ac21bc12d10b55ba442d24df407f83ef1c6cfd305f330

Contents?: true

Size: 990 Bytes

Versions: 9

Compression:

Stored size: 990 Bytes

Contents

# frozen_string_literal: true

module Timely
  module Date
    def at_time(hour = nil, minute = nil, second = nil)
      if hour.is_a?(::Time)
        time = hour
        hour = time.hour
        minute = time.min
        second = time.sec
      end

      ::Time.local(year, month, day, hour, minute, second)
    end

    alias at at_time

    # returns true if date between from and to
    # however if from and/or to are nil, it ignores that query
    # e.g. date.between?(from, nil) is "date > from",
    #      date.between?(from, nil) is "date < to"
    #      date.between?(nil, nil)  is true
    def between?(from, to)
      from = from.to_date if from && !from.is_a?(Date)
      to   = to.to_date if to && !to.is_a?(Date)

      if from && to
        self >= from && self <= to
      elsif from
        self >= from
      elsif to
        self <= to
      else
        true # i.e. no restrictive range return true
      end
    end
  end
end

class Date
  include Timely::Date
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
timely-0.14.0 lib/timely/date.rb
timely-0.13.0 lib/timely/date.rb
timely-0.12.0 lib/timely/date.rb
timely-0.11.0 lib/timely/date.rb
timely-0.10.0 lib/timely/date.rb
timely-0.9.0 lib/timely/date.rb
timely-0.8.0 lib/timely/date.rb
timely-0.7.0 lib/timely/date.rb
timely-0.6.0 lib/timely/date.rb