Rails time zone not overrides as well - ruby-on-rails

I have a noisy problems with UTC on my Rails project.
class ApplicationController < ActionController::Base
before_filter :set_timezone
def set_timezone
Time.zone = current_user.time_zone if current_user
end
Cool. I overrided the time zone.
And now, server' time zone is +3. User's time zone is +5. I hope that any requests to Time should get the User's time zone, but this code returns not expected values:
render :text => Time.zone.to_s + "<br/>" +
Time.now.to_s + "<br/>" +
Time.now.in_time_zone.to_s
RESULT:
(GMT+05:00) Tashkent
Thu Oct 20 19:41:11 +0300 2011
2011-10-20 21:41:11 +0500
Where does from +0300 offset comes??

To get the current time in the currently set timezone you can use
Time.zone.now
Your server' time zone is +3 and
Time.now.to_s is returning this

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:
class RacesController < ApplicationController
def create
#race = Race.new(params[:race])
#race.correct_time_before_save #can be before_save
#race.save
end
class Race < 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
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:
class Race < 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

Related

How to override a rails 4 module from the app

In my application I need to record the created_at based on the current user's time as I need to apply different rules and conditions based on the time user created a record.
This is what I have done:
in the application.rb:
config.time_zone = 'Melbourne'
config.active_record.default_timezone = :local
However when user from different timezone create a record or update a record it saves as Melbourne timezone. I thought default timezone would be set to where user create the record.
Basically I need to be able to do this for the created_at and updated_at:
Time.zone.now
So I did the following in my initialiser folder:
I have added timestamp.rb with the following code:
module ActiveRecord
module Timestamp
def self.included(base) #:nodoc:
base.alias_method_chain :create, :timestamps
base.alias_method_chain :update, :timestamps
base.class_inheritable_accessor :record_timestamps, :instance_writer => false
base.record_timestamps = true
end
private
def create_with_timestamps #:nodoc:
if record_timestamps
t = Time.zone.now
write_attribute('created_at', t) if respond_to?(:created_at) && created_at.nil?
write_attribute('created_on', t) if respond_to?(:created_on) && created_on.nil?
write_attribute('updated_at', t) if respond_to?(:updated_at) && updated_at.nil?
write_attribute('updated_on', t) if respond_to?(:updated_on) && updated_on.nil?
end
create_without_timestamps
end
def update_with_timestamps(*args) #:nodoc:
t = Time.zone.now
write_attribute('updated_at', t) if respond_to?(:updated_at)
write_attribute('updated_on', t) if respond_to?(:updated_on)
end
end
end
I restarted the server however nothing has changed, still I get the wrong timezone recordings. If somehow I force Rails to use my Timestamp override this problem would be solved.
This is on my Local machine at about 10:15 AM
Here is an example of created_at after create is triggered:
created_at: "2014-12-09 22:15:09"
Now when I run the Time.zone.now in the console I get
Wed, 10 Dec 2014 10:15:09 EST +11:00
FYI: (Not sure this is matter.)
Database is located on Amazon RDS which is in different timezone too and it is Postgresql.

Rails datetime_select posting my current time in UTC

I'm trying to compare what the user selects for a start date and end date to the current time to prevent the user from selecting a time in the past. It works except you have to pick a time, in my case, 4 hours ahead in order for it to pass the validation.
View:
datetime_select(:start_date, ampm: true)
Controller:
if self.start_date < DateTime.now || self.end_date < DateTime.now
errors.add(:date, 'can not be in the past.')
end
self.start_date is returning my current time but in utc which is wrong. DateTime.now is returning my current time but with an offset of -0400 which is correct.
Example:
My current time is 2013-10-03 09:00:00.000000000 -04:00
self.start_date is 2013-10-03 09:00:00.000000000 Z
DateTime.now is 2013-10-03 09:00:00.000000000 -04:00
Why is this happening and what would be the best way to fix it?
you can do something like this
around_filter :set_time_zone
private
def set_time_zone
old_time_zone = Time.zone
Time.zone = current_user.time_zone if logged_in?
yield
ensure
Time.zone = old_time_zone
end
you can also do this
adding following to application.rb works
config.time_zone = 'Eastern Time (US & Canada)'
config.active_record.default_timezone = 'Eastern Time (US & Canada)'
I ended up fixing it by converting the start_date to a string and back to time. It was weird that I needed :local, as the documentation on to_time says it is the default, but it only works when it is present.
def not_past_date
current_time = DateTime.now
start_date_selected = self.start_date.to_s.to_time(:local)
end_date_selected = self.start_date.to_s.to_time(:local)
if start_date_selected < current_time || end_date_selected < current_time
errors.add(:date, 'can not be in the past.')
end
end

Rails/Postgres query rows grouped by day with time zone

I'm trying to display a count of impressions per day for the last 30 days in the specific users time zone. The trouble is that depending on the time zone, the counts are not always the same, and I'm having trouble reflecting that in a query.
For example, take two impressions that happen at 11:00pm in CDT (-5) on day one, and one impression that happens at 1:00am CDT. If you query using UTC (+0) you'll get all 3 impressions occurring on day two, instead of two the first day and one the second. Both CDT times land on the day two in UTC.
This is what I'm doing now, I know I must be missing something simple here:
start = 30.days.ago
finish = Time.now
# if the users time zone offset is less than 0 we need to make sure
# that we make it all the way to the newest data
if Time.now.in_time_zone(current_user.timezone) < 0
start += 1.day
finish += 1.day
end
(start.to_date...finish.to_date).map do |date|
# get the start of the day in the user's timezone in utc so we can properly
# query the database
day = date.to_time.in_time_zone(current_user.timezone).beginning_of_day.utc
[ (day.to_i * 1000), Impression.total_on(day) ]
end
Impressions model:
class Impression < ActiveRecord::Base
def self.total_on(day)
count(conditions: [ "created_at >= ? AND created_at < ?", day, day + 24.hours ])
end
end
I've been looking at other posts and it seems like I can let the database handle a lot of the heavy lifting for me, but I wasn't successful with using anything like AT TIME ZONE or INTERVAL.
What I have no seems really dirty, I know I must missing something obvious. Any help is appreciated.
Ok, with a little help from this awesome article I think I've figured it out. My problem stemmed from not knowing the difference between the system Ruby time methods and the time zone aware Rails methods. Once I set the correct time zone for the user using an around_filter like this I was able to use the built in Rails methods to simplify the code quite a bit:
# app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
around_filter :set_time_zone
def set_time_zone
if logged_in?
Time.use_zone(current_user.time_zone) { yield }
else
yield
end
end
end
# app/controllers/charts_controller.rb
start = 30.days.ago
finish = Time.current
(start.to_date...finish.to_date).map do |date|
# Rails method that uses Time.zone set in application_controller.rb
# It's then converted to the proper time in utc
time = date.beginning_of_day.utc
[ (time.to_i * 1000), Impression.total_on(time) ]
end
# app/models/impression.rb
class Impression < ActiveRecord::Base
def self.total_on(time)
# time.tomorrow returns the time 24 hours after the instance time. so it stays UTC
count(conditions: [ "created_at >= ? AND created_at < ?", time, time.tomorrow ])
end
end
There might be some more that I can do, but I'm feeling much better about this now.
Presuming the around_filter correctly works and sets the Time.zone in the block, you should be able to refactor your query into this:
class Impression < ActiveRecord::Base
def self.days_ago(n, zone = Time.zone)
Impression.where("created_at >= ?", n.days.ago.in_time_zone(zone))
end
end

Invalid argument to TimeZone?

I'm trying to make my users Time zone be the current Time zone of my application so everything they interact will be by it. I run into an ArgumentError for my method inside of my ApplicationController though.
application_controller.rb
before_filter :set_user_time_zone
private
def set_user_time_zone
if signed_in?
Time.zone = Time.now.in_time_zone(current_user.time_zone)
end
end
Notes: current_user is a Devise Helper and my User model as a :time_zone column.
Than the error:
invalid argument to TimeZone[]: Mon, 20 Aug 2012 13:16:20 JST +09:00
I don't know where to go from here. Any ideas on how to correct this?
Thanks.
UPDATE
class Price
attr_accessible :date
def self.today
where(:date => Date.today)
end
end
If my method does:
def set_user_time_zone
if signed_in?
Time.zone = current_user.time_zone
end
end
The problem I have my times are like this:
Time.now = US EAST- 2012-08-22 21:17:03 -0400
Time.zone = TOKYO - (GMT+09:00) Tokyo
Time.zone.now 2012-08-23 10:17:03 +0900
Which means all of my Date methods go by
Time.now = US EAST- 2012-08-22 21:17:03 -0400
when it should be
Time.zone.now 2012-08-23 10:17:03 +0900
How can I get it to the latter?
Time.now.in_time_zone(current_user.time_zone) returns instance of TimeWithZone class, but
Time#zone= expects to get something that can be converted to TimeZone.
Assuming you store TimeZone identifiers in your :time_zone column (“Eastern Time (US & Canada)”, "Hawaii", etc.) you can simply do
Time.zone = current_user.time_zone
Time#zone= method accepts only these params:
A Rails TimeZone object.
An identifier for a Rails TimeZone object (e.g., “Eastern Time (US &
Canada)”, -5.hours).
A TZInfo::Timezone object.
An identifier for a TZInfo::Timezone object (e.g.,
“America/New_York”).
So you should pass something from this list

Get objects size between relative dates

I have many User objects with created_at attribute e.g.
I get objects with #users = User.all
I want get the count of User objects with various ages from creation with
#users.size
for these date ranges:
yesterday
last week
last month
last year.
How can I do it?
I use mongoid.
You can write scopes for this:
class User
include Mongoid::Document
...
## Scopes for calculating relative users
scope :created_yesterday, lambda { where(:created_at.gte => (Time.now - 1.day)) }
scope :created_last_week, lambda { where(:created_at.gte => (Time.now - 1.week)) }
scope :created_last_month, lambda { where(:created_at.gte => (Time.now - 1.month)) }
scope :created_last_year, lambda { where(:created_at.gte => (Time.now - 1.year)) }
...
end
The reason we need to use a lambda here is that it delays the evaluation of the Time.now argument to when the scope is actually invoked. Without the lambda the time that would be used in the query logic would be the time that the class was first evaluated, not the scope itself.
Now we can get the counts, by simply calling:
User.created_yesterday.count
User.created_last_week.count
...
If you want the objects:
#users_created_yesterday = User.created_yesterday
You can use #users_created_yesterday in the views.
Update
Well with yesterday, if you mean time between, yesterday beginning of day 0:00 and yesterday end of day 23:59, you will have to take Time zones into consideration.
Fo example, in your application.rb, if you have written:
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
config.time_zone = 'Central Time (US & Canada)'
If you have use this, all the times fetched by activerecord queries will be converted to this time zone, Central Time (US & Canada). In the database, all the times will be stored in UTC and will be converted to the time zone on fetching from the database. If you have commented out this line, default will be UTC. You can get all the time zones using the rake task rake time:zones:all or only the US timezones using rake time:zones:us.
If you want the time zone specified in the application.rb, you need to use Time.zone.now, in the following code. If you use Time.now in the following code, you will get the time zone according to the time zone of your server machine.
class User
include Mongoid::Document
...
scope :created_between, lambda { |start_time, end_time| where(:created_at => (start_time...end_time)) }
class << self
## Class methods for calculating relative users
def created_yesterday
yesterday = Time.zone.now - 1.day
created_between(yesterday.beginning_of_day, yesterday.end_of_day)
end
def created_last_week
start_time = (Time.zone.now - 1.week).beginning_of_day
end_time = Time.zone.now
created_between(start_time, end_time)
end
def created_last_month
start_time = (Time.zone.now - 1.month).beginning_of_day
end_time = Time.zone.now
created_between(start_time, end_time)
end
def created_last_year
start_time = (Time.zone.now - 1.year).beginning_of_day
end_time = Time.zone.now
created_between(start_time, end_time)
end
end
..
..
end
So, you can write class methods and calculate start time and end time, supply it to the scope created_between, and you will be able to call them like User.created_yesterday, like we called before.
Credits: EdgeRails. Since it is Mongoid, I had to look up at Mongoid docs

Resources