Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>saha! Sorry, but I have not a 15 points of reputation to give you the level-up)). Anyway thanks for your help. I wrote a TimeUtil helper, an uses it for time correction. This is my current pseudo-code: </p> <pre><code>class RacesController &lt; ApplicationController def create @race = Race.new(params[:race]) @race.correct_time_before_save #can be before_save @race.save end class Race &lt; ActiveRecord::Base def correct_time_before_save date = self.attributes["race_date"] time = self.attributes["race_time"] datetime = Time.local(date.year, date.month, date.day, time.hour, time.min, time.sec) datetime_corrected = TimeUtil::override_offset(datetime) self.race_date = datetime_corrected.to_date self.race_time = datetime_corrected.to_time end # TimeUtil is uses for time correction. It should be very clear, please read description before using. # It's for time correction, when server's and user's time zones are different. # Example: User lives in Madrid where UTC +1 hour, Server had deployed in New York, UTC -5 hours. # When user say: I want this race to be started in 10:00. # Server received this request, and say: Okay man, I can do it! # User expects to race that should be started in 10:00 (UTC +1hour) = 09:00 UTC+0 # Server will start the race in 10:00 (UTC -5 hour) = 15:00 UTC+0 # # This module brings the workaround. All that you need is to make a preprocess for all incoming Time data from users. # Check the methods descriptions for specific info. # # The Time formula is: # UTC + offset = local_time # or # UTC = local_time - offset # module TimeUtil # It returns the UTC+0 DateTime object, that computed from incoming parameter "datetime_arg". # The offset data in "datetime_arg" is ignored - it replaces with Time.zone offset. # Time.zone offset initialized in ApplicationController::set_timezone before-filter method. # def self.override_offset datetime_arg Time.zone.parse(datetime_arg.strftime("%D %T")).utc end </code></pre> <p>ActiveRecord getters adapted to user's time zones too. Time is stored in database (mysql) in "utc+0" format, and we want to get this time in current user's timezone format:</p> <pre><code>class Race &lt; ActiveRecord::Base def race_date date = self.attributes["race_date"] time = self.attributes["race_time"] datetime = Time.utc(date.year, date.month, date.day, time.hour, time.min, time.sec).in_time_zone datetime.to_date end def race_time date = self.attributes["race_date"] time = self.attributes["race_time"] datetime = Time.utc(date.year, date.month, date.day, time.hour, time.min, time.sec).in_time_zone datetime.to_time end </code></pre>
    singulars
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      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