Ruby Date Time Subtraction - ruby

I am trying to calculate the exact duration a process took from some log file result. After parsing the log, I reached at the following stage:
my_array = ["Some_xyz_process", "Start", "2018-07-12", "12:59:53,397", "End", "2018-07-12", "12:59:55,913"]
How can I subtract the start date and time from the end date and time in order to retrieve the exact duration the process took?

my_array = ["Some_xyz_process",
"Start", "2018-07-12", "12:59:53,397",
"End", "2018-07-12", "12:59:55,913"]
require 'date'
fmt = '%Y-%m-%d%H:%M:%S,%L'
is = my_array.index('Start')
#=> 1
ie = my_array.index('End')
#=> 4
DateTime.strptime(my_array[ie+1] + my_array[ie+2], fmt).to_time -
DateTime.strptime(my_array[is+1] + my_array[is+2], fmt).to_time
#=> 2.516 (seconds)
See DateTime#strptime and DateTime# (the latter for format directives). As long as the date and time formats are known I always prefer strptime to parse. Here's an example of why:
DateTime.parse 'Theresa May has had a bad week over Brexit'
#=> #<DateTime: 2018-05-01T00:00:00+00:00 ((2458240j,0s,0n),+0s,2299161j)>`.

You can concat the date and time field and use Time.parse to convert it to a time object and then calculate the difference in number of seconds
Time.parse('2018-07-12 12:59:55,397').to_i - Time.parse('2018-07-12 12:59:53,913').to_i
Hope this helps

Related

compare times with milliseconds

I have two dates a start date and an end date. I want to get a new time object which is the difference between the two. The differences I am concerned with are Hours, Minutes, Seconds and Milliseconds. I need to be able to create a new Time object from the result that includes the milliseconds difference
>> require 'time'
=> true
>> start_time = Time.parse '1970-01-01T00:00:00.200'
=> 1970-01-01 00:00:00 +0000
>> end_time = Time.parse '1970-01-01T01:01:01.400'
=> 1970-01-01 01:01:01 +0000
>> difference = Time.at(end_time - start_time)
=> 1970-01-01 01:01:01 +0000
my problem is that difference does not have the milliseconds
I can see that the Time has milliseconds by running
>> difference.strftime('%H:%M:%S.%L')
=> "01:01:01.199"
but how do I access the milliseconds that are in the Time difference object.
it is critical I have milliseconds as I am working in sub-second calculations?
UPDATE
I don't think my first attempt at this question was as descriptive as it should of been, my apologies for that.
require 'time'
a = Time.now
sleep(0.5)
b = Time.now
b - a
# => 0.505087
Milliseconds!
EDIT: Microseconds!
my problem is that difference does not have the milliseconds
It does have the milliseconds, Time#to_s / Time#inspect just doesn't show it. Its output is equivalent to: strftime "%Y-%m-%d %H:%M:%S %z"
how do I access the milliseconds that are in the Time difference object.
usec returns the microseconds and nsec returns the nanoseconds:
time = Time.at(0.2)
time.usec #=> 200000
time.nsec #=> 200000000
For milliseconds you could use
time.usec / 1000 #=> 200
Ruby's Time class has nanosecond precision: you can use Time#to_f to get a fractional number of seconds since the Unix epoch. If you subtract two Time objects, you'll get a fractional number of seconds between them. Thus, to get the number of milliseconds between two times, try:
((time2 - time1) * 1000).to_i

Error using strptime, strftime while converting date to epoch

I am trying to extract dates from csv file and convert them to epoch time
CSV.foreach(File.path("month.csv")) do |row|
dateper=row[0].split(',')[0]
p DateTime.strptime(dateper,"%m/%d/%y %I:%M:%S %p %z").strftime("%s")
end
I am getting invalid date (ArgumentError) as the result. However, if I print out dateper, choose one of the outputs randomly and cop-paste it in place of dateper in line 3, I get an epoch value. What am I doing wrong? dateper is of string value, and strptime minus strftime seems to give an output.
month.csv sample:
7/26/13 12:00:00 AM -05:00,62.2,63.02,62.07,63.02,5.00168E+07
8/23/13 12:00:00 AM -05:00,71.84,71.93,71.36,71.6,5.55304E+07
8/26/13 12:00:00 AM -05:00,71.56,72.91,71.52,71.87,8.26536E+07
8/27/13 12:00:00 AM -05:00,71.16,71.81,69.49,69.82,1.058488E+08
Could someone help me with this?
You have some malformed dates somewhere. Use rescue to avoid the script choking on them.
CSV.foreach(File.path("month.csv")) do |row|
dateper=row[0].split(',')[0]
p DateTime.strptime(dateper,"%m/%d/%y %I:%M:%S %p %z").strftime("%s") rescue p "malformed date:#{dateper} on line #{$.}"
end
This will allow the script to continue and show you the bad dates.

How do I get a range or array for every minute in a day in Ruby 1.9.3?

I know there is the Date#step method, however it wants days for steps. I need a range or array for every minute in a given day (1440 entries).
What's the best, and most effeicient way to do this in Ruby 1.9.3?
Ultimately, I'm going to format the output to be used like this:
00:00:00
00:01:00
00:02:00
...
23:59:00
This might get you started:
0.upto((60 * 24) - 1).each { |m| puts "%02d:%02d:00" % [m / 60, m % 60] }
You can step anyway:
require 'date'
today = Date.today.to_datetime
tomorrow = today+1
min = 1.0/(24*60)
today.step(tomorrow, min){|d| p d.strftime("%H:%M:%S")}

ROR + Ruby Date From XML API

By using XML API, I got date-time as "2008-02-05T12:50:00Z". Now I wanna convert this text format into different format like "2008-02-05 12:50:00". But I am getting proper way.
I have tried this one :: #a = "2008-02-05T12:50:00Z"
Steps
1. #a.to_date
=> Tue, 05 Feb 2008
2. #a.to_date.strftime('%Y')
=> "2008"
3. #a.to_date.strftime('%Y-%m-%d %H:%M:%S')
=> "2008-02-05 00:00:00
Suggest some thing ?
The to_date method converts your string to a date but dates don't have hours, minutes, or seconds. You want to use DateTime:
require 'date'
d = DateTime.parse('2008-02-05T12:50:00Z')
d.strftime('%Y-%m-%d %H:%M:%S')
# 2008-02-05 12:50:00
Use Ruby's DateTime:
DateTime.parse("2008-02-05T12:50:00Z") #=> #<DateTime: 2008-02-05T12:50:00+00:00 (353448293/144,0/1,2299161)>
From there you can output the value in any format you want using strftime. See Time#strftime for more info.

ROR + Ruby Date Decrease by 15 minutes

If I have #time = Time.now.strftime("%Y-%m-%d %H:%M:%S"),
How can I reduce this time by 15 minutes ?
I already tried this one :: #reducetime = #time-15.minutes, works fine at console but give errors while execution. Other than this Is there any way to resolve this issue.
Thanks
Your problem is that you're formatting your time into a string before you're done treating it as a time. This would make more sense:
#time = Time.now
#reducetime = #time - 15.minutes
# And then later when you're reading to display #time...
formatted_time = #time.strftime("%Y-%m-%d %H:%M:%S")
You shouldn't format your data until right before you're ready to display it.
If you must have #time as the formatted time then you're going to have to parse it before computing #reducetime:
#reducetime = (DateTime.strptime(#time, "%Y-%m-%d %H:%M:%S") - 15.minutes).to_time

Resources