Sha256: fcb43b757b665ee885556b2e93a077d6be6ae643f5ce5fedb41d4bdc9e410b69

Contents?: true

Size: 1.32 KB

Versions: 1

Compression:

Stored size: 1.32 KB

Contents

# frozen_string_literal: true

require "inq/version"
require "date"

module Inq
  ##
  # Various helper functions for working with DateTime objects.
  module DateTimeHelpers
    # Check if +left+ is less than or equal to +right+, where both are string
    # representations of a date.
    #
    # @param left [String] A string representation of a date.
    # @param right [String] A string representation of a date.
    # @return [Boolean] True if +left+ is less-than-or-equal to +right+,
    #   otherwise false.
    def date_le(left, right)
      left  = str_to_dt(left)
      right = str_to_dt(right)

      left <= right
    end

    # Check if +left+ is greater than or equal to +right+, where both are string
    # representations of a date.
    #
    # @param left [String] A string representation of a date.
    # @param right [String] A string representation of a date.
    # @return [Boolean] True if +left+ is greater-than-or-equal to +right+,
    #   otherwise false.
    def date_ge(left, right)
      left  = str_to_dt(left)
      right = str_to_dt(right)

      left >= right
    end

    private

    # Converts a +String+ representation of a date to a +DateTime+.
    #
    # @param str [String] A date.
    # @return [DateTime] A DateTime representation of +str+.
    def str_to_dt(str)
      DateTime.parse(str)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
inq-26.0.0 lib/inq/date_time_helpers.rb