Module: HhTime

Defined in:
app/my_lib/hh_time.rb

Overview

Time extension method

Class Method Summary collapse

Class Method Details

.minimum_booking_time_exceeded?(date, start_time, minimum_booking_time, time_zone) ⇒ Boolean

Method to check if the current time plus a given minimum booking interval exceeds the specified start time.

Examples:

Usage

HhTime.minimum_booking_time_exceeded?('2024-09-09', '15:00', 30, 'Asia/Bangkok')
#=> true or false

Parameters:

  • date (Date)

    The date of the booking.

  • start_time (String)

    The start time in “HH:MM” format.

  • minimum_booking_time (Integer)

    The minimum booking time in minutes.

  • time_zone (String)

    The time zone to be used for the calculation.

Returns:

  • (Boolean)

    Returns true if the current time plus the minimum booking time exceeds the given start time, false otherwise.



24
25
26
27
28
29
30
31
# File 'app/my_lib/hh_time.rb', line 24

def minimum_booking_time_exceeded?(date, start_time, minimum_booking_time, time_zone)
  Time.use_zone time_zone do
    current_time_plus_buffer = Time.zone.now + minimum_booking_time.minutes
    start_time_object = Time.zone.parse("#{date} #{start_time}")

    current_time_plus_buffer > start_time_object
  end
end

.start_time_to_end_time_interval(start_time, end_time) ⇒ Object

return time interval, example: expect(HhTime.start_time_to_end_time_interval('15:00', '16:00')).to eq(['15:00', '15:15', '15:30', '15:45'])



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/my_lib/hh_time.rb', line 37

def start_time_to_end_time_interval(start_time, end_time)
  times = []

  init_start_time = Time.zone.parse(start_time)
  init_end_time = Time.zone.parse(end_time)

  loop do
    break if (init_start_time + 15.minutes) > init_end_time

    times.push init_start_time.strftime('%H:%M')

    init_start_time += 15.minutes
  end
  times
end

.time_has_passed?(date, start_time, time_zone) ⇒ Boolean

Returns:

  • (Boolean)


5
6
7
8
9
# File 'app/my_lib/hh_time.rb', line 5

def time_has_passed?(date, start_time, time_zone)
  Time.use_zone time_zone do
    Time.zone.parse("#{date} #{start_time}") < Time.zone.now
  end
end