Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You will need to use <code>Time.zone.now</code> or <code>Time.now.in_time_zone</code> exclusively in your application in order to force Rails to use the timezone you've set in your config file. To default the <code>date_select</code> object to the time in UTC, simply initialize the new record with <code>new_record.date = Time.zone.now</code>, perhaps in your controller's <code>new</code> action.</p> <p><strong>Background</strong></p> <p>By default, Rails will convert all datetime fields to UTC before storing them in the database. When Rails reads a record back out, it will convert that UTC timestamp to be in the zone specified in your config file. <code>Time.now</code> will return a Time object in the timezone that your web server is configured to use. <code>Time.zone.now</code> will convert the system time to be in the timezone in your config file.</p> <p><strong>Example</strong></p> <p>Here's a practical example. My computer (aka the server) is configured in EST. I have a Rails app configured to use PST. The database stores everything in UTC</p> <pre><code>1.9.3p286 &gt; Time.now =&gt; 2013-11-12 13:33:40 -0500 1.9.3p286 &gt; Time.now.zone =&gt; "EST" 1.9.3p286 &gt; Time.now.in_time_zone =&gt; Tue, 12 Nov 2013 10:33:40 PST -08:00 1.9.3p286 &gt; Time.now.in_time_zone.zone =&gt; "PST" 1.9.3p286 &gt; Time.zone.now =&gt; Tue, 12 Nov 2013 10:33:40 PST -08:00 1.9.3p286 &gt; Time.zone.now.zone =&gt; "PST" 1.9.3p286 &gt; Time.parse("12:34:56") =&gt; 2013-11-12 12:34:56 -0500 1.9.3p286 &gt; Time.parse("12:34:56").zone =&gt; "EST" 1.9.3p286 &gt; Time.parse("12:34:56").in_time_zone =&gt; Tue, 12 Nov 2013 09:34:56 PST -08:00 1.9.3p286 &gt; Time.parse("12:34:56").in_time_zone.zone =&gt; "PST" 1.9.3p286 &gt; Time.zone.parse("12:34:56") =&gt; Tue, 12 Nov 2013 12:34:56 PST -08:00 1.9.3p286 &gt; Time.zone.parse("12:34:56").zone =&gt; "PST" 1.9.3p286 &gt; Time.parse("12:34:56 UTC") =&gt; 2013-11-12 12:34:56 UTC 1.9.3p286 &gt; Time.parse("12:34:56 UTC").zone =&gt; "UTC" 1.9.3p286 &gt; Time.parse("12:34:56 UTC").in_time_zone =&gt; Tue, 12 Nov 2013 04:34:56 PST -08:00 1.9.3p286 &gt; Time.parse("12:34:56 UTC").in_time_zone.zone =&gt; "PST" 1.9.3p286 &gt; Time.zone.parse("12:34:56 UTC") # Note that the time is parsed as UST and then converted to PST =&gt; Tue, 12 Nov 2013 04:34:56 PST -08:00 1.9.3p286 &gt; t = TripTicket.first =&gt; #&lt;TripTicket ...&gt; 1.9.3p286 &gt; t.appointment_time # Reading a datetime field from the database =&gt; Thu, 21 May 2009 10:29:11 PDT -07:00 1.9.3p286 &gt; t.appointment_time.in_time_zone # No additional conversion because rails already converted it to PST =&gt; Thu, 21 May 2009 10:29:11 PDT -07:00 1.9.3p286 &gt; t.appointment_time_before_type_cast # The raw value in the database, stored as UTC =&gt; "2009-05-21 17:29:11.614345" 1.9.3p286 &gt; t.appointment_time.utc =&gt; 2009-05-21 17:29:11 UTC 1.9.3p286 &gt; t.appointment_time = Time.parse("12:34:56") # Set the time in the server local timezone, i.e. EST =&gt; 2013-11-12 12:34:56 -0500 1.9.3p286 &gt; t.appointment_time # Reading the time. Note it's converted to PST already =&gt; Tue, 12 Nov 2013 09:34:56 PST -08:00 1.9.3p286 &gt; t.appointment_time.zone =&gt; "PST" 1.9.3p286 &gt; t.appointment_time.utc =&gt; 2013-11-12 17:34:56 UTC 1.9.3p286 &gt; t.save =&gt; true 1.9.3p286 &gt; t.reload =&gt; #&lt;TripTicket ...&gt; 1.9.3p286 &gt; t.appointment_time_before_type_cast =&gt; "2013-11-12 17:34:56" </code></pre>
 

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