Weird date/time incompatibility

I’ve uploaded my development project to my production server, and am
experiencing an odd effect.

All database find methods in which the date is passed as a param are
coming up nil – it seems that there’s an incompatibility in the date/
time format in there somewhere.

E.g. in the view, obtaining @date from the controller to the index

labs = Lab.all( :conditions => [ “patient_id = ?”, id ], :order =>
“drawn DESC” )
@dates = []
labs.each { |lab| @dates << lab.drawn }

Then in the index view,
<% for date in @dates %>…
<%= link_to … :date => date… %>

Then back in the show method in the controller:
@date = params[ :date ]
then
LabData.first … :conditions => … ‘labs.drawn’ => date
isn’t finding it on the production server, but it was on the
development machine.

I’ve tried fiddling with the time zone settings on the production
server, no luck.

Has anyone encountered such a problem? Any ideas how to fix it?

Many TIA,
Craig

On 2 April 2010 05:38, Dudebot [email protected] wrote:

“drawn DESC” )
@dates = []
labs.each { |lab| @dates << lab.drawn }

Then in the index view,
<% for date in @dates %>…
<%= link_to … :date => date… %>

Have you checked the html of the page in the browser to check that the
links are correct? Compare the html with that seen in the development
environment.

Then back in the show method in the controller:
@date = params[ :date ]

Have you checked the log to see whether the params are the same in the
development and production environment. You may have to change the
logging setup in config/environments/production.rb to see this.

then
LabData.first … :conditions => … ‘labs.drawn’ => date

Again check the log to see what sql is being used.
Have you checked in the db that the records are there?

Hopefully one of the above will give you some clue.

Colin

On Apr 2, 2:22 am, Colin L. [email protected] wrote:

Checking the html, the database entries with phpMyAdmin, and the log
file were where I went first–I should have written that. It’s all
there, just like the development environment. The log file was what
pointed me to the database call, and I mocked it up with a script/
console call that mimicked the one in my model. That’s when I
discovered that it looks like the date format isn’t being passed
correctly in the params hash, which I find really weird. It is, after
all, coming from the database originally.

I think it may be how I pass the dates to the index view, as an array,
is where the formatting may change

labs = Lab.all( :conditions => [ “patient_id = ?”, id ], :order =>
“drawn DESC” )
@dates = []
labs.each { |lab| @dates << lab.drawn }

But I could be wrong about that.

Has anyone ever experienced a problem with date formatting like this?
And if so, how did you fix it?

Another question for you gurus is, when I put a raise —.to_yaml in
the production server code, those only appear in the logs, the
“errors” are prettily concealed on the production views. Any way to
temporarily shut that behavior off while I’m debugging the production
code?

Again, many TIA,
Craig

On 2 April 2010 12:16, Dudebot [email protected] wrote:

all, coming from the database originally.
If the html is correct then it must be getting it out of the db ok so
it is not a problem there. Yet you say the params are getting passed
incorrectly from that html? is that in the log file you see that or
after you get into the action? So what params are you seeing there in
development and what in production?

I think it may be how I pass the dates to the index view, as an array,
is where the formatting may change

You say the html is correct in the index view so it cannot be an issue
passing the dates to the index view, or you would see the wrong dates
there.

Colin

On Apr 2, 6:30 am, Colin L. [email protected] wrote:

You say the html is correct in the index view so it cannot be an issue
passing the dates to the index view, or you would see the wrong dates
there.

Thanks for helping with this. What I see in the development html is ?
date=2010-03-31+18%3A19%3A00+UTC
and what I see in the production html is ?date=Thu+Apr
+01+23%3A02%3A00±0500+2010
and I’m wondering if somehow it’s choking on the date format on the
production server…

On Apr 2, 3:12 pm, Colin L. [email protected] wrote:

Am I right in assuming that is what is contained in the <a href link
in the html? If so then the first thing is that they are in different
timezones. Can you confirm that you are definitely seeing the above
dates with the same version of the code. You said you had been

Do you want to see a local time there or UTC?

Thanks again–the difference was that I had this in the development
config/enviroment.rb
config.time_zone = ‘UTC’
But not in the production file. I uncommented it in the production
environment.rb just now, and lo and behold, that part is now working.

Except–I now remember I had in commented out of the production server
as another part of the code which displays the “created at” field for
a record was giving the wrong time. E.g. if I run date in a terminal

I get Fri Apr 2 17:13:05 CDT 2010

(Which is my time)
But for the created at for this record in the database I’m getting

2010-04-02 22:13:33

I don’t seem to have that problem on my development machine (MacOS
laptop). The production server is a Redhat Linux machine.

Any idea how to “sync” created at and the system time on the
production server?

Again, many TIA,
Craig

On Fri, 2010-04-02 at 15:28 -0700, Dudebot wrote:

I get Fri Apr 2 17:13:05 CDT 2010
production server?

Again, many TIA,
Craig


they actually are in sync

Fri Apr 2 17:13:05 CDT 2010 == 2010-04-02 22:13:05 (UTC) as CDT is UTC

  • 5 hours

Craig


This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.

On Apr 2, 6:05 pm, Craig W. [email protected] wrote:

they actually are in sync

Fri Apr 2 17:13:05 CDT 2010 == 2010-04-02 22:13:05 (UTC) as CDT is UTC

  • 5 hours

Sorry for my sloppiness–I understand. I guess my main question is why
would it show the correct time in the view on one machine, but not
another?

datetime.strftime(“%A %B %d %Y %I:%M:%S %p”)

Is different for each machine. I’m busily writing code to detect
where the user is, and adjusting the time shown in the view
accordingly, but I’m still mildly confused by why the two views show
differently.

Thanks,
Craig

On 2 April 2010 20:42, Dudebot [email protected] wrote:

and what I see in the production html is ?date=Thu+Apr
+01+23%3A02%3A00±0500+2010
and I’m wondering if somehow it’s choking on the date format on the
production server…

Am I right in assuming that is what is contained in the <a href link
in the html? If so then the first thing is that they are in different
timezones. Can you confirm that you are definitely seeing the above
dates with the same version of the code. You said you had been
experimenting with setting the timezone in rails, are you sure you
have updated the production server and restarted it with the same code
as on your development system.

Do you want to see a local time there or UTC?

What have you currently got the timezone set to in environment.rb?

The second thing is that they are formatted differently. How are you
formatting it in your erb code? If you have not got explicit format
it might be worth doing so.

Colin

On Apr 3, 2:19 am, Colin L. [email protected] wrote:

The above code will assume that you want to display it in local time,
if you change which timezone you tell rails it is in then that will
change the display of local time. Values in the db will always be in
UTC as far as Rails is concerned.

I’ve learned that from this exercise :wink:

How are you detecting which timezone the user is in? I did not think
it was possible (other than asking him of course).

In my new, improved design, I’m storing the time offset for the user
in the user database.

Last weekend I discovered my laptop has a feature that will try to
detect the current time zone, kind of like a cell phone. Perhaps that
will become a ubiquitous feature among operating systems, and then I
could use it for this… :slight_smile:

Thanks again for all your help,
Craig

OK, I finally figured this one out, and I’m thinking of it more as a
non-caught bug in my original code :slight_smile:

When I pass a date into the params hash in the view, e.g.

link_to …, …path( …:date => date.drawn )

I was getting something like this in my html link on the development
machine:

?date=2010-03-31+18%3A19%3A00+UTC

And something like this on the production machine:

?date=Thu+Apr+01+23%3A02%3A00±0500+2010

So on the production machine it was making the date human readable
before passing into the params hash–which I would think would be
intended behavior. I solved it by uncasting it in the view:

link_to …, …path( …:date => date.drawn_before_type_cast )

A little tidbit I picked up while watching Ryan B. most excellent
screencast on time zones

Hope this helps others out there wanting to pass dates in the params
hash, and thanks again for all your help,
Craig

On 3 April 2010 00:37, Dudebot [email protected] wrote:

would it show the correct time in the view on one machine, but not
another?

datetime.strftime(“%A %B %d %Y %I:%M:%S %p”)

Is different for each machine. I’m busily writing code to detect
where the user is, and adjusting the time shown in the view
accordingly, but I’m still mildly confused by why the two views show
differently.

The above code will assume that you want to display it in local time,
if you change which timezone you tell rails it is in then that will
change the display of local time. Values in the db will always be in
UTC as far as Rails is concerned.

How are you detecting which timezone the user is in? I did not think
it was possible (other than asking him of course).

Colin