Change Time zone in pure ruby (not rails)

前端 未结 5 1550
抹茶落季
抹茶落季 2021-02-14 07:04

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:55

    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

提交回复
热议问题