Calling Date.today in Ruby returns the current date. However, what timezone is it in? I assume UTC, but I want to make sure. The documentation doesn’t state either way.
Calling Date.today in Ruby returns the current date. However, what timezone is it in?
Share
TL;DR:
Date.todayuses the system’s local time zone. If you require it be in UTC, instead get the date from a UTC time, e.g.Time.now.utc.to_date.Dates do not have timezones, since they don’t represent a time.
That said, as for how it calculates the current day, let’s look at this extract from the code for
Date.today:It then proceeds to use use
tmto create theDateobject. Sincetmcontains the system’s local time usinglocaltime(),Date.todaytherefore uses the system’s local time, not UTC.You can always use
Time#utcon anyTimeconvert it in-place to UTC, orTime#getutcto return a new equivalentTimeobject in UTC. You could then callTime#to_dateon that to get aDate. So:some_time.getutc.to_date.If you’re using ActiveSupport’s time zone support (included with Rails), note that it is completely separate from Ruby’s time constructors and does not affect them (i.e. it does not change how
Time.noworDate.todaywork). See also ActiveSupport extensions toTime.