Module: NBA::CLI::Formatters::TimeFormatters

Included in:
NBA::CLI::Formatters
Defined in:
lib/nba/cli/formatters/time_formatters.rb

Overview

Formatters for time-related output

Constant Summary collapse

ET_TIME_PATTERN =

Pattern to match Eastern time format (e.g., “7:30 pm ET”)

Returns:

  • (Regexp)

    the time pattern

/\A(\d{1,2}):(\d{2})\s*(am|pm)\s*ET\z/i

Instance Method Summary collapse

Instance Method Details

#build_et_time(hour, minute) ⇒ Time

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Builds a Time object in Eastern timezone

Parameters:

  • hour (Integer)

    the hour in 24-hour format

  • minute (Integer)

    the minute

Returns:

  • (Time)

    the time in Eastern timezone



56
57
58
59
# File 'lib/nba/cli/formatters/time_formatters.rb', line 56

def build_et_time(hour, minute)
  today = Date.today
  Time.new(today.year, today.month, today.day, hour, minute, nil, "-05:00")
end

#convert_et_to_local(status) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Converts Eastern time to local time zone

Parameters:

  • status (String)

    the status string (may contain ET time)

Returns:

  • (String)

    the status with time converted to local zone



15
16
17
18
19
20
# File 'lib/nba/cli/formatters/time_formatters.rb', line 15

def convert_et_to_local(status)
  match = ET_TIME_PATTERN.match(status)
  return status unless match

  format_local_time(parse_et_time(match))
end

#convert_to_24h(hour, period) ⇒ Integer

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Converts 12-hour format to 24-hour format

Parameters:

  • hour (Integer)

    the hour in 12-hour format

  • period (String)

    “am” or “pm”

Returns:

  • (Integer)

    the hour in 24-hour format



42
43
44
45
46
47
48
# File 'lib/nba/cli/formatters/time_formatters.rb', line 42

def convert_to_24h(hour, period)
  if period.eql?("am")
    hour.eql?(12) ? 0 : hour
  else
    hour.eql?(12) ? 12 : hour + 12
  end
end

#format_local_time(et_time) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Formats a time in the local timezone

Parameters:

  • et_time (Time)

    the time in Eastern timezone

Returns:

  • (String)

    formatted local time string



66
67
68
69
70
# File 'lib/nba/cli/formatters/time_formatters.rb', line 66

def format_local_time(et_time)
  local_time = et_time.localtime
  zone_abbr = local_time_zone_abbr
  local_time.strftime("%-I:%M %p #{zone_abbr}")
end

#local_time_zone_abbrString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the local timezone abbreviation

Returns:

  • (String)

    the timezone abbreviation (e.g., “PST”, “EST”)



76
77
78
# File 'lib/nba/cli/formatters/time_formatters.rb', line 76

def local_time_zone_abbr
  Time.now.zone || "ET"
end

#parse_et_time(match) ⇒ Time

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parses Eastern time components into a Time object

Parameters:

  • match (MatchData)

    the regex match with hour, minute, am/pm

Returns:

  • (Time)

    the time in Eastern timezone



27
28
29
30
31
32
33
34
# File 'lib/nba/cli/formatters/time_formatters.rb', line 27

def parse_et_time(match)
  hour = Integer(match[1] || 0)
  minute = Integer(match[2] || 0)
  period = (match[3] || "am").downcase

  hour = convert_to_24h(hour, period)
  build_et_time(hour, minute)
end