使用Ruby从CSV减去时间 [英] Subtract Time from CSV using Ruby
本文介绍了使用Ruby从CSV减去时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想用Ruby从CSV数组中减去时间
Hi I would like to subtract time from a CSV array using Ruby
时间[0]是12:12:00 AM
时间[1]是12:12:01 AM
time[0] is 12:12:00AM
time[1] is 12:12:01AM
这是我的代码
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
有没有办法解决这个问题?
is there a way to fix this?
推荐答案
You can use Time#strptime to define the format of the parsed string.
在您的情况下,字符串为%I:%M:%S%p
.
In your case the string is %I:%M:%S%p
.
-
%I
= 12小时时间 -
%M
=分钟 -
%S
=秒 -
%p
= AM/PM指示器
%I
= 12 hour time%M
= minutes%S
= seconds%p
= AM/PM indicator
因此,请分析您的示例:
So to parse your example:
require 'time'
time = %w(12:12:00AM 12:12:01AM)
parsed_time = time.map { |t| Time.strptime(t, '%I:%M:%S%p').to_i }
parsed_time.last - parsed_time.first
=> 1
这篇关于使用Ruby从CSV减去时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文