Hello everyone, I''ve been struggling with something that is probably very simple. I need to save an entry from an atom feed that has a datetime string like this: 2010-07-20T11:49:19.000Z which is an XMLSchema date time. I''ve tried many variations around: glog.elements.each("published") do |element| @snapshot.entrycreated Time.xmlschema(element.text).to_s(:db) end If I save that to a regular string I get what looks correct to me: "2010-07-20 11:49:19" but saved in the entrycreated column it outputs as: "Sat Jan 01 11:49:19 UTC 2000" Can anyone help me with the correct code to save this datetime correctly pleased. In case it''s relevant, on my dev machine I have sqlite. Eventually it will be on mysql. Thanks for any help, Steve -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Time.zone.parse might be your friend here:>> date = Time.zone.parse(''2010-07-20T11:49:19.000Z'')date = Time.zone.parse(''2010-07-20T11:49:19.000Z'') => Tue, 20 Jul 2010 11:49:19 UTC +00:00>> date.to_s(:db)date.to_s(:db) => "2010-07-20 11:49:19">> date.class.namedate.class.name => "ActiveSupport::TimeWithZone" Rails extensions to Time and Date classes can take a bit to work out in certain circumstances. Hope that helps, Walter On Wed, Jul 21, 2010 at 8:31 AM, Steve Root <ukroot-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hello everyone, > > I''ve been struggling with something that is probably very simple. > > I need to save an entry from an atom feed that has a datetime string > like this: > 2010-07-20T11:49:19.000Z > which is an XMLSchema date time. > > I''ve tried many variations around: > > glog.elements.each("published") do |element| > -xzjGh16LyHhaTqR628vtCB7MsYLaNCIZ@public.gmane.org > Time.xmlschema(element.text).to_s(:db) > end > > If I save that to a regular string I get what looks correct to me: > "2010-07-20 11:49:19" > but saved in the entrycreated column it outputs as: "Sat Jan 01 > 11:49:19 UTC 2000" > > Can anyone help me with the correct code to save this datetime > correctly pleased. > > In case it''s relevant, on my dev machine I have sqlite. Eventually it > will be on mysql. > Thanks for any help, > Steve > > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Oh, to finish up. Time.parse looks to be sufficient for what you are after, too.>> date = Time.parse(''2010-07-20T11:49:19.000Z'')date = Time.parse(''2010-07-20T11:49:19.000Z'') => Tue Jul 20 11:49:19 UTC 2010>> date.class.namedate.class.name => "Time">> date.to_s(:db)date.to_s(:db) => "2010-07-20 11:49:19" IIRC, the ActiveSupport::TimeWithZone class is better at handling pre-1900 dates than Rails extension of the Time class. Cheers, Walter On Wed, Jul 21, 2010 at 2:36 PM, Walter McGinnis <walter.mcginnis-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Time.zone.parse might be your friend here: > >>> date = Time.zone.parse(''2010-07-20T11:49:19.000Z'') > date = Time.zone.parse(''2010-07-20T11:49:19.000Z'') > => Tue, 20 Jul 2010 11:49:19 UTC +00:00 >>> date.to_s(:db) > date.to_s(:db) > => "2010-07-20 11:49:19" >>> date.class.name > date.class.name > => "ActiveSupport::TimeWithZone" > > Rails extensions to Time and Date classes can take a bit to work out > in certain circumstances. > > Hope that helps, > Walter > > On Wed, Jul 21, 2010 at 8:31 AM, Steve Root <ukroot-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> Hello everyone, >> >> I''ve been struggling with something that is probably very simple. >> >> I need to save an entry from an atom feed that has a datetime string >> like this: >> 2010-07-20T11:49:19.000Z >> which is an XMLSchema date time. >> >> I''ve tried many variations around: >> >> glog.elements.each("published") do |element| >> -xzjGh16LyHhaTqR628vtCB7MsYLaNCIZ@public.gmane.org >> Time.xmlschema(element.text).to_s(:db) >> end >> >> If I save that to a regular string I get what looks correct to me: >> "2010-07-20 11:49:19" >> but saved in the entrycreated column it outputs as: "Sat Jan 01 >> 11:49:19 UTC 2000" >> >> Can anyone help me with the correct code to save this datetime >> correctly pleased. >> >> In case it''s relevant, on my dev machine I have sqlite. Eventually it >> will be on mysql. >> Thanks for any help, >> Steve >> >> >> -- >> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. >> To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. >> >> >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Tue, Jul 20, 2010 at 1:31 PM, Steve Root <ukroot-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> If I save that to a regular string I get what looks correct to me: > "2010-07-20 11:49:19" > but saved in the entrycreated column it outputs as: "Sat Jan 01 > 11:49:19 UTC 2000"How are you trying to save this? As a string?>> bird = Bird.first=> #<Bird id: 2, common_name: "hummingbird", species: "hoverus buzzus", migratory: true, created_at: "2010-03-05 21:08:36", updated_at: "2010-05-28 14:40:52", primary_food: "nectar", secondary_food: "">>> whenever = bird.created_at=> Fri, 05 Mar 2010 21:08:36 UTC 00:00>> whenever.class=> ActiveSupport::TimeWithZone So not a *string* here...>> dt = DateTime.now=> Tue, 20 Jul 2010 18:12:54 -0700>> bird.created_at = dt=> Tue, 20 Jul 2010 18:12:54 -0700>> bird.save=> true So maybe you want to convert your string to a DateTime object and save that? Just one possibility :-) -- Hassan Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org twitter: @hassan -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Jul 20, 9:31 pm, Steve Root <ukr...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> If I save that to a regular string I get what looks correct to me: > "2010-07-20 11:49:19" > but saved in the entrycreated column it outputs as: "Sat Jan 01 > 11:49:19 UTC 2000" >Is you entrycreated column a time column ? Time columns only save a time of day (and ignore the date), what you want is a datetime column. Fred> Can anyone help me with the correct code to save this datetime > correctly pleased. > > In case it''s relevant, on my dev machine I have sqlite. Eventually it > will be on mysql. > Thanks for any help, > Steve-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 21 July, 08:08, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Jul 20, 9:31 pm, Steve Root <ukr...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > If I save that to a regular string I get what looks correct to me: > > "2010-07-20 11:49:19" > > but saved in the entrycreated column it outputs as: "Sat Jan 01 > > 11:49:19 UTC 2000" > > Is you entrycreated column a time column ? Time columns only save a > time of day (and ignore the date), what you want is a datetime column. > > Fred >Thanks! It was a simple as that. I''m sure I''d already checked my migration was creating a t.datetime column, but checking now it had t.time -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.