diff --git a/lib/et-orbi/time.rb b/lib/et-orbi/time.rb index cf531e9..9dc00dc 100644 --- a/lib/et-orbi/time.rb +++ b/lib/et-orbi/time.rb @@ -110,8 +110,11 @@ def seconds=(f) def zone=(z) + tz = self.class.get_tzone(z || zone) + fail ArgumentError.new("Cannot determine timezone from #{z.inspect}") \ + unless tz - @zone = self.class.get_tzone(zone || :current) + @zone = tz touch diff --git a/test/eo_time_test.rb b/test/eo_time_test.rb index c9fc480..f53f9e0 100644 --- a/test/eo_time_test.rb +++ b/test/eo_time_test.rb @@ -113,6 +113,33 @@ end end + group '#zone=' do + + test 'assigns the requested timezone and invalidates the time cache' do + + ot = EtOrbi::EoTime.new(0, 'UTC') + ot.hour + + ot.zone = 'Asia/Kolkata' + + assert ot.zone.name, 'Asia/Kolkata' + assert ot.hour, 5 + assert ot.min, 30 + assert ot.to_f, 0.0 + end + + test 'does not change the timezone when assignment fails' do + + ot = EtOrbi::EoTime.new(0, 'UTC') + + assert_error( + lambda { ot.zone = 'Invalid/Timezone' }, + ArgumentError) + + assert ot.zone.name, 'UTC' + end + end + group '.utc' do [