views:

33

answers:

3

Hi,

I'm having trouble working with Time.now. For some reason when I save an object to the DB it saves it for the year 2000. I'm trying to make a comparison to the object I saved in the DB to Time.now to check if it is greater, but it always returns false because of the year 2000. Does anyone know of a way I can work around this?

I just need to check to make sure 10 minutes has passed since I created a time object compared to Time.now

A: 

What is your system date? from console:

date
Tombart
what command do I type?
Brian
what OS are you on?
ormuriauga
I am using windows 7
Brian
then you can just check the system clock (I think it is in the bottom right corner)
ormuriauga
A: 

created_at < 10.minutes.ago should work

ormuriauga
that doesn't seem to work.. If I just create the object and type 1 minute ago after 2 it still says true
Brian
try appending to_date and see it that helps `created_at < 10.minutes.ago.to_date`
ormuriauga
Is your database server on the same machine as the running code?
ormuriauga
no its not.. just testing out the app on my desktop
Brian
so there is probably a mismatch on your date settings. try to fix that
ormuriauga
what time do you get if you print `created_at` from a newly created object? and what time do you have on your machine?
ormuriauga
created_at is Fri, 10 Sep 2010 06:26:22 UTC +00:00
Brian
my machine is todays date at 3:26
Brian
this seems to work.. u.online.created_at > u.online.create_at - 10.minutes
Brian
but that will always be true.
ormuriauga
lol yeah I just thought of that
Brian
your problem, I think, is that your database is in another timezone. you need to compensate for that `u.online.created_at > 3.hours.from_now - 10.minutes` or something similar. experiment with plus and minus and ago/from_now. I have problems thinking about time.
ormuriauga
actually, check your config (in rails3 config/application.rb), there you can set the timezone of your app which will automatically compensate for you.
ormuriauga
+2  A: 

It sounds like your database column is of type time rather than datetime or timestamp. This will only store the time and when it is converted to a Time instance in ruby (which does support day, month, year, etc) the default values are used for day, month, year which is why you're seeing the year 2000.

You probably need to update your database column to be datetime or timestamp if that is the problem as it sounds like you'll want the day, month, year parts of the time anyway. In which case you're comparisons will work.

Shadwell
yeah, this sounds plausible.
ormuriauga
yes, the column is of the type time. I've been trying the created_at part and it still doesn't work..
Brian
Not sure what you mean. I didn't mention created_at.
Shadwell
yup, I changed it to datetime and it seems to work with u.online.last_seen > 10.minutes.ago
Brian
thank you to both of you for the help
Brian