Why does Time.now return 2013-12-10 20:49:59 -0600 when Time.now.utc returns 2013-12-11 02:49:59 UTC?
Time.now is your local time.
Time.now.utc is the UTC time. Not your time.
read about UTC here http://en.wikipedia.org/wiki/Coordinated_Universal_Time
Time.now is evaluated in your time zone, which is -0600 or 6 hours behind utc. You'll notice that if you add those 6 hours to the time you get from Time.now, you will get the Time.now.utc result.
Related
I'm trying to add a countdown and I need to calculate the remaining time in millisecond.
Basically I have a button that user can press 24 hours after the last press.
I have
last_press
# => Mon, 10 Jan 2022 10:31:25.000000000 UTC +00:00
And the time difference.
I add 1 day to the last press and I remove the current time
Time.now
# => 2022-01-10 11:50:59 +0100
time_diff = last_press + 1.day - Time.now
If I parse the result is
Time.at(time_diff.to_i.abs).utc.strftime "%H:%M:%S"
# => "00:09:24"
The issue is that time_diff is a float
time_diff = last_meditation + 1.day - Time.now
# => 85180.960988
Basically the calculation is wrong... and I cannot understand why.
I am located in the Eastern Time (ET) timezone. It is 4 hours behind UTC time. UTC is the standard by which the world regulates clocks and time. I am using Mac OSX.
I was reading this particular article. It seems to suggest that Ruby uses UTC by default if the TZ environment variable is not set. And he gives an example:
ENV["TZ"]
#=> nil
Time.now
#=> 2015-12-08 10:30:00 -0200
ENV["TZ"] = "America/Los_Angeles"
#=> "America/Los_Angeles"
Time.now
#=> 2015-12-08 04:30:14 -0800
Once the environment variable is set, Ruby will then use that timezone. So I tried it in irb:
ENV["TZ"]
=> nil
Time.now
=> 2018-03-29 16:30:21 -0400
ENV["TZ"] = 'Eastern Time (US & Canada)'
=> "Eastern Time (US & Canada)"
Time.now
=> 2018-03-29 20:30:40 +0000
Right now it is a little after 4:30pm or 16:30 in military time. So actually when the TZ environment variable was NOT set, it gave the local time, not UTC time. And after I set the variable to my timezone, it gave a completely wrong time. Why do I not get UTC time when environment variable is not set and why do I get the wrong time when I set the environment variable to my timezone?
This is incorrect. Ruby will pick up the time zone from your computer rather than use UTC by default.
As for why
ENV["TZ"] = 'Eastern Time (US & Canada)'
didn't change the time to eastern is because the right way to do it is:
ENV["TZ"] = 'US/Eastern'
When the value in ENV["TZ"] is not nil, but is not recognized, Ruby defaults to UTC:
Time.now.zone # => "EEST"
ENV["TZ"] = 'Some gibberish'
Time.now.zone # => "UTC"
ENV["TZ"] = 'US/Eastern'
Time.now.zone # => "EDT"
I need to assign a timezone offset to a Time to get current day of the week for a specified offset.
This is not with rails so I need a pure Ruby formatter/parser to do this.
Thanks.
This is what I found:
require 'date'
local = DateTime.now
new_offset = Rational(0, 24) #put the offset you want as first argument
utc = local.new_offset(new_offset)
Returns the offset in seconds between the timezone of time and UTC.
t = Time.gm(2000,1,1,20,15,1) #=> 2000-01-01 20:15:01 UTC
t.gmt_offset #=> 0
l = t.getlocal #=> 2000-01-01 14:15:01 -0600
l.gmt_offset #=> -21600
#As a string
t = Time.new(2011,6,27,14,10,0, "+07:00")
# or in seconds from UTC
t = Time.new(2011,6,27,14,10,0, 7*60*60)
I have an app deployed on a server where the system time is 7 hours behind UTC. I'm actually in England so I want times displayed in local time GMT (with daylight saving adjusted).
A gem that I'm using, resque, uses Time.now to retrieve the current time. What do I need to configure to get Time.now to return the correct time?
The easiest way would be to set the ENV["TZ"] variable.
> Time.now
=> 2011-05-21 13:13:23 +0200
> ENV["TZ"] = "Europe/London"
=> "Europe/London"
> Time.now
=> 2011-05-21 12:13:55 +0100
Check out some "time warping" gems: http://ruby-toolbox.com/categories/time_warping.html
If I have d = DateTime.now, how do I convert 'd' into UTC (with the appropriate date)?
DateTime.now.new_offset(0)
will work in standard Ruby (i.e. without ActiveSupport).
d = DateTime.now.utc
Oops!
That seems to work in Rails, but not vanilla Ruby (and of course that is what the question is asking)
d = Time.now.utc
Does work however.
Is there any reason you need to use DateTime and not Time? Time should include everything you need:
irb(main):016:0> Time.now
=> Thu Apr 16 12:40:44 +0100 2009
Unfortunately, the DateTime class doesn't have the convenience methods available in the Time class to do this. You can convert any DateTime object into UTC like this:
d = DateTime.now
d.new_offset(Rational(0, 24))
You can switch back from UTC to localtime using:
d.new_offset(DateTime.now.offset)
where d is a DateTime object in UTC time. If you'd like these as convenience methods, then you can create them like this:
class DateTime
def localtime
new_offset(DateTime.now.offset)
end
def utc
new_offset(Rational(0, 24))
end
end
You can see this in action in the following irb session:
d = DateTime.now.new_offset(Rational(-4, 24))
=> #<DateTime: 106105391484260677/43200000000,-1/6,2299161>
1.8.7 :185 > d.to_s
=> "2012-08-03T15:42:48-04:00"
1.8.7 :186 > d.localtime.to_s
=> "2012-08-03T12:42:48-07:00"
1.8.7 :187 > d.utc.to_s
=> "2012-08-03T19:42:48+00:00"
As you can see above, the initial DateTime object has a -04:00 offset (Eastern Time). I'm in Pacific Time with a -07:00 offset. Calling localtime as described previously properly converts the DateTime object into local time. Calling utc on the object properly converts it to a UTC offset.
Try this, works in Ruby:
DateTime.now.to_time.utc
You can set an ENV if you want your Time.now and DateTime.now to respond in UTC time.
require 'date'
Time.now #=> 2015-11-30 11:37:14 -0800
DateTime.now.to_s #=> "2015-11-30T11:37:25-08:00"
ENV['TZ'] = 'UTC'
Time.now #=> 2015-11-30 19:37:38 +0000
DateTime.now.to_s #=> "2015-11-30T19:37:36+00:00"
In irb:
>>d = DateTime.now
=> #<DateTime: 11783702280454271/4800000000,5/12,2299161>
>> "#{d.hour.to_i - d.zone.to_i}:#{d.min}:#{d.sec}"
=> "11:16:41"
will convert the time to the utc. But as posted if it is just Time you can use:
Time.now.utc
and get it straight away.