Add work hours calc

This commit is contained in:
Anton Vakhrushev 2019-11-03 12:23:17 +03:00
parent e236a1bdac
commit 3195a14bb3
3 changed files with 46 additions and 5 deletions

View File

@ -0,0 +1,6 @@
[
{
"start": "2019-01-01 10:00:00",
"finish": "2019-01-01 20:00:00"
}
]

View File

@ -2,7 +2,18 @@ require "./spec_helper"
describe Dayoff do describe Dayoff do
it "can calc work hours" do it "can calc work hours" do
app = Dayoff::App.new("./spec/data/planned-dates.json") app = Dayoff::App.new(
app.get_work_hours.should eq 20 "./spec/data/planned-dates.json",
"./spec/data/work-records.json",
)
app.get_planned_hours.should eq 20
end
it "can calc work hours" do
app = Dayoff::App.new(
"./spec/data/planned-dates.json",
"./spec/data/work-records.json",
)
app.get_work_hours.should eq 10
end end
end end

View File

@ -10,20 +10,44 @@ module Dayoff
) )
end end
class WorkRecord
JSON.mapping(
start: String,
finish: String,
)
end
class App class App
def initialize(pddates_path) def initialize(pdates_path, wdates_path)
content = File.open(pddates_path) do |file| content = File.open(pdates_path) do |file|
file.gets_to_end file.gets_to_end
end end
@pdates = Array(PlannedDate).from_json(content) @pdates = Array(PlannedDate).from_json(content)
content = File.open(wdates_path) do |file|
file.gets_to_end
end
@wrecords = Array(WorkRecord).from_json(content)
end end
def get_work_hours def get_planned_hours
sum = 0 sum = 0
@pdates.each do |wd| @pdates.each do |wd|
sum += wd.hours sum += wd.hours
end end
sum sum
end end
def get_work_hours
sum = 0
location = Time::Location.load("Europe/Moscow")
@wrecords.each do |wr|
s = Time.parse(wr.start, "%Y-%m-%d %H:%M:%S", location)
f = Time.parse(wr.finish, "%Y-%m-%d %H:%M:%S", location)
diff = f - s
sum += diff.total_hours.to_i32
end
return sum
end
end end
end end