Module: TimezoneHelper

Included in:
Kernel
Defined in:
lib/leagues/timezones.rb

Instance Method Summary collapse

Instance Method Details

#find_zone(league:, season:) ⇒ Object

Raises:

  • (ArgumentError)


133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/leagues/timezones.rb', line 133

def find_zone( league:, season: )
   ## note: do NOT pass in league struct! pass in key (string)

   raise ArgumentError, "league code as string|symbol expected"  unless league.is_a?(String) || league.is_a?(Symbol)

   @zones ||= begin
                zones = {}
                ['timezones_africa',
                 'timezones_america',
                 'timezones_asia',
                 'timezones_europe',
                 'timezones_middle_east',
                 'timezones_pacific',
                 'timezones_world',].each do |name|
                     recs = read_csv( "#{SportDb::Module::Leagues.root}/config/#{name}.csv" )
                     recs.each do |rec|
                        zone = UTC.find_zone( rec['zone'] )
                        if zone.nil?
                          ## raise ArgumentError - invalid zone

                          puts "!! ERROR - cannot find timezone in timezone db:"
                          pp rec
                          exit 1
                        end
                        ##  todo/fix - rename key to (league) code

                        zones[ rec['key']] = zone
                     end
                 end
                 zones
              end

   season      = Season( season )
   league_code = league.to_s.downcase
   
=begin
   ###
   ##  map code here - why? why not?
   ##   or (always) require canoncial code???
   league_info = LeagueCodes.find_by( code: league, season: season )

   league_code = if league_info
                    ## lookup first try by league+season
                    league_info['code']   ## use canonical name
                 else
                    ## fallback; no league found
                    ##    or report error in the future - why? why not?
                    league.to_s.downcase
                 end
=end



   ##  e.g. world+2022, etc.

   key = "#{league_code}+#{season}"
   zone = @zones[key]

   ## try league e.g. eng.1 etc.

   zone = @zones[league_code]  if zone.nil?

   ## try first code only (country code )

   if zone.nil?
     code, _  = league_code.split( '.', 2 )
     zone = @zones[code]
   end

   zone
end

#find_zone!(league:, season:) ⇒ Object



123
124
125
126
127
128
129
130
# File 'lib/leagues/timezones.rb', line 123

def find_zone!( league:, season: )
  zone = find_zone( league: league, season: season )
  if zone.nil?  ## still not found; report error

    puts "!! ERROR: no timezone found for #{league} #{season}"
    exit 1
  end
  zone
end