Avael Kross Avael Kross - 1 year ago 145
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)?

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download