ruby get Time in given timezone

前端 未结 9 1233
情书的邮戳
情书的邮戳 2020-12-28 13:17

In ruby, how can I get current time in a given timezone? I know the offset from UTC, and want to get the current time in the timezone with that offset.

9条回答
  •  心在旅途
    2020-12-28 13:38

    I tried a gem install active_support, it installed activesupport-3.00, which gave me an error:

    "You don't have tzinfo installed in your application. Please add it to your Gemfile and run bundle install"

    tzinfo is a gem that ActiveSupport uses -- it was a little cleaner for my purposes, doesn't have any external dependencies -- the downside is that it appears to leave everything looking like it is UTC, so if you want your timezones to look correct, gem install activerecord will install everything you need. I'm including this answer mostly for reference for others that run into the same issue / googlability.

    (use gem install tzinfo) to install the gem

    require 'tzinfo'
    zone = TZInfo::Timezone.get('US/Eastern')
    puts zone.now
    

    there are a number of different ways to get the timezones, but you can see a list using

    TZInfo::Timezone.all
    

提交回复
热议问题