ruby 中的时间操作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10056066/
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 04:59:05  来源:igfitidea点击:

Time manipulation in ruby

rubytime

提问by FlyingFoX

I want to create a DateTime instance that lies 20 minutes and 10 seconds in the future. I tried around with Time and DateTime in irb, but can't seem to figure out a way that really makes sense. I can only add days to DateTime objects and only add seconds to the Time objects.

我想创建一个位于未来 20 分 10 秒的 DateTime 实例。我在 irb 中尝试使用 Time 和 DateTime,但似乎无法找到真正有意义的方法。我只能向 DateTime 对象添加天数,并且只能向 Time 对象添加秒数。

Isn't there a better way than to always convert the time I want to add into seconds?

没有比总是将我想要添加的时间转换为秒更好的方法吗?

回答by Frederick Cheung

A Timeis a number of seconds since an epoch whereas a DateTimeis a number of days since an epoch which is why adding 1to a DateTimeadds a whole day. You can however add fractions of a day, for example

ATime是自一个纪元以来的秒数,而 aDateTime是自一个纪元以来的天数,这就是为什么添加1到 aDateTime会增加一整天。但是,您可以添加一天的分数,例如

d = DateTime.now
d + Rational(10, 86400)

Will add 10 seconds to d(since there are 86400 seconds in a day).

将增加 10 秒d(因为一天有 86400 秒)。

If you are using Rails, Active Support adds some helper methods and you can do

如果您使用的是 Rails,Active Support 会添加一些辅助方法,您可以这样做

d + 20.minutes + 10.seconds

Which will do the right thing is dis a DateTimeor a Time. You can use Active Support on its own, and these days you can pull in just the bits you need. I seem to recall that this stuff is in activesupport/duration. I believe there are a few other gems that offer help with time handling too.

哪个会做正确的事情d是 aDateTime或 a Time。您可以单独使用 Active Support,现在您可以只获取您需要的部分。我好像记得这个东西在activesupport/duration. 我相信还有其他一些宝石也可以帮助处理时间。

回答by alex

Assuming you have required Active Support or you're working in a Rails project. A very simple and readable way to do this in Ruby is:

假设您需要 Active Support 或者您在 Rails 项目中工作。在 Ruby 中执行此操作的一种非常简单易读的方法是:

DateTime + 5.minutes
Time + 5.minutes

Also works with seconds, hours, days, weeks, months, years.

也适用于seconds, hours, days, weeks, months, years

回答by seo

Just use the Active Support Time extensions. They are very convenient and less error prone than trying to do this by hand. You can import just the module you need:

只需使用活动支持时间扩展。与尝试手动执行此操作相比,它们非常方便且不易出错。您可以只导入您需要的模块:

# gem 'activesupport'
require 'active_support/core_ext/numeric/time.rb'
DateTime.now + 20.minutes


N.B: Yes, this goes against the StackOverflow party line of staying away from 3rd party libraries, but you shouldn't avoid using libraries when they are practically standard, reduce your risk significantly, and provide better code clarity.

注意:是的,这与远离 3rd 方库的 StackOverflow 党路线背道而驰,但是当它们实际上是标准的、显着降低风险并提供更好的代码清晰度时,您不应该避免使用库。

回答by wondersz1

As noted above, you can add secondsto a Time object, so if you call to_timeon a DateTime object, you can add seconds to it:

如上所述,您可以添加seconds到 Time 对象,因此如果您调用to_timeDateTime 对象,则可以向其添加秒数:

DateTime.strptime("11/19/2019 18:50","%m/%d/%Y %H:%M") + 1 => adds a day

(DateTime.strptime("11/19/2019 18:50","%m/%d/%Y %H:%M").to_time) +1 => adds a second

This doesn't require adding gems.

这不需要添加宝石。

回答by rodvlopes

Pure Ruby (no Rails)

纯 Ruby(无 Rails)

class Numeric
  def minutes; self/1440.0 end
  alias :minute :minutes

  def seconds; self/86400.0 end
  alias :second :seconds
end

Where 1440 is the number of minutes and 86400 is the number of seconds in a day. Based on how Rails does.

其中 1440 是分钟数,86400 是一天中的秒数。基于 Rails 的做法。

Then you can just let the magic happen:

然后你就可以让魔法发生:

d + 20.minutes + 10.seconds

Source: https://github.com/rails/rails/blob/v6.0.3.1/activesupport/lib/active_support/core_ext/numeric/time.rb

来源:https: //github.com/rails/rails/blob/v6.0.3.1/activesupport/lib/active_support/core_ext/numeric/time.rb