Module: Booker::Concerns::DateTimeConcern

Included in:
V4::Models::Model
Defined in:
lib/booker/concerns/date_time_concern.rb

Instance Method Summary collapse

Instance Method Details

#time_from_booker_datetime(booker_datetime) ⇒ Object

Booker’s API hands you back all time as if the business is in server time. First load the time in server time, then return the same hours and minutes in current time zone.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/booker/concerns/date_time_concern.rb', line 6

def time_from_booker_datetime(booker_datetime)
  timestamp = booker_datetime[/\/Date\((.\d+)[\-\+]/, 1].to_i / 1000.to_f

  original_tz = Time.zone
  begin
    # Booker's server is always EST
    Time.zone = Booker::Client::BOOKER_SERVER_TIMEZONE

    booker_time = Time.zone.at(timestamp)
  ensure
    Time.zone = original_tz
  end

  # Convert it back to location time without changing hours and minutes
  Time.zone.parse(booker_time.strftime('%Y-%m-%d %H:%M:%S'))
end

#time_to_booker_datetime(time) ⇒ Object

Booker’s API requires times to be sent in as if the business is in Eastern Time!



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/booker/concerns/date_time_concern.rb', line 24

def time_to_booker_datetime(time)
  original_tz = Time.zone

  begin
    # Booker's server is always EST
    Time.zone = Booker::Client::BOOKER_SERVER_TIMEZONE
    timestamp = (Time.zone.parse(time.strftime("%Y-%m-%dT%H:%M:%S")).to_f * 1000).to_i
  ensure
    Time.zone = original_tz
  end

  "/Date(#{timestamp})/"
end

#timezone_from_booker_offset!(booker_timezone_name) ⇒ Object

Raises:



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/booker/concerns/date_time_concern.rb', line 53

def timezone_from_booker_offset!(booker_timezone_name)
  booker_offset_match = booker_timezone_name.scan(/(\A)(.*)(?=\))/).first

  if booker_offset_match.present?
    booker_offset = booker_offset_match.delete_if { |match| match.blank? }.first

    if booker_offset
      booker_timezone_map_key = Booker::Helpers::ActiveSupport.booker_timezone_names.find do |key|
        key.start_with?(booker_offset)
      end

      return Booker::Helpers::ActiveSupport.to_active_support(booker_timezone_map_key) if booker_timezone_map_key
    end
  end

  raise Booker::Error
end

#timezone_from_booker_timezone(booker_timezone_name) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/booker/concerns/date_time_concern.rb', line 38

def timezone_from_booker_timezone(booker_timezone_name)
  normalized_booker_timezone_name = Booker::Helpers::ActiveSupport.to_active_support(booker_timezone_name)
  return normalized_booker_timezone_name if normalized_booker_timezone_name.present?

  begin
    Booker::Helpers::LoggingHelper.log_issue(
        'Unable to find time zone name using Booker::Helpers::ActiveSupport.to_active_support',
        booker_timezone_name: booker_timezone_name
    )
  rescue
  end

  timezone_from_booker_offset!(booker_timezone_name)
end

#to_wday(booker_wday) ⇒ Object



71
# File 'lib/booker/concerns/date_time_concern.rb', line 71

def to_wday(booker_wday); Date.parse(booker_wday).wday; end