---
title: "Ruby 2.4 DateTime#to_time & Time#to_time keeps info"
description:
  "DateTime#to_time and Time#to_time preserves receiver's timezone offset info
  in Ruby 2.4"
canonical_url: "https://www.bigbinary.com/blog/to-time-preserves-time-zone-info-in-ruby-2-4"
markdown_url: "https://www.bigbinary.com/blog/to-time-preserves-time-zone-info-in-ruby-2-4.md"
---

# Ruby 2.4 DateTime#to_time & Time#to_time keeps info

DateTime#to_time and Time#to_time preserves receiver's timezone offset info in
Ruby 2.4

- Author: Sushant Mittal
- Published: September 19, 2017
- Categories: Ruby 2.4, Ruby

In Ruby, `DateTime#to_time` and `Time#to_time` methods can be used to return a
Time object.

In Ruby 2.3, these methods convert time into system timezone offset instead of
preserving timezone offset of the receiver.

### Ruby 2.3

```ruby

> datetime = DateTime.strptime('2017-05-16 10:15:30 +09:00', '%Y-%m-%d %H:%M:%S %Z')
 #=> #<DateTime: 2017-05-16T10:15:30+09:00 ((2457890j,4530s,0n),+32400s,2299161j)>
> datetime.to_time
 #=> 2017-05-16 06:45:30 +0530

> time = Time.new(2017, 5, 16, 10, 15, 30, '+09:00')
 #=> 2017-05-16 10:15:30 +0900
> time.to_time
 #=> 2017-05-16 06:45:30 +0530

```

As you can see, `DateTime#to_time` and `Time#to_time` methods return time in
system timezone offset `+0530`.

Ruby 2.4 fixed
[DateTime#to_time](https://github.com/ruby/ruby/commit/5f11a6eb6cca740b08384d1e4a68df643d98398c)
and
[Time#to_time](https://github.com/ruby/ruby/commit/456523e2ede3073767fd8cb73cc4b159c3608890).

Now, `DateTime#to_time` and `Time#to_time` preserve receiver's timezone offset
info.

### Ruby 2.4

```ruby

> datetime = DateTime.strptime('2017-05-16 10:15:30 +09:00', '%Y-%m-%d %H:%M:%S %Z')
 #=> #<DateTime: 2017-05-16T10:15:30+09:00 ((2457890j,4530s,0n),+32400s,2299161j)>
> datetime.to_time
 #=> 2017-05-16 10:15:30 +0900

> time = Time.new(2017, 5, 16, 10, 15, 30, '+09:00')
 #=> 2017-05-16 10:15:30 +0900
> time.to_time
 #=> 2017-05-16 10:15:30 +0900

```

Since this is a breaking change for Rails application upgrading to ruby 2.4,
Rails 4.2.8 built a compatibility layer by adding a
[config option](https://github.com/rails/rails/commit/c9c5788a527b70d7f983e2b4b47e3afd863d9f48).
`ActiveSupport.to_time_preserves_timezone` was added to control how `to_time`
handles timezone offsets.

Here is an example of how application behaves when `to_time_preserves_timezone`
is set to `false`.

```ruby
> ActiveSupport.to_time_preserves_timezone = false

> datetime = DateTime.strptime('2017-05-16 10:15:30 +09:00', '%Y-%m-%d %H:%M:%S %Z')
 #=> Tue, 16 May 2017 10:15:30 +0900
> datetime.to_time
 #=> 2017-05-16 06:45:30 +0530

> time = Time.new(2017, 5, 16, 10, 15, 30, '+09:00')
 #=> 2017-05-16 10:15:30 +0900
> time.to_time
 #=> 2017-05-16 06:45:30 +0530
```

Here is an example of how application behaves when `to_time_preserves_timezone`
is set to `true`.

```ruby
> ActiveSupport.to_time_preserves_timezone = true

> datetime = DateTime.strptime('2017-05-16 10:15:30 +09:00', '%Y-%m-%d %H:%M:%S %Z')
 #=> Tue, 16 May 2017 10:15:30 +0900
> datetime.to_time
 #=> 2017-05-16 10:15:30 +0900

> time = Time.new(2017, 5, 16, 10, 15, 30, '+09:00')
 #=> 2017-05-16 10:15:30 +0900
> time.to_time
 #=> 2017-05-16 10:15:30 +0900
```

## Links

- [Human page](https://www.bigbinary.com/blog/to-time-preserves-time-zone-info-in-ruby-2-4)
