Add work hours calc
This commit is contained in:
parent
e236a1bdac
commit
3195a14bb3
6
spec/data/work-records.json
Normal file
6
spec/data/work-records.json
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
[
|
||||||
|
{
|
||||||
|
"start": "2019-01-01 10:00:00",
|
||||||
|
"finish": "2019-01-01 20:00:00"
|
||||||
|
}
|
||||||
|
]
|
@ -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
|
||||||
|
@ -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
|
||||||
|
Loading…
Reference in New Issue
Block a user