Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to represent variable offset time zones in REST XML web service?
    primarykey
    data
    text
    <p>I've come across an interesting problem regarding Rails REST XML and the way datetime values are serialized. Namely, datetime values are represented as constant offset values like so:</p> <pre><code>&lt;model&gt; &lt;date type="datetime"&gt;2010-07-21T16:38:03-05:00&lt;/date&gt; &lt;/model&gt; </code></pre> <p>Note the datetime value plus a constant offset of -0500. When consuming information from this REST web service, such an offset works perfectly. But suppose I wanted to POST information to this web service, and I wanted Rails to account for daylight savings much like it would do when a user submits a web form. I've found I can do this:</p> <pre><code>&lt;model&gt; &lt;date type="datetime"&gt;2010-07-21 16:38:03&lt;/date&gt; &lt;/model&gt; </code></pre> <p>and the time will be correctly interpreted as the local user's <a href="http://railscasts.com/episodes/106-time-zones-in-rails-2-1" rel="nofollow">time zone preference</a>. This is, in effect, what is happening with a regular form submission. So far so good.</p> <p>Also suppose that I need the ability to directly specify the time zone in the request. The reasoning is that a specific user might change their time zone preference at any time, but the API requests coming on their behalf are always in a fixed location (and thus a fixed time zone).</p> <p>This seems simple enough, but is complicated by a few factors:</p> <ul> <li>Time zone offsets generally change twice a year (i.e. from -0500 to -0400 and back).</li> <li>Rails always expects constant time zone offsets or the datetime will be interpreted as local time (or the user's time zone preference).</li> <li>Ruby's Time class doesn't understand variable offsets. For example, I can specify a time is Eastern Standard Time (EST) or Eastern Daylight Time (EDT), but <code>Time.parse</code> will fail to properly understand "Eastern Time", or ET. I can't specify one or the other because that will cause the time to be off by an hour half of the year.</li> </ul> <p>I've considered the following solutions:</p> <ol> <li><p>Add a time_zone virtual attribute to the model. I don't like this because it makes very little sense in the context of the model, and the setting of the time zone is prone to issues with parameter assignment (i.e. the model must exhibit different behaviors when time_zone is processed before the date, after the date, and when time_zone is nil).</p></li> <li><p>Use a query string parameter in association with the POST'ed XML data. Many people seem to frown on query strings being used in HTTP POST requests, and even <a href="https://github.com/archiloque/rest-client" rel="nofollow">popular rest clients</a> don't <a href="https://github.com/archiloque/rest-client/blob/master/lib/restclient/request.rb#L65" rel="nofollow">seem to support it</a>.</p></li> </ol> <p>Are there any other possible solutions to this problem? I wish I could specify variable offsets like these but none of them work:</p> <pre><code>&lt;date&gt;2010-07-21 16:38:03 Eastern Time&lt;/date&gt; &lt;date&gt;2010-07-21 16:38:03 Eastern Time (US &amp; Canada)&lt;/date&gt; &lt;date&gt;2010-07-21 16:38:03 ET&lt;/date&gt; &lt;date&gt;2010-07-21 16:38:03 America/New York&lt;/date&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload