Change Time zone in pure ruby (not rails)

前端 未结 5 1326
暖寄归人
暖寄归人 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 08:07

    If you don't want to load a ton of libraries, and if you don't mind doing it the poor man's way, and you know your offset is -8 for example, you can subtract 8*3600 seconds to get the new time. For example,

    offset=-8
    t = Time.now + offset * 3600
    

    You'll need to adjust for day light savings time yourself though, also this doesn't change the time zone so %z won't be right.

提交回复
热议问题