Hi all, I am trying to change a date from a time zone to UTC. First I set the time zone and then get its offset from UTC. Then I am trying to change the time zone of my date by substracting the offset but it failed. --------------- Time.zone = @time_zone # ''Paris'' @offset = Time.zone.utc_offset() # @offset = 3600 # @date = now @date = @date - @offset # @date should be now - 1 but its somewhere in 1998 ... --------------- Reading the pick axe Time documentation, when you substract something from a date, it should be in seconds, which is the case. But with my code, it failed. What can I do to fix that ? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Nov 4, 5:32 pm, MR Damien <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hi all, > > I am trying to change a date from a time zone to UTC. > > First I set the time zone and then get its offset from UTC. > > Then I am trying to change the time zone of my date by substracting the > offset but it failed. > > --------------- > Time.zone = @time_zone # ''Paris'' > @offset = Time.zone.utc_offset() # @offset = 3600 > > # @date = now > @date = @date - @offset > > # @date should be now - 1 but its somewhere in 1998 ... > --------------- > > Reading the pick axe Time documentation, when you substract something > from a date, it should be in seconds, which is the case. But with my > code, it failed. >Are you sure your date is an instance of Time and not an instance of Date? (for Date - 1 means -1 day so -3600 would be about 10 years ie somewhere in 1998). Fred> What can I do to fix that ? > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung wrote:> On Nov 4, 5:32�pm, MR Damien <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: >> Time.zone = @time_zone # ''Paris'' >> code, it failed. >> > Are you sure your date is an instance of Time and not an instance of > Date? (for Date - 1 means -1 day so -3600 would be about 10 years ie > somewhere in 1998). > > FredHi, I am using DateTime. @date = DateTime.strptime(params[:start_date], "%B %d, %Y %I:%M %p") It seems that - 1 substract a day as you said. I will now search how to convert a DateTime to Time. Thanks -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@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 5 Nov 2008, at 09:09, MR Damien wrote:> > Frederick Cheung wrote: >> On Nov 4, 5:32�pm, MR Damien <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> >> wrote: >>> Time.zone = @time_zone # ''Paris'' >>> code, it failed. >>> >> Are you sure your date is an instance of Time and not an instance of >> Date? (for Date - 1 means -1 day so -3600 would be about 10 years ie >> somewhere in 1998). >> >> Fred > > Hi, > > I am using DateTime. > > @date = DateTime.strptime(params[:start_date], "%B %d, %Y %I:%M %p") > > It seems that - 1 substract a day as you said. I will now search how > to > convert a DateTime to Time.You can also use rails'' date/time calculation things, for example t += 5.seconds does the right thing whether t is a Time or a DateTime Fred --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung wrote:> On 5 Nov 2008, at 09:09, MR Damien wrote: > >>> >> convert a DateTime to Time. > You can also use rails'' date/time calculation things, for example > > t += 5.seconds > > does the right thing whether t is a Time or a DateTime > > FredHey, Thanks for that trick. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---