Change Time zone in pure ruby (not rails)

前端 未结 5 1553
抹茶落季
抹茶落季 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:50

    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.

提交回复
热议问题