ruby on rails - How to create a new DateTime object in a specific time zone (preferably the default time zone of my app, not UTC)? -


I have set the time zone in /config/application.rb , and I expect every time I've been created in my app by default in this time zone, even when I create a new DateTime object (using .new ), Then call it GMT . How can I get it in my app's time zone?

/config/application.rb

  config.time_zone = 'Pacific Time (US and Canada)'   

irb

  irb> Date time = now = & gt; Mercury, July 11, 2012 19:04:56 -0700 ERB & gt; Mydate = DateTime.new (2012, 07, 11, 20, 10, 0) => Mercury, July 11, 2012 20:10:00 +0000 # GMT, but I want to use PDT   

in_time_zone does not work because it's just GMT Time changes for PDT time, which is the wrong time:

  irb> Mydate.in_time_zone ('Pacific Time (US and Canada)') = & gt; Mercury, 11 July 2012 13:10:00 PDT-07: 00 # Wrong time (I need 20:10)    

You can use the ActiveSupport ( Time.zone ) object to create and parsize dates in your application's time zone:

 < Code> 1.9 3p0: 001 & gt; time. Zone.now = & gt; Mercury, 11 July 2012 19:47:03 PDT-07: 00 1. 9.3 p: 002 & gt; Time.zone.parse ('2012-07-11 21:00') = & gt; Mercury, 11 July 2012 21:00:00 PDT-07: 00    

Comments

Popular posts from this blog

Python SQLAlchemy:AttributeError: Neither 'Column' object nor 'Comparator' object has an attribute 'schema' -

java - How not to audit a join table and related entities using Hibernate Envers? -

mongodb - CakePHP paginator ignoring order, but only for certain values -