A simpler, more lightweight solution:
Time.now.getlocal('-08:00')
Time.now.getlocal(-28800)
Well documented here.
Update 2021.12.23: If you’ve got a tzdata
timezone name like ‘US/Pacific’ instead of an offset and you’re willing to pull in the tzinfo
gem, you could also do this (with thanks to both @chadoh and @kevin from below):
require 'tzinfo'
TZInfo::Timezone.get('US/Pacific').now
Not really the problem as posed, but maybe helpful to folks in the future.
If you want to do this for moments other than #now
, you should study up on the Ruby Time class, particularly Time#gm
and Time#local
, and the Ruby TZInfo classes, particularly TZInfo::Timezone.get
and TZInfo::Timezone#period_for_local