Change Time zone in pure ruby (not rails)

前端 未结 5 1316
暖寄归人
暖寄归人 2021-02-14 07:02

I am building a Sinatra site which has mixed UTC/PST data sources, but will be viewed in PST. So I need a way to easily convert Time objects from UTC to PST. Without Rails, I d

5条回答
  •  伪装坚强ぢ
    2021-02-14 07:53

    I recently did the same thing on an iron.io worker which default time is UTC

    # For generality/testing, only require what you need. 
    require 'active_support/all'  
    
    # Set the Timezone 
    Time.zone = "Asia/Hong_Kong" 
    
    t = Time.zone.now
    #=> 2015-01-20 16:44:58 +0800
    

    For info on the supported Timezones take a look at ActiveSupport http://api.rubyonrails.org/classes/ActiveSupport/TimeZone.html

提交回复
热议问题