Tag: 时间

找出当前时间是否介于两次之间

我有两个时间列存储在Postgresql数据库中: open_time和close_time 。 我试图找出当前时间,忽略日期,是否在两次之间,忽略了日期。 此代码比较日期和时间: current_time = Time.now if current_time.between?(store.open_time, store.close_time) puts “IN BETWEEN” end 例如,当current_time # => 2018-06-06 23:59:49 -0600和open_time # => 2000-01-01 22:59:00 UTC时,它不起作用。 如何让它不包括日期,只是比较时间?

使用Ruby从CSV中减去时间

嗨,我想使用Ruby从CSV数组中减去时间 时间[0]是12:12:00 AM 时间[1]是12:12:01 AM 这是我的代码 time_converted = DateTime.parse(time) difference = time_converted[1].to_i – time_converted[0].to_i p difference 但是,我得到了0 p time[0].to_i给了我12 有没有办法来解决这个问题?

如何使用Ruby将数组转换为64位双Mac绝对时间?

是否可以将存储为每个元素的二进制或hex值转换为64位双麦绝对时间? 当我使用p var_bytes控制台检查数组时,控制台显示以下输出。 \000\000\000\000\000\000\000\000\000\000\000\234\225x\266A\000\000\000\345\005\230\264 是否可以将64位双麦克风绝对时间中的上述数组元素转换为字符串? 我的代码只是一个简单的do..end puts “\nClose off the page header#{y.unpack(“n”)}\n” z.scan(/(.{8})(.{8})(.{4})(.{4})(.{4})(.{4})(.{8})(.{8})(.{8})(.*\w)/m).each do |j,k,l,m,n,o,p,q,r,s| puts “\nContent1#{j.unpack(“n”)}\n” puts “\nContent2:#{k.unpack(“n”)}\n” puts “\nContent3:#{l.unpack(“n”)}\n” puts “\nContent4:#{m.unpack(“n”)}\n” puts “\nContent5:#{n.unpack(“n”)}\n” puts “\nContent6:#{o.unpack(“n”)}\n” puts “\nContent7:#{p.unpack(“n”)}\n” expdt = Time.at((q.unpack(“L”))[0]) createdt = Time.at((r.unpack(“L”))[0]) puts “Date1:\n#{expdt}\n” puts “\nDate2:\n#{createdt}\n” puts “\nCookie:\n” puts s.split(/\0/m) end end 将这个负值转换为正数的简单方法是什么,所以Time.at不会给出错误,然后根据MAC Epoch时间转换它?

使用Rails和Mysql的毫秒数

使用Rails + Mysql时,以毫秒为单位存储时间的最佳方法是什么? 我将使用decimal和composed_of,以便能够将此值作为Ruby Time进行操作。 有没有人有更好的主意?!

Rails:时区之间的差异,以小时为单位

我希望能够说user1 is 4 hours ahead of user2 ,根据用户在其帐户中指定的时区计算。 使用以下代码: time1 = Time.zone.now.in_time_zone(user1.time_zone) time2 = Time.zone.now.in_time_zone(user2.time_zone) distance_of_time_in_words time1,time2 …给出less than a minute的差异 – 同样减去两次给出0 。 Rails显然仍然认为这两次是相同的。 知道如何计算两个时区之间的差异吗?

为什么Time.strptime()返回当前日期?

向私有(无公共文档)API发出GET请求以JSON格式返回数据。 我感兴趣的价值是日期。 它以ASP.NET JSON日期格式返回日期。 这是它的样子: AanmeldDatum: “/Date(1406675114000+0200)/” 还有一个名为AangebodenSindsTekst的变量,意思是OfferedSinceText,它的值是“8 augustus 2014”。 因此,未知的Date格式应该被解析为该特定值。 我试过这个: require ‘time’ foo = Time.strptime(‘1406675114000+0200’, ‘%N’) # => 2014-08-12 13:38:46 +0200 foo = Time.strptime(‘1406675114000+0200’, ‘%N%z’) # => 2014-08-12 14:38:58 +0200 但它只是返回当前时间。 我知道100%肯定/Date(1406675114000+0200)/解析时,应该返回日期2014-07-30 。 问题是我该如何确定它呢?

在Ruby中将Time类对象转换为RFC3339

Google Calendar API(v2)的与时间相关的查询必须采用RFC3339格式。 当我在’require’时间”之后查找Time类时,我看不到rfc3339方法。

如何比较ruby的时间

我需要使用Time对象作为int(TimeObject.to_i)然后我需要将int转换回Time,以与原始Time进行比较。 简短的例子 t1 = Time.now t2 = Time.at(t1.to_i) puts t1 == t2 # Says False puts t1.eql?(t2) # Says False 为什么这说错呢? 当我同时打印时,objetcs显示同样的事情D: puts t1 #shows : 2012-01-06 16:01:53 -0300 puts t2 #shows : 2012-01-06 16:01:53 -0300 puts t1.to_a.to_s #shows : [ 53, 1, 16, 6, 1, 2012, 5, 6, true, “CLST”] puts t2.to_a.to_s #shows : […]

Ruby中字符串的平均次数

我有一个数组: array = [“0:00:31”, “0:00:52”, “0:01:05”, “0:00:55”, “0:01:33”, “0:00:05”, “0:00:01”, “0:05:10”, “0:02:40”, “0:03:03”, “0:01:33”, “0:00:00″] 我需要平均数组中不等于”0:00:00” 。 “0:00:00″应该被抛弃。 最好的方法是什么? 我正在循环遍历数组,删除所有0:00:00值,将字符串转换为整数并做一个平均值 – 但它似乎是我想要做的很多代码。

Ruby – 将格式化日期转换为时间戳

我需要将日期字符串转换为Unix时间戳格式。 我从API获得的字符串如下所示: 2015-05-27T07:39:59Z 与.tr()我得到: 2015-05-27 07:39:59 这是一种非常规则的日期格式。 尽管如此,Ruby无法将其转换为Unix TS格式。 我试过.to_time.to_i但我一直得到NoMethodError错误。 在PHP中,函数strtotime()只对此有效。 Ruby有一些类似的方法吗?