Sha256: b85bd3cd2a346cee5376e182a3ec2cb6364c06678e62b5a22eec76717f6cb5a6

Contents?: true

Size: 816 Bytes

Versions: 1

Compression:

Stored size: 816 Bytes

Contents

# frozen_string_literal: true

module Blackcal
  # Time utils module
  module TimeUtil
    # Returns the week of month
    # @return [Integer] week of month
    # @see https://stackoverflow.com/a/30470158/955366
    def self.week_of_month(date_or_time, start_day = :sunday)
      date = date_or_time.to_date
      week_start_format = start_day == :sunday ? '%U' : '%W'

      week_of_month_start = Date.new(date.year, date.month, 1)
      week_of_month_start_num = week_of_month_start.strftime(week_start_format).to_i
      # Skip first week if doesn't contain a Thursday
      week_of_month_start_num += 1 if week_of_month_start.wday > 4

      month_week_index = date.strftime(week_start_format).to_i - week_of_month_start_num
      month_week_index + 1 # Add 1 so that first week is 1 and not 0
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
blackcal-0.5.0 lib/blackcal/time_util.rb