I''m having a problem converting a date retrieved from my MySQL database and I''m hoping someone can help. MySQL stores the date/time as this: 2005-03-08 17:00:34.0 and according to my Pickaxe book Ruby stores times as "the number of seconds and microseconds since [...] January 1, 1970". All well and good. Unfortunately, RoR is converting my date to this: Tue Mar 08 17:00:34 MST 2005 which, as far as I can tell, is as string representation of the date; as such, I can''t perform any date operations on it (strftime, etc). My question has two parts: 1) Can some sort of date operation be brought to bear on RoR''s internal date format to make it compatible with standard Ruby date operations? 2) If not, is there some way to turn off RoR''s parsing of the date returned from the MySQL query? -- Nathan Wright http://www.brandalism.com
On Tue, 8 Mar 2005, Nathan Wright wrote:> I''m having a problem converting a date retrieved from my MySQL database and > I''m hoping someone can help. > > MySQL stores the date/time as this: > > 2005-03-08 17:00:34.0 > > and according to my Pickaxe book Ruby stores times as "the number of seconds > and microseconds since [...] January 1, 1970". All well and good. > Unfortunately, RoR is converting my date to this: > > Tue Mar 08 17:00:34 MST 2005this looks like the output of Time#to_s to me - eg. it looks like you do, in fact, have Time object and should be able to do manipulations with it> which, as far as I can tell, is as string representation of the date; as > such, I can''t perform any date operations on it (strftime, etc).what are you trying to do that you can''t?> > My question has two parts: > 1) Can some sort of date operation be brought to bear on RoR''s internal date > format to make it compatible with standard Ruby date operations? > 2) If not, is there some way to turn off RoR''s parsing of the date returned > from the MySQL query? > > -- > Nathan Wright > http://www.brandalism.com > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-a -- ==============================================================================| EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov | PHONE :: 303.497.6469 | When you do something, you should burn yourself completely, like a good | bonfire, leaving no trace of yourself. --Shunryu Suzuki ===============================================================================
>> Tue Mar 08 17:00:34 MST 2005 > > this looks like the output of Time#to_s to me - eg. it looks like you > do, in > fact, have Time object and should be able to do manipulations with itThat''s what I thought, but nothing seems to work with it.> what are you trying to do that you can''t?For instance, I call that time from the database with this query in Rails: comment.created_on Since I want to retrieve only the name of the date (i.e., "Tuesday"), I tried to use this: comment.created_on.strftime("%A") which doesn''t seem to work. By your first comment it looks as though I''m inadvertently calling Time#to_s without realizing it, but I don''t see how ... -- Nathan Wright http://www.brandalism.com
On Tue, 8 Mar 2005, Nathan Wright wrote:>>> Tue Mar 08 17:00:34 MST 2005 >> >> this looks like the output of Time#to_s to me - eg. it looks like you do, >> in >> fact, have Time object and should be able to do manipulations with it > > That''s what I thought, but nothing seems to work with it. > >> what are you trying to do that you can''t? > > For instance, I call that time from the database with this query in Rails: > > comment.created_on > > Since I want to retrieve only the name of the date (i.e., "Tuesday"), I tried > to use this: > > comment.created_on.strftime("%A") > > which doesn''t seem to work.define ''doesn''t seem to work''.> By your first comment it looks as though I''m inadvertently calling Time#to_s > without realizing it, but I don''t see how ...is your object interpolated in a string somewhere? "#{ myobj }" ?? render the output of comment.created_on.class and you will know. -a -- ==============================================================================| EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov | PHONE :: 303.497.6469 | When you do something, you should burn yourself completely, like a good | bonfire, leaving no trace of yourself. --Shunryu Suzuki ===============================================================================
I''ve found similar problems with dates cropping up as strings in places. In particular, I submitted a ticket (http://dev.rubyonrails.com/ticket/681) about dates in association tables. The tests for ActiveRecord use to_s on dates and times liberally, so would not pick up if a date was coming out as a String rather than a Date. Try looking at comment.created_on.class to see if it really is a string. Pete Yandell On 09/03/2005, at 11:34 AM, Nathan Wright wrote:> I''m having a problem converting a date retrieved from my MySQL > database and I''m hoping someone can help. > > MySQL stores the date/time as this: > > 2005-03-08 17:00:34.0 > > and according to my Pickaxe book Ruby stores times as "the number of > seconds and microseconds since [...] January 1, 1970". All well and > good. Unfortunately, RoR is converting my date to this: > > Tue Mar 08 17:00:34 MST 2005 > > which, as far as I can tell, is as string representation of the date; > as such, I can''t perform any date operations on it (strftime, etc). > > My question has two parts: > 1) Can some sort of date operation be brought to bear on RoR''s > internal date format to make it compatible with standard Ruby date > operations? > 2) If not, is there some way to turn off RoR''s parsing of the date > returned from the MySQL query? > > -- > Nathan Wright > http://www.brandalism.com > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
How about a test case here? Something like: def test_strftime comment = Comment.find(1) create_time = comment.created_on assert_kind_of(Time, create_time) day_of_week = create_time.strftime("%A") assert_equal("Tuesday", day_of_week) end Can you stick that in your test suite and see where it fails? Pete. On Tue, 08 Mar 2005 18:04:03 -0700, Nathan Wright <nathan-t2/9jZt1M8BPjb27YXYYTQ@public.gmane.org> wrote:> >> Tue Mar 08 17:00:34 MST 2005 > > > > this looks like the output of Time#to_s to me - eg. it looks like you > > do, in > > fact, have Time object and should be able to do manipulations with it > > That''s what I thought, but nothing seems to work with it. > > > what are you trying to do that you can''t? > > For instance, I call that time from the database with this query in Rails: > > comment.created_on > > Since I want to retrieve only the name of the date (i.e., "Tuesday"), I > tried to use this: > > comment.created_on.strftime("%A") > > which doesn''t seem to work. By your first comment it looks as though I''m > inadvertently calling Time#to_s without realizing it, but I don''t see how > ... > > > -- > Nathan Wright > http://www.brandalism.com > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > >
Nathan Wright:> 2) If not, is there some way to turn off RoR''s parsing of the date > returned from the MySQL query?I do this: @page.created_at.to_s(:db) It gives the raw date string from MySQL. I asked David about it and he said that it works for MySQL, PostgreSQL and SQLite. The others might store the date string differently. Sascha
> Try looking at comment.created_on.class to see if it really is a string.If it is being returned as a string (I''ll have to look into this tomorrow), how does one convert this string back to a time? More to the point, how would the time have gotten changed into a string in the first place? -- Nathan Wright http://www.brandalism.com
> How about a test case here?I''ll try the test case tomorrow and let you know how it comes out. -- Nathan Wright http://www.brandalism.com
> I do this: > > @page.created_at.to_s(:db) > > It gives the raw date string from MySQL.Thanks, I''ll keep this in mind as a last resort if I can''t figure out what I''m doing wrong. -- Nathan Wright http://www.brandalism.com
Sorry for taking so long to check out everyone''s suggestions, but here''s what I''ve found out: I''m accessing the time from the MySQL database with this query "comment.created_on". By checking the class of this query with "comment.created_on.class" I''ve found out that this is, in fact, returning a Time object. (neat trick by the way!) When I attempt to apply any "time" methods to this object (for instance, "comment.created_on.strftime_("%A")") I get the following error: undefined method `strftime'' for nil:NilClass Does anyone have an idea of what I''m doing wrong (likely), or is this a problem with Rails (unlikely)? --------------------------- Original Problem (If you''re joining us late in the game): --------------------------- I''m having a problem converting a date retrieved from my MySQL database and I''m hoping someone can help. MySQL stores the date/time as this: 2005-03-08 17:00:34.0 and according to my Pickaxe book Ruby stores times as "the number of seconds and microseconds since [...] January 1, 1970". All well and good. Unfortunately, RoR is converting my date to this: Tue Mar 08 17:00:34 MST 2005 which, as far as I can tell, is as string representation of the date; as such, I can''t perform any date operations on it (strftime, etc). My question has two parts: 1) Can some sort of date operation be brought to bear on RoR''s internal date format to make it compatible with standard Ruby date operations? 2) If not, is there some way to turn off RoR''s parsing of the date returned from the MySQL query? -- Nathan Wright http://www.brandalism.com
On Wed, 9 Mar 2005, Nathan Wright wrote:> Sorry for taking so long to check out everyone''s suggestions, but here''s what > I''ve found out: > > I''m accessing the time from the MySQL database with this query > "comment.created_on". > > By checking the class of this query with "comment.created_on.class" I''ve > found out that this is, in fact, returning a Time object. (neat trick by the > way!) > > When I attempt to apply any "time" methods to this object (for instance, > "comment.created_on.strftime_("%A")") I get the following error:what''s with the underscore?> undefined method `strftime'' for nil:NilClassyou can also render this (another neat trick): comment.created_on.methods.inspect or even comment.created_on.class.instance_methods.inspect if you see ''strftime'' in there you are doing something strange. of course you can always ask the object too by rendering the output of comment.created_on.repspond_to? ''strftime''> > Does anyone have an idea of what I''m doing wrong (likely), or is this a > problem with Rails (unlikely)? > > > --------------------------- > Original Problem (If you''re joining us late in the game): > --------------------------- > I''m having a problem converting a date retrieved from my MySQL database and > I''m hoping someone can help. > > MySQL stores the date/time as this: > > 2005-03-08 17:00:34.0 > > and according to my Pickaxe book Ruby stores times as "the number of seconds > and microseconds since [...] January 1, 1970". All well and > good. Unfortunately, RoR is converting my date to this: > > Tue Mar 08 17:00:34 MST 2005 > > which, as far as I can tell, is as string representation of the date; as > such, I can''t perform any date operations on it (strftime, etc). > > My question has two parts: > 1) Can some sort of date operation be brought to bear on RoR''s internal date > format to make it compatible with standard Ruby date > operations? > 2) If not, is there some way to turn off RoR''s parsing of the date returned > from the MySQL query? > > > -- > Nathan Wright > http://www.brandalism.com > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-a -- ==============================================================================| EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov | PHONE :: 303.497.6469 | When you do something, you should burn yourself completely, like a good | bonfire, leaving no trace of yourself. --Shunryu Suzuki ===============================================================================
>> When I attempt to apply any "time" methods to this object (for instance, >> "comment.created_on.strftime_("%A")") I get the following error: > > what''s with the underscore?Did I mention that I''m a sloppy typist? :) That should have read "comment.created_on.strftime("%A")".> comment.created_on.methods.inspectThanks for another trick. I assume that this shows all of the methods that are available for this object ... is that right?> if you see ''strftime'' in there you are doing something strange.Ok, that narrowed it down. I must be doing something strange as "strftime" appeared in the list of available methods. Granted that I''m doing *something* wrong, any idea what that *something* could be? All I''m doing is letting RoR set the date/time when the method is created, and then having RoR pull the date/time out so that I can show when it was entered. I''m not doing any other transformations on it. Interestingly, I can use "comment.created_on.to_time.strftime("%A")" to get the information that I need, and this really confuses me. Given that the object created from "comment.created_on" is a Time object, why do I need to convert it to Time (with the "to_time" method) if it''s already a time object? Thanks for all your help. -- Nathan Wright http://www.brandalism.com
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On 09-Mar-2005, at 11:40, Ara.T.Howard wrote:> On Wed, 9 Mar 2005, Nathan Wright wrote: > >> Sorry for taking so long to check out everyone''s suggestions, but >> here''s what I''ve found out: >> >> I''m accessing the time from the MySQL database with this query >> "comment.created_on". >> >> When I attempt to apply any "time" methods to this object (for >> instance, >> "comment.created_on.strftime_("%A")") I get the following error: > > what''s with the underscore? > >> undefined method `strftime'' for nil:NilClass > > you can also render this (another neat trick): > > comment.created_on.methods.inspect > > or even > > comment.created_on.class.instance_methods.inspect > > if you see ''strftime'' in there you are doing something strange. of > course you > can always ask the object too by rendering the output of > > comment.created_on.repspond_to? ''strftime''Maybe I missed something, you should see strftime in the instance_methods, shouldn''t you? irb(main):001:0> t=Time.new => Wed Mar 09 12:02:07 EST 2005 irb(main):002:0> t.class.instance_methods => ["tv_usec", "to_a", "instance_eval", "gmtime", "type", "isdst", "protected_methods", "sec", "extend", "eql?", "_dump", "utc_offset", "instance_variable_set", "+", "is_a?", "month", "hash", "to_s", "usec", "-", "getlocal", "class", "min", "tainted?", "private_methods", "hour", "untaint", "year", "id", "to_i", "display", "dst?", <<<<<< "strftime" >>>>>>, "inspect", "getgm", "clone", "succ", "zone", "public_methods", "utc?", "mday", "freeze", "respond_to?", "__id__", "<=>", "to_f", "wday", "<", "methods", "==", "===", "getutc", "ctime", ">", "gmtoff", "nil?", "dup", "instance_variables", "gmt?", ">=", "instance_of?", "day", "<=", "send", "tv_sec", "between?", "localtime", "yday", "method", "object_id", "=~", "singleton_methods", "asctime", "__send__", "gmt_offset", "equal?", "taint", "frozen?", "instance_variable_get", "utc", "kind_of?", "mon"] Regards, JJ -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.4 (Darwin) iD8DBQFCLyze56Q2CqQ+d1wRAsmPAJ4qtt3bove7ezwj/MQus+VzQ1nYiQCgmWOg OM7Ts3hgGxk147uP4jaZkLo=VlVH -----END PGP SIGNATURE-----
On Wed, 9 Mar 2005, John Johnson wrote:>> if you see ''strftime'' in there you are doing something strange. of course >> you can always ask the object too by rendering the output ofgsub /you see/, "you don''t see">> >> comment.created_on.repspond_to? ''strftime'' > > Maybe I missed something, you should see strftime in the instance_methods, > shouldn''t you? > > irb(main):001:0> t=Time.new > => Wed Mar 09 12:02:07 EST 2005 > irb(main):002:0> t.class.instance_methods > => ["tv_usec", "to_a", "instance_eval", "gmtime", "type", "isdst", > "protected_methods", "sec", "extend", "eql?", "_dump", "utc_offset", > "instance_variable_set", "+", "is_a?", "month", "hash", "to_s", "usec", "-", > "getlocal", "class", "min", "tainted?", "private_methods", "hour", "untaint", > "year", "id", "to_i", "display", "dst?", <<<<<< "strftime" >>>>>>, "inspect", > "getgm", "clone", "succ", "zone", "public_methods", "utc?", "mday", "freeze", > "respond_to?", "__id__", "<=>", "to_f", "wday", "<", "methods", "==", "===", > "getutc", "ctime", ">", "gmtoff", "nil?", "dup", "instance_variables", > "gmt?", ">=", "instance_of?", "day", "<=", "send", "tv_sec", "between?", > "localtime", "yday", "method", "object_id", "=~", "singleton_methods", > "asctime", "__send__", "gmt_offset", "equal?", "taint", "frozen?", > "instance_variable_get", "utc", "kind_of?", "mon"] > > Regards, > JJ > -----BEGIN PGP SIGNATURE----- > Version: GnuPG v1.2.4 (Darwin) > > iD8DBQFCLyze56Q2CqQ+d1wRAsmPAJ4qtt3bove7ezwj/MQus+VzQ1nYiQCgmWOg > OM7Ts3hgGxk147uP4jaZkLo> =VlVH > -----END PGP SIGNATURE----- > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-a -- ==============================================================================| EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov | PHONE :: 303.497.6469 | When you do something, you should burn yourself completely, like a good | bonfire, leaving no trace of yourself. --Shunryu Suzuki ===============================================================================
On Wed, 9 Mar 2005, Nathan Wright wrote:> Thanks for another trick. I assume that this shows all of the methods that > are available for this object ... is that right?yes.>> if you (don''t) see ''strftime'' in there you are doing something strange. > > Ok, that narrowed it down. I must be doing something strange as "strftime" > appeared in the list of available methods. Granted that I''m doing > *something* wrong, any idea what that *something* could be? All I''m doing is > letting RoR set the date/time when the method is created, and then having > RoR pull the date/time out so that I can show when it was entered. I''m not > doing any other transformations on it.> Interestingly, I can use "comment.created_on.to_time.strftime("%A")" to get > the information that I need, and this really confuses me. Given that the > object created from "comment.created_on" is a Time object, why do I need to > convert it to Time (with the "to_time" method) if it''s already a time > object?you shouldn''t. for Time objects to_time is defined as def to_time self end eg. it''s a noop. something very odd is happening. can you post your actual code? -a -- ==============================================================================| EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov | PHONE :: 303.497.6469 | When you do something, you should burn yourself completely, like a good | bonfire, leaving no trace of yourself. --Shunryu Suzuki ===============================================================================
> Maybe I missed something, you should see strftime in the > instance_methods, shouldn''t you?Yes, I do see "strftime" in the instance_method list, however, when I try to call it on my object " comment.created_on.strftime("A") " I always get the same error message: undefined method `strftime'' for nil:NilClass Hmmmm, I just tried it again, and this time calling " comment.created_on.strftime("%A") " worked without a hitch. ???? I guess that my problem is solved, even though the solution is the same thing that I''ve been doing for the past day ... Thanks everyone for your help. -- Nathan Wright http://www.brandalism.com
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On 09-Mar-2005, at 12:11, Ara.T.Howard wrote:> On Wed, 9 Mar 2005, John Johnson wrote: > >>> if you see ''strftime'' in there you are doing something strange. of >>> course >>> you can always ask the object too by rendering the output of > > > gsub /you see/, "you don''t see" >NoMethodError: private method `gsub'' called for #<Email:0x5974c> :-) Regards, JJ -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.4 (Darwin) iD8DBQFCLzL256Q2CqQ+d1wRAlVfAJ9x4V6H7nnQQe/pYkpXVCwBz8ODAACg6y/3 fLlV11oeLfB8NNVnhDLs4gI=x5dp -----END PGP SIGNATURE-----
On Wed, 9 Mar 2005, Nathan Wright wrote:>> Maybe I missed something, you should see strftime in the instance_methods, >> shouldn''t you? > > Yes, I do see "strftime" in the instance_method list, however, when I try to > call it on my object " comment.created_on.strftime("A") " I always get the > same error message: > > undefined method `strftime'' for nil:NilClass > > > Hmmmm, I just tried it again, and this time calling " > comment.created_on.strftime("%A") " worked without a hitch. ???? I guess > that my problem is solved, even though the solution is the same thing that > I''ve been doing for the past day ...ah. i think i see the issue : your db wasn''t populated or the schema was modified somehow. eg. you WERE getting back a nil object for your created_on field because that field was empty for some reason. this could be because you changed schemas to created_on timestamp default current_timestamp or something like that. either that or the db was totally empty and now it isn''t? in any case you defintely could get nil mapped back to ruby if the field was emtpy in the db somehow and did not have a default value. you should check your schema closely. cheers. -a -- ==============================================================================| EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov | PHONE :: 303.497.6469 | When you do something, you should burn yourself completely, like a good | bonfire, leaving no trace of yourself. --Shunryu Suzuki ===============================================================================
> ah. i think i see the issue : your db wasn''t populated or the schema was > modified somehow. eg. you WERE getting back a nil object for your > created_on > field because that field was empty for some reason.You, my friend, are a *genius*. That''s exactly what happened. I had one row in the DB that had an empty time. RoR was reading it and choking on it when I attempted to convert the returned nil to a date. Thanks for the assistance. -- Nathan Wright http://www.brandalism.com
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On 09-Mar-2005, at 12:56, Nathan Wright wrote:>> ah. i think i see the issue : your db wasn''t populated or the schema >> was >> modified somehow. eg. you WERE getting back a nil object for your >> created_on >> field because that field was empty for some reason. > > You, my friend, are a *genius*. That''s exactly what happened. I had > one row in the DB that had an empty time. RoR was reading it and > choking on it when I attempted to convert the returned nil to a date. > Thanks for the assistance. > >You can also use a rescue in an Erb tag to handle these kinds of things, if you just want to display it: <%= @kit.created_on.strftime("%A") rescue "day unknown" %> Useful for putting a in a table so the cells will underline properly, even when empty. Regards, JJ -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.4 (Darwin) iD8DBQFCLzvV56Q2CqQ+d1wRAjesAJ9LOQl0nfnXDJrhGE9tB/qKwoz7QQCeK512 U+/Bo62/OzZGCk/Qtp6ZQ6o=Hz+Z -----END PGP SIGNATURE-----