I get the current month number using the following simple code in a Controller:
#month = DateTime.now.month
In my application_controller.rb:
around_filter :use_time_zone
def use_time_zone(&block)
Time.use_zone('Pacific Time (US & Canada)', &block)
end
And it seems to work great. Though I did see some kind of Rails 4 deprecation warning somewhere, I think in my production logs. Maybe that's the cause of my issue?
What happens is when I deploy to Heroku, the #month I'm guessing is using UTC time instead of Pacific. It's 5:30pm March 31 right now and #month is 4 (April). But only in production on Heroku, locally (using SQLite3 if that matters), #month is correctly 3 (March).
How do I get the correct month for a particular timezone on Heroku?
Use DateTime.now.in_time_zone('Pacific Time (US & Canada)').month or DateTime.now.in_time_zone.month. In the latter case the time zone returned by Time.zone is used.
Unlike Time.zone.now and DateTime.current that use the Rails time zone, Time.now and DateTime.now, ruby classes, use the system's time zone by default.
Related
In our rails 3.2 application, we have configured,
config.time_zone = 'London'
config.active_record.default_timezone = :local
And in postgresql also configured timezone as "Europe/London".
For the past one week, our application datetime field is not working properly with timezone.
For an example, If we create an reminder start_date at 2015-08-18 10AM. it creates `2015-08-18 10:00:00 in postgres database.
when displaying in template,
reminder start date: 2015-08-18 10AM (2015-08-18 10:00:00 +0100 )
BUT,(not always) now frequently its showing UTC time.
reminder start date: 2015-08-18 9AM (2015-08-18 09:00:00 UTC )
it could not be reproduced in development.
If we restart the unicorn server then it is not occurring for 4 hours.
anyone faced this kind of problem?
I fixed this issue, by adding around filter in ApplicationController.
around_filter :use_time_zone
private
def use_time_zone(&block)
Time.use_zone('London', &block)
end
So, whenever the default timezone changed to UTC, it will override and set as BST.
How does Rails know my time zone? I don't have it set in application.rb:
# config.time_zone = 'Central Time (US & Canada)'
I searched the whole app for time_zone and that is the only instance of it. I don't see any environment variables for time zone set either. I'm on Windows.
C:\Users\Chloe\workspace\MyBestBody>rails runner "p Time.now"
DL is deprecated, please use Fiddle
2015-06-12 23:38:33 -0400
It prints UTC time when deployed to Heroku.
C:\Users\Chloe\workspace\MyBestBody>heroku run rails console
Running `rails console` attached to terminal... up, run.1949
Loading production environment (Rails 4.2.1)
irb(main):001:0> Time.new
=> 2015-06-13 03:28:34 +0000
Rails 4.2.1
From gettimeofday(), or so the ruby MRI source would have me believe:
int __cdecl
gettimeofday(struct timeval *tv, struct timezone *tz)
{
FILETIME ft;
GetSystemTimeAsFileTime(&ft);
filetime_to_timeval(&ft, tv);
return 0;
}
Per comment, this is a C function, called by the ruby date routines under the hood. You do not call the method yourself, rather you call the ruby methods you are calling.
Your question involves two separate things.
The first is what ruby thinks the time zone is. This is the zone used when you use things likeTime.now or Time.mktime. Ruby uses the C level APIs provided by the operating system to get this information (the TZ environment variable can override this on unixy operating systems).
The second is the time zone your users will see when they use your app. This is frequently not the same thing. Even if your users were all in the same timezone it's a good idea to have your servers use UTC because it is free from things like daylight savings time.
For this reason rails has its own timezone system and the setting you found controls the default value of that timezone. Sometimes you might overwrite this on a per user basis. Rails always records information in UTC, but it will be displayed in views using the value of Time.zone (And input from forms is interpreted in that zone). You can see what the current time in this zone is by doing
Time.zone.now
I'm trying to get a time of users action.
However, it seems return default UTC time even I changed timezone as seoul(UTC +9).
Here are what I did.
in
config/application.rb
put codes as follow.(change default time_zone to seoul)
config.time_zone = 'Seoul'
config.active_record.default_timezone = :local
then, run
rails c
Time.now
I expect time as UTC +9, but it shows UTC +0
Saved time of users action in data is also shown UTC +0.
How can I get time as UTC +9?, is there anything that I need to set?
Try
config.time_zone = 'Korea Time Zone (UTC+09:00)'
I am working with a legacy Rails 2.3.11 incident reporting application and I'm having some issues with the time zone. For some reason when a user selects a date/time from a datepicker it seems to store the value fine (I check the database and the value is correct), but when the value is retrieved from the database it is 5 hours in the future. Does anyone know why this might be?
I have checked the time on both the app server and the database server and they seem to be correct: Mon Jun 10 07:52:18 CDT 2013
Here are some values that might be important:
environment.rb
config.time_zone = 'Central Time (US & Canada)'
config.active_record.default_timezone = :local
config.active_record.time_zone_aware_attributes = false
incident.rb (model class)
def self.format_time(time)
if (time)
time.strftime("%Y-%m-%d %I:%M:%S %p")
else
return "Not Available"
end
end
def self.time_format
"%Y-%m-%d %I:%M:%S %p"
end
Edit: Also note that I cannot replicate this issue on my local development machine. It seems to only be an issue when the app is pushed to the server.
I've got this time stored as a string:
2010-07-25 04:16:25
This is the GMT time for some action I took.
Since I live in the Jerusalem time zone, I would like to show it at Jerusalem time, i.e. 07:16 in the morning, not the GMT time of 04:16:25 which is 3 hours before.
How do I properly convert it programmatically with Ruby on Rails? I seem to get lost with the multitude of timezone functions and considerations I need to take when serving users from different locations.
I tried:
Time.parse("2010-07-25 04:16:25")
and it gave me:
"Sun Jul 25 04:16:25 +0300 2010".
I suppose the "+0300" is the difference to where I'm at?
Some light on this, or even a link to a good article that doesn't assume you know much, would help.
You can define your timezone in environment.rb file (if you are using Rails 2.3.*) or application.rb (if you're using Rails 3).
Just look for section about time zones and everything is explained in comment. It will say something like this (this is from Rails 3):
# 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)'
Just uncomment that last line and you should be fine.
To configure it easily related to your system local zone you can add this in your application.rb
config.time_zone = Time.now.zone
and you can use something like this to get the localtime
Post.created_at.localtime