module Montrose::Utils

Constants

MAX_DAYS_IN_MONTH
MAX_DAYS_IN_YEAR
MAX_HOURS_IN_DAY
MAX_WEEKS_IN_YEAR

Public Instance Methods

as_date(time) click to toggle source
# File lib/montrose/utils.rb, line 31
def as_date(time)
  as_time(time).to_date
end
as_time(time) click to toggle source
# File lib/montrose/utils.rb, line 12
def as_time(time)
  return nil unless time

  if time.is_a?(String)
    parse_time(time)
  elsif time.is_a?(ActiveSupport::TimeWithZone)
    time
  elsif time.respond_to?(:to_time)
    time.to_time
  else
    Array(time).flat_map { |d| as_time(d) }
  end
end
current_time() click to toggle source
# File lib/montrose/utils.rb, line 39
def current_time
  ::Time.current
end
days_in_month(month, year = current_time.year) click to toggle source
# File lib/montrose/utils.rb, line 43
def days_in_month(month, year = current_time.year)
  date = ::Date.new(year, month, 1)
  ((date >> 1) - date).to_i
end
days_in_year(year) click to toggle source

Returns the number of days in the given year. If no year is specified, it will use the current year. github.com/rails/rails/pull/22244

# File lib/montrose/utils.rb, line 51
def days_in_year(year)
  ::Montrose::Utils.days_in_month(2, year) + 337
end
normalize_time(time) click to toggle source

Recurrence at fractions of a second are not recognized

# File lib/montrose/utils.rb, line 27
def normalize_time(time)
  time&.change(usec: 0)
end
parse_time(*args) click to toggle source
# File lib/montrose/utils.rb, line 35
def parse_time(*args)
  ::Time.zone.nil? ? ::Time.parse(*args) : ::Time.zone.parse(*args)
end
to_index(string) click to toggle source

Returns string.to_i only if string fully matches an integer otherwise ensures that return value won’t match a valid index

# File lib/montrose/utils.rb, line 57
def to_index(string)
  /^\d+/.match?(string) ? string.to_i : -1
end