Here is a pure-Ruby answer.
Code
require 'date'
R1 = /
\s*-\s* # match a hypen optionally surrounded by whitespace
| # or
\s*to\s* # match 'to' optionally surrounded by whitespace
/x # free-spacing regex definition mode
R2 = /st|nd|rd|th # match one of the four pairs of letters
/x
def total_days(data)
data.lines.reduce(0) do |tot, str|
start_date, end_date = str.
chomp.
split(R1).
drop(1).
map { |s| Date.strptime(s.sub(R2, ''), '%d %b %Y') }
tot + (end_date - start_date).to_i
end
end
Example
data =<<-_
Company 1 - 11th Feb 2008 to 23rd Feb 2010
Company 2 - 14 May 2010 to 17 Oct 2014
Company 3 - 22 Dec 2014 to 14 Jan 2017
_
total_days(data)
#=> 3114
Explanation
See Date::strptime and (for date format codes) DateTime#strftime.
The steps are as follows.
a = data.lines
#=> ["Company 1 - 11th Feb 2008 to 23rd Feb 2010\n",
# "Company 2 - 14 May 2010 to 17 Oct 2014\n",
# "Company 3 - 22 Dec 2014 to 14 Jan 2017\n"]
The block variable tot is set to reduce's argument (0) and the first element of a is generated and passed to the block, becoming the value of the block variable str:
tot = 0
str = a.first
#=> "Company 1 - 11th Feb 2008 to 23rd Feb 2010\n"
The block calculation is now performed
a = str.chomp
#=> "Company 1 - 11th Feb 2008 to 23rd Feb 2010"
b = a.split(R1)
#=> ["Company 1", "11th Feb 2008", "23rd Feb 2010"]
c = b.drop(1)
#=> ["11th Feb 2008", "23rd Feb 2010"]
d = c.map { |s| Date.strptime(s.sub(R2, ''), '%d %b %Y') }
#=> [#<Date: 2008-02-11 ((2454508j,0s,0n),+0s,2299161j)>,
# #<Date: 2010-02-23 ((2455251j,0s,0n),+0s,2299161j)>]
In calculating d, the first element of c passed to the block is
s = c.first
# => "11th Feb 2008"
and the block calculation for that string is
g = s.sub(R2, '')
#=> "11 Feb 2008"
Date.strptime(g, '%d %b %Y')
#=> #<Date: 2008-02-11 ((2454508j,0s,0n),+0s,2299161j)>
Continuing,
start_date, end_date = d
#=> [#<Date: 2008-02-11 ((2454508j,0s,0n),+0s,2299161j)>,
# #<Date: 2010-02-23 ((2455251j,0s,0n),+0s,2299161j)>]
start_date
#=> #<Date: 2008-02-11 ((2454508j,0s,0n),+0s,2299161j)>
end_date
#=> #<Date: 2010-02-23 ((2455251j,0s,0n),+0s,2299161j)>
e = end_date - start_date
#=> (743/1) <rational>
f = e.to_i
#=> 743
tot + 743
#=> 743
f is number of days the person worked in the first job. The last value is the new value of the block variable tot, the cumulative number of days worked in all jobs processed so far.