Module: OpeningHoursConverter::Utils

Included in:
Iterator, OpeningHoursParser, WeekIndex
Defined in:
lib/opening_hours_converter/utils.rb

Instance Method Summary collapse

Instance Method Details

#add_days_to_time(time, days) ⇒ Object



12
13
14
# File 'lib/opening_hours_converter/utils.rb', line 12

def add_days_to_time(time, days)
  time + days * seconds_in_day
end

#datetime_to_time(datetime) ⇒ Object



43
44
45
# File 'lib/opening_hours_converter/utils.rb', line 43

def datetime_to_time(datetime)
  Time.new(datetime.year, datetime.month, datetime.day, datetime.hour, datetime.min, datetime.sec, datetime.zone)
end

#day_difference(from, to) ⇒ Object



16
17
18
# File 'lib/opening_hours_converter/utils.rb', line 16

def day_difference(from, to)
  to - from
end

#last_day_of_month(month, year) ⇒ Object



34
35
36
37
# File 'lib/opening_hours_converter/utils.rb', line 34

def last_day_of_month(month, year)
  return [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month] unless leap_year?(year) && month == 1
  return 29
end

#leap_year?(year = Time.now.year) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/opening_hours_converter/utils.rb', line 30

def leap_year?(year = Time.now.year)
  year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)
end

#reindex_sunday_week_to_monday_week(wday) ⇒ Object



3
4
5
# File 'lib/opening_hours_converter/utils.rb', line 3

def reindex_sunday_week_to_monday_week(wday)
  (wday + 6) % 7
end

#seconds_in_dayObject



26
27
28
# File 'lib/opening_hours_converter/utils.rb', line 26

def seconds_in_day
  24 * 60 * 60
end

#time_to_datetime(time) ⇒ Object



39
40
41
# File 'lib/opening_hours_converter/utils.rb', line 39

def time_to_datetime(time)
  DateTime.new(time.year, time.month, time.day, time.hour, time.min, time.sec, Rational(time.gmt_offset / 3600, 24))
end

#timstring_as_minutes(time) ⇒ Object



7
8
9
10
# File 'lib/opening_hours_converter/utils.rb', line 7

def timstring_as_minutes(time)
  values = time.split(':')
  values[0].to_i * 60 + values[1].to_i
end

#week_difference(from, to) ⇒ Object



20
21
22
23
24
# File 'lib/opening_hours_converter/utils.rb', line 20

def week_difference(from, to)
  day_diff = to - from
  day_diff -= (day_diff % 7)
  (day_diff / 7).to_i
end