From 3195a14bb3c6a5fb48efbd3104a86715c10b8315 Mon Sep 17 00:00:00 2001 From: Anton Vakhrushev Date: Sun, 3 Nov 2019 12:23:17 +0300 Subject: [PATCH] Add work hours calc --- spec/data/work-records.json | 6 ++++++ spec/dayoff_spec.cr | 15 +++++++++++++-- src/dayoff.cr | 30 +++++++++++++++++++++++++++--- 3 files changed, 46 insertions(+), 5 deletions(-) create mode 100644 spec/data/work-records.json diff --git a/spec/data/work-records.json b/spec/data/work-records.json new file mode 100644 index 0000000..9c2df8d --- /dev/null +++ b/spec/data/work-records.json @@ -0,0 +1,6 @@ +[ + { + "start": "2019-01-01 10:00:00", + "finish": "2019-01-01 20:00:00" + } +] \ No newline at end of file diff --git a/spec/dayoff_spec.cr b/spec/dayoff_spec.cr index c8f1aa9..b389254 100644 --- a/spec/dayoff_spec.cr +++ b/spec/dayoff_spec.cr @@ -2,7 +2,18 @@ require "./spec_helper" describe Dayoff do it "can calc work hours" do - app = Dayoff::App.new("./spec/data/planned-dates.json") - app.get_work_hours.should eq 20 + app = Dayoff::App.new( + "./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 diff --git a/src/dayoff.cr b/src/dayoff.cr index 18b1afc..5505bbe 100644 --- a/src/dayoff.cr +++ b/src/dayoff.cr @@ -10,20 +10,44 @@ module Dayoff ) end + class WorkRecord + JSON.mapping( + start: String, + finish: String, + ) + end + class App - def initialize(pddates_path) - content = File.open(pddates_path) do |file| + def initialize(pdates_path, wdates_path) + content = File.open(pdates_path) do |file| file.gets_to_end end @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 - def get_work_hours + def get_planned_hours sum = 0 @pdates.each do |wd| sum += wd.hours end sum 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