ruby get Time in given timezone

前端 未结 9 1255
情书的邮戳
情书的邮戳 2020-12-28 13:17

In ruby, how can I get current time in a given timezone? I know the offset from UTC, and want to get the current time in the timezone with that offset.

9条回答
  •  长情又很酷
    2020-12-28 14:02

    Just add or subtract the appropriate number of seconds:

    >> utc = Time.utc(2009,5,28,10,1)           # given a utc time
    => Thu May 28 10:01:00 UTC 2009
    >> bst_offset_in_mins = 60                  # and an offset to another timezone
    => 60
    >> bst = t + (bst_offset_in_mins * 60)      # just add the offset in seconds
    => Thu May 28 11:01:00 UTC 2009             # to get the time in the new timezone
    

提交回复
热议问题