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”)
/\A(\d{1,2}):(\d{2})\s*(am|pm)\s*ET\z/i
Instance Method Summary collapse
-
#build_et_time(hour, minute) ⇒ Time
private
Builds a Time object in Eastern timezone.
-
#convert_et_to_local(status) ⇒ String
private
Converts Eastern time to local time zone.
-
#convert_to_24h(hour, period) ⇒ Integer
private
Converts 12-hour format to 24-hour format.
-
#format_local_time(et_time) ⇒ String
private
Formats a time in the local timezone.
-
#local_time_zone_abbr ⇒ String
private
Returns the local timezone abbreviation.
-
#parse_et_time(match) ⇒ Time
private
Parses Eastern time components into a Time object.
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
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
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
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
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_abbr ⇒ 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.
Returns the local timezone abbreviation
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
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 |