Change Time zone in pure ruby (not rails)

前端 未结 5 1308
暖寄归人
暖寄归人 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:43

    Get the current time in utc, then add the offset:

    Time.now.utc + Time.zone_offset('PDT')
    #=> 2015-12-01 18:10:07 UTC
    

    The problem here is that PDT is fixed to -8, PST is fixed to -7 and Pacific Zone (one that adjusts between the two based on time of year and daylight savings) does not exist, for that you likely need ActiveSupport.

提交回复
热议问题