Avael Kross Avael Kross - 4 months ago 21
Ruby Question

Invalid timezone

Just wondering why it happens:

1.9.3-p327 :001 > Time.now.zone
=> "YEKT"

1.9.3-p327 :002 > Time.now.in_time_zone("YEKT")
ArgumentError: Invalid Timezone: YEKT

YEKT - is the timezone of my city(Yekaterinburg).

What the hell is going on? I'm setting up a project and trying to fix all tests, but can't fix this behavior. Or can I emulate another current timezone(without changing code ideally)?


Why it happens(thanks @Athar):

Time.now.zone returns a system time zone, while in_time_zone works with strict list (ActiveSupport::TimeZone.all.collect(&:name))

Workaround: You can specify your timezone that way:

TZ=UTC rails console

1.9.3-p327 :001 > Time.now.zone
=> "UTC"

"UTC" is a valid timezone, so everything works.