Seems Time.zonew.now depends on the machine current time. It's weird for me.
application.rb
config.time_zone = 'Central Time (US & Canada)'
I made the experiment.
real current time time in CST time zone
5:54 AM
STEP 1) I have forced current machine time to 7:37pm.
system time
drobazko#drobazko:~/www$ date
Mon Aug 17 19:37:35 EDT 2015
rails c
1.9.3-p484 :001 > Time.zone
=> (GMT-06:00) Central Time (US & Canada)
1.9.3-p484 :002 > Time.zone.now
=> Mon, 17 Aug 2015 18:40:15 CDT -05:00
STEP 2) I have forced current machine time to 3:17pm.
real current time time in CST
5:54 AM
system time
drobazko#drobazko:~/www$ date
Mon Aug 17 15:17:09 EDT 2015
rails c
1.9.3-p484 :001 > Time.zone
=> (GMT-06:00) Central Time (US & Canada)
1.9.3-p484 :002 > Time.zone.now
=> Mon, 17 Aug 2015 14:18:39 CDT -05:00
Question: why Time.zone.now doesn't give me real current CST time about 5am?
You can find the solution here: http://www.timeanddate.com/time/zones/cst
In Summer: CDT and Winter: CST. At the moment, we are in Summer :)
Time.now method returns system time, so Time.zone.now also returns system time but in the application's time zone.
Related
I've been banging my head against this for a while and I can't seem to understand how rails timezones and in_time_zone works.
Here is some rails c output that I'd like to understand:
[26] VMM(bby - main - dev)> Time.zone.now
=> Wed, 14 Mar 2018 09:13:17 CDT -05:00
[27] VMM(bby - main - dev)> MyModel.first.started_at
=> Fri, 09 Mar 2018 09:17:00 CST -06:00
[28] VMM(bby - main - dev)> MyModel.first.started_at.in_time_zone(Time.zone)
=> Fri, 09 Mar 2018 09:17:00 CST -06:00
So:
From the first line, the Time.zone seems to be CDT -5.
From the second line, the started_at attribute seem to be CST -6
On the third line, my intention is to change that atribute to use CDT -5, so I'd expect an output of Fri, 09 Mar 2018 10:17:00 CDT -05:00
Why does this behave as it does instead of how I expect it to?
Thanks in advance!
My server's clock is set to London time (I'm currently living in the GMT+0 zone). The output of timedatectl status gives me this:
Local time: Mon 2016-05-23 08:13:06 BST Universal time: Mon 2016-05-23 07:13:06 UTC
RTC time: Mon 2016-05-23 07:13:06
Timezone: Europe/London (BST, +0100)
NTP enabled: yes NTP synchronized: no RTC in local TZ: no
DST active: yes Last DST change: DST began at
Sun 2016-03-27 00:59:59 GMT
Sun 2016-03-27 02:00:00 BST Next DST change: DST ends (the clock jumps one hour backwards) at
Sun 2016-10-30 01:59:59 BST
Sun 2016-10-30 01:00:00 GMT
And in my application.rb file I set the timezone:
config.time_zone = 'London'
Locally, it works right:
Time.now.dst?
# true
Time.now.hour
# 8
But in production it doesn't seem that DST is on.
Time.now.dst?
# false
Time.now.hour
# 7
I am not sure if it's a Rails thing or a server misconfiguration, but for me it seems right.
If this application is running within a docker container, the timezone of the host server may not be applied to the containers. You can probably set the TZ environment variable to whatever timezone you'd like and ruby will pick it up.
I highly recommend not using non-UTC timezone for your application/database, as it will almost certainly cause problems down the line.
Full disclosure: I am one of the dokku maintainers.
I tried to parse this time string "21:58:06 Apr 29, 2015 PDT". What is the right way to do that?
first approach:
zone = Time.zone
Time.zone = "Pacific Time (US & Canada)"
payed_at = Time.parse params[:payment_date]
payed_at.in_time_zone(zone)
result:
Thu, 30 Apr 2015 10:21:30 CEST +02:00
second guess:
payed_at = DateTime.parse date
result:
Thu, 30 Apr 2015 21:58:29 +0000
the correct result would be
Thu, 30. April 2015, 06:58 Uhr
environment:
ruby '2.1.5'
gem 'rails', '~> 3.2.15'
Since you have rails specified in tags, here is an ActiveSupported solution:
tz = ActiveSupport::TimeZone['US/Pacific']
tz.parse("21:58:06 Apr 29, 2015 PDT").localtime
#⇒ 2015-05-01 06:58:29 +0200
Parse using strptime method
Eg : DateTime.strptime('December 09, 2011', '%B %d, %Y')
http://ruby-doc.org/stdlib-1.9.3/libdoc/date/rdoc/DateTime.html#method-c-_strptime
I've tried
config.time.in_time_zone("Pacific Time (US & Canada)")
as well as
config.time_zone("PST")
and both caused errors and caused the heroku app to crash. What is the proper way to switch my app to PST time?
Thanks!!
Place the following in your config/application.rb and restart rails server:
config.time_zone = 'Pacific Time (US & Canada)'
Here is the description from Rails Guides Configuring Rails Application:
config.time_zone sets the default time zone for the application and
enables time zone awareness for Active Record.
Then the usage for in_time_zone:
> Date.today
=> Thu, 13 Mar 2014
> Time.zone
=> (GMT-08:00) Pacific Time (US & Canada)
> Date.today.to_time.in_time_zone
=> Wed, 12 Mar 2014 21:00:00 PDT -07:00
To output a time in different time zone:
> Date.today.to_time.in_time_zone('Eastern Time (US & Canada)')
=> Wed, 13 Mar 2014 00:00:00 EDT -04:00
I am running Rails on two machines, but getting different results from to_time method. I only checked the system timezone and Time.zone configuration, am I missing anything? Thanks
Server 1
user#Server1:/var/www/app$ date
Wed Oct 23 23:56:35 MDT 2013
user#Server1:/var/www/app$ cat /etc/timezone
America/Denver
user#Server1:/var/www/app$ bundle exec rails c production
irb(main):011:0> Time.zone
=> (GMT+00:00) UTC
irb(main):012:0> Date.parse("10/24/2013").to_time
=> 2013-10-24 00:00:00 +0000
Server 2
user#Server2:/var/www/app$ date
Thu Oct 24 00:03:28 MDT 2013
user#Server2:/var/www/app$ cat /etc/timezone
America/Denver
user#Server2:/var/www/app$ bundle exec rails c production
irb(main):002:0> Time.zone
=> (GMT+00:00) UTC
irb(main):003:0> Date.parse("10/24/2013").to_time
=> 2013-10-24 00:00:00 -0600
Time#local_time (called by ActiveSupport in the implementation of Date#to_time) respects the TZ environment variable for which timezone should be used for the "local" time.
Ensure that the environment is the same between the two servers.