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.
相关问题
- How to specify memcache server to Rack::Session::M
- Date with SimpleDateFormat in Java
- Why am I getting a “C compiler cannot create execu
- reference to a method?
- Display time in local timezone when querying Influ
相关文章
- Ruby using wrong version of openssl
- Difference between Thread#run and Thread#wakeup?
- how to call a active record named scope with a str
- “No explicit conversion of Symbol into String” for
- Segmentation fault with ruby 2.0.0p247 leading to
- How to detect if an element exists in Watir
- uninitialized constant Mysql2::Client::SECURE_CONN
- ruby - simplify string multiply concatenation
TL;DR:
Date.today
uses 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
tm
to create theDate
object. Sincetm
contains the system's local time usinglocaltime()
,Date.today
therefore uses the system's local time, not UTC.You can always use
Time#utc
on anyTime
convert it in-place to UTC, orTime#getutc
to return a new equivalentTime
object in UTC. You could then callTime#to_date
on 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.now
orDate.today
work). See also ActiveSupport extensions toTime
.You can use
To get the current date on the configured timezone.
An instance of Date is represented as an Astronomical Julian Day Number; it has no fractional part of a day. While a Julian Day is relative to GMT - so technically an instance of Date should be considered to be in GMT - for most purposes you can ignore that and treat it as having no timezone. You would only care about a time zone if you converted it to a DateTime or Time.
ActiveSupport's tools for date conversion let you specify whether you want local time or UTC.
E.g.:
If your rails applications is configured to run in time zone UTC then just use
Time.zone.today
, otherwise force it by usingTime.now.utc.to_date
You can get away by using
Time.now.utc.to_date
in ruby