I'm working on unit tests for my Rails app and have run into the following problem.
I have an Event model with a fixture that boils down to:
concert:
name: Wallflowers
start_at: <%= DateTime.new(1999) %>
In my unit test, I have the following assertion:
assert_equal DateTime.new(1999), events(:concert).start_at
The test fails, with the following message:
<Fri, 01 Jan 1999 00:00:00 +0000> expected but was
<Thu, 31 Dec 1998 19:00:00 UTC +00:00>.
I can't figure out why its getting adjusted. The offset of the incorrect time is 5 hours, which is my local offset.
Other info that might be relevant:
The problem only occurs while testing--I don't have any problems in development
environment.rb contains config.time_zone = 'UTC'
The test works if I use Date.new instead of DateTime.new, but I need to use DateTime
What am I missing? Appreciate the help.
Change it to this
concert:
name: Wallflowers
start_at: <%= DateTime.new(1999).to_s(:db) %>
ActiveRecord automatically converts all inserted timestamps to UTC. This explains why your concert time is adjusted to a different timezone. The first step would be to see if the problem is solved by you setting your config.time_zone in environment.rb to your timezone.
If that doesn't solve the issue, read on:
After some testing, I discovered that there seems to be some discrepancy with using DateTime in fixtures compared to the same code in a controller. If I used DateTime.new(1999) in my controller, the inserted column was 1999-01-01 00:00:00. If I used the same call in my fixture, the inserted column was 1999-01-01 10:30:00, which is my timezone. This was regardless of what config.time_zone was set to.
In both cases, changing the timezone correctly changed the ActiveRecord object fetched from the database.
In truth, I don't know which is the correct representation. I do know that the test passed when I changed the fixture to '1999-01-01 00:00' instead of using DateTime.new(1999). If the solution in the first paragraph doesn't work for you, try changing the fixture to the string representation of the date.
Related
I have a following issue - I'm not using config.time_zone, so it should default to my server time zone (if I understand it correctly).
And in my rails console when I do something like
'Oct 12, 2012'.to_datetime
it returns
Fri, 12 Oct 2012 00:00:00 +0000
but when I run
'Oct 12, 2012'.to_date.end_of_day
I get the time zone I actually need:
2012-10-12 23:59:59 -0400
Do you have any ideas why that can happen and how I can get it to work in the same time zone?
I found this link - https://rails.lighthouseapp.com/projects/8994/tickets/864-string-to_datetime-doesn-t-take-into-account-timezone-or-second-fractions, but I thought it should be fixed.
Thanks!
It's likely that Rails 2.3.14 will be the end of the line for the Rails 2.3 series unless a major security update is required. This will likely go unpatched.
Maybe using to_date produces different results than to_datetime, so you may be able to work around this somehow.
Don't forget you can also tweak the time-zone of any DateTime object if required.
I've got a Rails 3.2.6 app running on Ruby 1.8.7. The app is configured to use central European time (i.e. UTC+2) as its time zone, and in my initializers, I monkey-patch Time and DateTime with some custom functionality.
The odd thing is, that in my monkey-patched methods, the Time/DateTime instances act as if they're UTC (but using the time zone-adjusted value), but elsewhere in the app they respect the time zone config.
So, as an example, in config/initializers/monkey_patching.rb I have the following
module MonkeyPatching
def foo
inspect
end
end
class Time
include MonkeyPatching
end
class DateTime
include MonkeyPatching
end
Now, elsewhere in the app (or in the rails console), here's what I get
model.created_at.inspect #=> "Mon, 24 Sep 2012 15:06:34 CEST +02:00" (correct!)
model.created_at.foo #=> "Mon Sep 24 15:06:34 UTC 2012" (all wrong!)
So, calling inspect "directly" on model.created_at gives me the correct, timezone-adjusted result. But calling the patched-in method foo - which also just calls inspect! - treats the time as UTC, even though it isn't.
To add to my confusion, this only happens with model attributes. I.e. in the rails console, I get identical - and correct - results for DateTime.now.inspect and for DateTime.now.foo. But doing the same for a DateTime attribute, give me the strange behavior seen above.
Any idea why this happens (and how to fix it)?
Rails uses ActiveSupport::TimeWithZone for time attributes, not regular Ruby Time. Try to patch ActiveSupport::TimeWithZone too.
class ActiveSupport::TimeWithZone
include MonkeyPatching
end
The simple thing I want is to store a DateTime in UTC in the database and retrieve it in UTC again, but rails seems to assume I stored the DateTime in my local time zone and adds the 8 hours difference (PST). I have a DateTime field in my model. If I send a request (PUT) to update this field in a particular instance of my model, somehow timezones get mixed up for whatever reason:
Sending to rails via PUT: 2012-02-17T03:46:58Z
Returned via subsequent GET: 2012-02-17T11:46:58Z
The time difference is exactly 8 hours and could be explained by my timezone which is PST (-08:00).
I am using Datamapper 1.2 on Rails 3.1.3.
I set my application config timezone to UTC explicitly.
I also tried to use dm-zone-types. Did not change anything for me.
A git-bisect on my repo revealed, that this misbehavior was introduced as I switched the database to postgres from the original sqlite. This commit only changed the database.yml and the gemfile. Nothing else.
Any ideas?
I have found a hacky solution myself:
Set the servers timezone in the environment variable TZ to 'UTC'. In order to persist this configuration option, I decided to put the environment variable setting in config/boot.rb:
ENV['TZ'] = "UTC"
I still feel dirty and this solution still gives me the willies. So any better/cleaner solution is highly appreciated!
I have some code that seems to execute improperly when it is serialized and run with delayed_job. To get the necessaries out of the way, I am running Ubuntu 11.04, Ruby 1.8.7 with Rails 3.0.4.
I have a datetime field in one of my tables that obviously stores the date and time of a particular event. In my RoR application, I use this field through the application calling strftime on it to format it in different ways. The output of this formatting is correct on the web page.
I am also using delayed_job to put this same field into an email that is sent out (triggered via some action). When the email arrives, it appears that it has somehow been formatted as GMT. For example, if the date time was supposed to be 11-12-2011 15:30:00 (3:30 PM) in the database, the email would read 11-12-2011 22:30:00 (10:30 PM).
I looked in the database and found somethings that are interesting:
The datetime in the database is 2011-11-12 22:30:00
The app, when displayed via the web, formats the data properly as 11-12-2011, 3:30 PM
The email, formats the data properly as 11-12-2011, 10:30 PM
When I run a small ruby file that simply prints the date and time
p Time.now
I get this the correct output (local, non GMT time)
Mon Aug 15 10:15:27 -0600 2011
When I look at what is in the serialized yaml for the delayed_jobs table, I can see that the date field is formatted as
2011-11-12 22:30:00 Z
In my application.rb, I have
config.time_zone = 'Mountain Time (US & Canada)'
and in my environment.rb, I have
timezone = "Mountain Time (US & Canada)"
Can any one help me figure out what is going on here? I am relatively new to RoR, so this may be a super obvious, easy question. If you need any more debug information, please let me know and I can post it up. Similarly, if my question is unclear, I can try to clar
In my case, I solved explicitly specifying the method in_time_zone in the view
example: #my_obj.created_at.in_time_zone.
I know it is not elegant, but so far I have not found better solutions.
Try changing the date object to a string before serializing.
You may want to set the timezone in the database. If it's MySQL, see: http://dev.mysql.com/doc/refman/5.5/en/time-zone-support.html
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