如何在 Ruby on Rails 中更改区域偏移量一段时间? [英] How do I change the zone offset for a time in Ruby on Rails?

查看:26
本文介绍了如何在 Ruby on Rails 中更改区域偏移量一段时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含时间的变量 foo,比如说今天下午 4 点,但是区域偏移量是错误的,即它位于错误的时区.如何更改时区?

I have a variable foo that contains a time, lets say 4pm today, but the zone offset is wrong, i.e. it is in the wrong time zone. How do I change the time zone?

当我打印它时我得到

Fri Jun 26 07:00:00 UTC 2009

所以没有偏移,我想将偏移设置为 -4 或东部标准时间.

So there is no offset, and I would like to set the offset to -4 or Eastern Standard Time.

我希望能够将偏移量设置为 Time 对象的属性,但这似乎不可用?

I would expect to be able to just set the offset as a property of the Time object, but that doesn't seem to be available?

推荐答案

在他们添加使 weppos 解决方案工作的代码之前,我使用的是 Rails 2.0.这就是我所做的

I'm using Rails 2.0 before they added the code that makes weppos solution work. Here's what I did

# Silly hack, because sometimes the input_date is in the wrong timezone
temp = input_date.to_time.to_a
temp[8] = true
temp[9] = "Eastern Daylight Time"
input_date = Time.local(*temp)

我将时间分解为 10 个元素的数组,更改时区,然后将数组转换回时间.

I break the time down into a 10 element array, change the timezone and then convert the array back into a time.

这篇关于如何在 Ruby on Rails 中更改区域偏移量一段时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆