草庐IT

datetime2

全部标签

ruby - 从 UTC 秒数创建 DateTime

我不敢相信我在任何地方都找不到这个,但是你怎么能从UTC秒创建一个DateTime?类似于在Java中,您可以通过Calendar.getInstance().setTimeInMillis(time)获取特定时间。DateTime.new1344899588没有工作,我在DateTimeAPI文档中没有看到任何说明如何执行此操作的内容。这行得通,但似乎是一种迂回的方式:DateTime.strptime(1344899588.to_s,"%s") 最佳答案 您可以为此使用Time类,即t=Time.at(1344899588)如果

ruby - 如何从 Ruby 中的 DateTime 中减去 n 个月?

我是ruby​​的新手,日期算术看起来很困惑。如何在不使用任何其他模块的情况下从DateTime中删除n个月? 最佳答案 自DateTime是Date的子类,你可以使用或prev_month:require'date'd=DateTime.now#=>#d#d.prev_month(4)#=>#请注意DateTime不考虑夏令时。 关于ruby-如何从Ruby中的DateTime中减去n个月?,我们在StackOverflow上找到一个类似的问题: https

ruby - Podio:设置 DateTime 字段值时使用哪个时区

使用跑道API创建新项目或更新现有项目时,将DateTime字段值设置为:2016-10-2114:15:00(例如)。将使用哪个时区来存储此DateTime?例如要求:app_id=content={'title'=>'Datesetto"14:15"','date'=>{'start'=>'2016-10-2114:15:00','end'=>'2016-10-2115:00:00'}}item=Podio::Item.create(app_id,'fields'=>content)结果:'start_date_utc'=>2016-10-21'end'=>2016-10-2115

ruby - rspec - 如何与预期中的实际 DateTime 进行比较?

我的rspec:it"canshowthecurrentmonthname"doexpect(Calendar.create_date_using_month(1)).toeq'2000-01-0100:00:00-0500'end失败:expected:"2000-01-0100:00:00-0500"got:2000-01-0100:00:00-0500对于我的代码:defself.create_date_using_month(n)Time.new(2000,n,1)end我应该/可以更改RSpec以便与实际字符串而不是日期进行比较吗?我试过:Date.strptime("{20

ruby-on-rails - 将 Rails DateTime 四舍五入到最接近的 15 分钟间隔

我需要将DateTime和Time舍入到最接近的15分钟间隔。我的想法是将秒和毫秒归零(它们是否存在于DateTime或Time中?)甚至纳秒?然后将分钟数除以15,四舍五入,然后将结果乘以15并将其设置为分钟数:#zerooutthesecondstime-=time.sec.seconds#zerooutthemilliseconds(doesthatexist?)#zerooutthenanoseconds(lesslikelythisexists)minutes_should_be=(time.min/15.to_f).round*15time+=(minutes_should

ruby - 如何将 apache 日志时间转换为 activerecord :datetime

我想通过ActiveRecord将apache日志保存到MySQL。在apache日志中,默认时间字符串如下:[13/Aug/2008:00:50:49-0700]如何将其转换为ActiveRecord:datetime类型?谢谢! 最佳答案 apache_time="[13/Aug/2008:00:50:49-0700]"d=DateTime.strptime(apache_time,"[%d/%b/%Y:%H:%M:%S%Z]")格式字符串记录在此处:http://ruby-doc.org/core/classes/Time.s

ruby-on-rails - 在 ActiveRecord 中使用范围返回多个 DateTime 范围内的结果

我有一个Session模型,它有一个:created_at日期和一个:start_time日期,它们都存储在数据库中:时间。我目前在一个巨大的表上吐出一堆结果,并允许用户使用范围按单个日期和可选的时间范围过滤结果,如下所示:classSessionDateTime.strptime(date,'%m/%d/%Y')..DateTime.strptime(date,'%m/%d/%Y').end_of_day)}scope:filter_by_time,lambda{|date,time|to=time[:to]from=time[:from]where(:start_time=>Dat

Ruby DateTime.Parse 到本地时间

我有一个日期字符串20101129220021,所以我将使用require'date'd=DateTime.parse('20101129220021')这部分工作正常,我得到一个日期,它是UTC。我的问题是,如何将其转换为本地时间?我尝试了很多方法,例如使用d.to_time提取时间部分并操作结果,但它没有用。据我所知,DateTime对象是不可变的。我能得到一些帮助吗? 最佳答案 irb(main):001:0>require"date"=>trueirb(main):002:0>d=DateTime.parse('201011

ruby - ruby 中的 Datetime.civil 和 Datetime.new 有区别吗?

你好,我一直在互联网上搜索,看到有很多ruby​​方法的别名做同样的事情(我不知道为什么)所以我很困惑,ruby中的Datetime.civil和Datetime.new之间有区别吗? 最佳答案 根据thedocumentation,他们是一样的。还有:DateTime.method(:new)==DateTime.method(:civil)=>true 关于ruby-ruby中的Datetime.civil和Datetime.new有区别吗?,我们在StackOverflow上找到一

datetime - 两个 time.Time 对象之间的差异

对“Go”非常陌生。问题可能是基本问题。我有两个time.Time对象,我想在小时/分钟/秒方面获得两者之间的差异。让我们说:t1=2016-09-0919:09:16+0530ISTt2=2016-09-0919:09:16+0530IST在上述情况下,因为差异是0。它应该给我00:00:00。考虑另一种情况:t1=2016-09-1414:12:48+0530ISTt2=2016-09-1414:18:29+0530IST在这种情况下,差异将是00:05:41。我看了https://godoc.org/time但无法从中得到任何东西。 最佳答案