module Montrose::Utils
Constants
- MAX_DAYS_IN_MONTH
- MAX_DAYS_IN_YEAR
- MAX_HOURS_IN_DAY
- MAX_WEEKS_IN_YEAR
Public Instance Methods
Source
# File lib/montrose/utils.rb, line 31 def as_date(time) as_time(time).to_date end
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
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
Source
# File lib/montrose/utils.rb, line 51 def days_in_year(year) ::Montrose::Utils.days_in_month(2, year) + 337 end
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
Source
# File lib/montrose/utils.rb, line 27 def normalize_time(time) time&.change(usec: 0) end
Recurrence
at fractions of a second are not recognized
Source
# File lib/montrose/utils.rb, line 35 def parse_time(*args) ::Time.zone.nil? ? ::Time.parse(*args) : ::Time.zone.parse(*args) end
Source
# File lib/montrose/utils.rb, line 57 def to_index(string) /^\d+/.match?(string) ? string.to_i : -1 end
Returns string.to_i only if string fully matches an integer otherwise ensures that return value won’t match a valid index