Issue #14850 has been updated by nobu (Nobuyoshi Nakada).


sam.saffron (Sam Saffron) wrote:
> Is there any particular reason there is no direct, supported API for setting timezone? 

Because there is no standard in POSIX or similar.
Even the tz database provides data files only, not API.

> ActiveSupport carries http://api.rubyonrails.org/v5.1/classes/ActiveSupport/TimeWithZone.html for this exact reason, it would be nice for core Ruby to support this out-of-the-box

I don't think we adopt that API directly.
Instead we'll provide "rawer" methods, `Time.new` and `Time#getlocal` with a timezone object.
I adjusted the interface for conversion between local and UTC times,  to "tzinfo" and "timezone" gems.

I think methods of `Time.zone` and `Time#in_time_zone` would be able to implement with these features.

# Some issues:

I'm wondering a couple of issues, any suggestions are welcome.

## timezone argument to `Time.at`

`Time.at` already has the fraction second and its unit arguments.
To add another optional argument feels complicated, or a keyword argument?

Or, is `Time.at(time).localtime(tz)` suffice?

## look-up timezone by the name

With "tzinfo" gem, `Time.new(Y, M, D, h, m, s, TZInfo::Timezone.get(zone_name))`, and
with "timeone" gem, `Time.new(Y, M, D, h, m, s, Timezone[zone_name])` are possible but not handy.
`Time.new(Y, M, D, h, m, s, zone_name)` feels preferable, but I don't want to couple them tightly.
Or leave it to gems?

This also affects marshaling.
`Marshal.dump` writes the zone name only right now, and `Marshal.load` loads it as a fixed offset `Time`.
I think it would be better to be converted to a timezone object.


----------------------------------------
Feature #14850: Add official API for setting timezone on Time
https://bugs.ruby-lang.org/issues/14850#change-74437

* Author: sam.saffron (Sam Saffron)
* Status: Closed
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
Only way of setting zone on a Time object appears to be via marshalling and messing with ENV. 

```
>> ENV['TZ'] = 'America/New_York'
>> Time.now.zone
=> "EDT"
>> ENV['TZ'] = 'Europe/London'
>> Time.now.zone
=> "BST"
```

Is there any particular reason there is no direct, supported API for setting timezone? 

ActiveSupport carries http://api.rubyonrails.org/v5.1/classes/ActiveSupport/TimeWithZone.html for this exact reason, it would be nice for core Ruby to support this out-of-the-box



-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>