Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 448 Vote(s) - 3.52 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do I alter the timezone of a DateTime in Ruby?

#1
Reading, writing, and serializing dates and times while keeping the time zone constant is becoming annoying. I'm using Ruby (and Rails 3.0) and am trying to alter the time zone of a DateTime. (to UTC) but not the time itself.

I want this:

t = DateTime.now
t.hour
-> 4
t.offset = 0
t.hour
-> 4
t.utc?
-> true

The closest I have come is this, but it's not intuitive.

t = DateTime.now
t.hour
-> 4
t += t.offset
t = t.utc
t.hour
-> 4
t.utc?
-> true

Is there any better way?
Reply

#2
If someone(like me) has **time zone in string format like "Pacific Time (US & Canada)"**

Than this is the best way i found:

```ruby
datetime.change(ActiveSupport::TimeZone[time_zone_string].formatted_offset(false))
```
Reply

#3
You can specify the Time Zone you want to use in your app by adding the following line in the config file (config/application.rb):
config.time_zone = 'Mumbai'

You can find the official documentation for the same here:

[To see links please register here]


The other option is that you "Monkey Patch" the 'DateTime' class.
Reply

#4
As **@Sam** pointed out, changing offset is not sufficient and would lead to errors. In order to be resistant to DST clock advancements, the conversion should be done in a following way:

d = datetime_to_alter_time_zone
time_zone = 'Alaska'

DateTime.new
.in_time_zone(time_zone)
.change(year: d.year, month: d.month, day: d.day, hour: d.hour, min: d.min, sec: d.sec)
Reply

#5
Using a number offset e.g. '+1000' wont work all year round because of daylight savings. Checkout ActiveSupport::TimeZone. [More info here](

[To see links please register here]

)
Reply

#6
Using Rails 3, you're looking for DateTime.change()

dt = DateTime.now
=> Mon, 20 Dec 2010 18:59:43 +0100
dt = dt.change(:offset => "+0000")
=> Mon, 20 Dec 2010 18:59:43 +0000
Reply

#7
I would use a [Time](

[To see links please register here]

) object instead. So get the current local time then increment it by the UTC offset and convert to UTC, like so:

t = Time.now # or Time.parse(myDateTime.asctime)
t # => Thu Dec 16 21:07:48 -0800 2010
(t + t.utc_offset).utc # => Thu Dec 16 21:07:48 UTC 2010

Although, per Phrogz comment, if you just want to store timestamps in a location independent way then just use the current UTC time:

Time.now.utc

Reply

#8
d = DateTime.now
puts [ d, d.zone ]
#=> 2010-12-17T13:28:29-07:00
#=> -07:00

d2 = d.new_offset(3.0/24)
puts d2, d2.zone
#=> 2010-12-17T23:28:29+03:00
#=> +03:00

**Edit**: This answer doesn't account for the information provided by comment to another answer that the desire is to have the reported 'hours' be the same after the time zone change.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through