ruby Date.today 是 UTC 吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10219670/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 05:02:11  来源:igfitidea点击:

Is Date.today in UTC?

rubydatetimezone

提问by Matt Huggins

Calling Date.todayin Ruby returns the current date. However, what timezone is it in? I assume UTC, but I want to make sure. The documentationdoesn't state either way.

Date.today在 Ruby 中调用将返回当前日期。但是,它在哪个时区?我假设UTC,但我想确定。该文件没有说明任何一种方式。

回答by vipulnsward

You can get away by using

你可以通过使用

Time.now.utc.to_date

Time.now.utc.to_date

in ruby

红宝石

回答by Andrew Marshall

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.

TL;DR:Date.today使用系统的本地时区。如果您要求它是 UTC,则从 UTC 时间获取日期,例如Time.now.utc.to_date.



Dates do not have timezones, since they don't represent a time.

日期没有时区,因为它们不代表时间。

That said, as for howit calculates the current day, let's look at this extract from the code for Date.today:

也就是说,至于它如何计算当天,让我们看看以下代码的Date.today摘录:

time_t t;
struct tm tm;
// ...
if (time(&t) == -1)
  rb_sys_fail("time");
if (!localtime_r(&t, &tm))
  rb_sys_fail("localtime");

It then proceeds to use use tmto create the Dateobject. Since tmcontains the system's local time using localtime(), Date.todaytherefore uses the system's local time, not UTC.

然后继续使用 usetm来创建Date对象。由于使用tm包含系统的本地时间localtime()Date.today因此使用系统的本地时间,而不是 UTC。



You can always use Time#utcon any Timeconvert it in-place to UTC, or Time#getutcto return a new equivalent Timeobject in UTC. You could then call Time#to_dateon that to get a Date. So: some_time.getutc.to_date.

您始终可以使用Time#utconTime将其就地转换为 UTC,或Time#getutc以UTC返回新的等效Time对象。然后你可以调用Time#to_date它来获取Date. 所以: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.nowor Date.todaywork). See also ActiveSupport extensions to Time.

如果您正在使用ActiveSupport 的时区支持(包含在 Rails 中),请注意它与 Ruby 的时间构造函数完全分离并且不会影响它们(即它不会改变方式Time.nowDate.today工作方式)。另请参阅ActiveSupport 扩展到Time.

回答by Rich Drummond

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.

Date 的一个实例表示为一个天文儒略日数;它没有一天的小数部分。虽然儒略日与格林威治标准时间有关 - 因此从技术上讲,日期的实例应该被视为格林威治标准时间 - 在大多数情况下,您可以忽略它并将其视为没有时区。如果您将时区转换为 DateTime 或 Time,您将只关心时区。

ActiveSupport's tools for date conversion let you specify whether you want local time or UTC.

ActiveSupport 的日期转换工具可让您指定是需要本地时间还是 UTC。

E.g.:

例如:

>> t = Date.today.to_time
=> Wed Apr 18 00:00:00 -0400 2012
>> t.zone
=> "EDT"
>> t = Date.today.to_time(:utc)
=> Wed Apr 18 00:00:00 UTC 2012
>> t.zone
=> "UTC"

回答by bluehallu

If your rails applications is configured to run in time zone UTC then just use Time.zone.today, otherwise force it by using Time.now.utc.to_date

如果您的 rails 应用程序配置为在时区 UTC 中运行,则只需使用Time.zone.today,否则通过使用强制它Time.now.utc.to_date

回答by Arnold Roa

You can use

您可以使用

Date.current

To get the current date on the configured timezone.

获取配置时区的当前日期。