Skip to content

Commit

Permalink
Some naming changes
Browse files Browse the repository at this point in the history
  • Loading branch information
manso92 committed May 17, 2020
1 parent 2fd993e commit bdb64a0
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 31 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.1.0
0.2.0
18 changes: 11 additions & 7 deletions tests/test_initial.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import unittest

import numpy as np
from datetime import timedelta, datetime, time
from workingtime import _dates_between_dates
from workingtime import workingtime
from datetime import timedelta, datetime
from workingtime.workingtime import _dates_between_dates
from workingtime import WorkingTime, Time


class InitialTest(unittest.TestCase):

Expand All @@ -16,14 +17,17 @@ def test_dates_between_dates(self):
np.testing.assert_array_equal(
_dates_between_dates(now, now + day), [])
np.testing.assert_array_equal(
_dates_between_dates(now, now+ 2*day),
_dates_between_dates(now, now + 2 * day),
[now + day])

def test_another_test(self):
st = workingtime()
wt = WorkingTime()
np.testing.assert_equal(
st.workingtime(datetime(2020, 10, 3, 1), datetime(2020, 10, 4, 23), (time(22, 0), time(0, 30))).total_seconds(),
(2.5 + 1)*3600)
wt.working_time(datetime(2020, 10, 3, 1),
datetime(2020, 10, 4, 23),
(Time(22, 0), Time(0, 30))).total_seconds(),
(2.5 + 1) * 3600)


if __name__ == '__main__':
print()
Expand Down
2 changes: 1 addition & 1 deletion workingtime/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"""
import errno as _errno

from .workingtime import _dates_between_dates, workingtime, time
from .workingtime import WorkingTime, Time

import os as _os

Expand Down
52 changes: 30 additions & 22 deletions workingtime/workingtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,21 @@ def overlap(x1, x2, y1=None, y2=None):
return x2 - x1

if y1 is None:
y1 = time.min
y1 = Time.min

if x1 == x2 or y1 == y2:
return timedelta(0)

if y2 is None:
y2 = time.max
y2 = Time.max

if not _is_overlapping(x1, x2, y1, y2):
raise ValueError("Intervals does not overlaps")

return min(x2, y2) - max(x1, y1)


class time(dt.time):
class Time(dt.time):
def seconds(self):
return self.hour * 3600 + self.minute * 60 + self.second

Expand All @@ -45,50 +45,58 @@ def from_seconds(seconds):
secs = seconds % 60
mins = int(seconds / 60) % 60
hours = int(seconds / 3600) % 24
return time(hours, mins, secs)
return Time(hours, mins, secs)

@staticmethod
def from_time(dtime):
return time(dtime.hour, dtime.minute, dtime.second)
return Time(dtime.hour, dtime.minute, dtime.second)

@staticmethod
def from_dt(dtime):
return time.from_time(dtime.time())
return Time.from_time(dtime.time())

def __sub__(self, other):
return timedelta(seconds=self.seconds() - other.seconds())

def __rsub__(self, other):
return time.from_time(other) - self
return Time.from_time(other) - self


class workingtime:
class WorkingTime:
def __init__(self, weekends=None, holidays=None):
# TODO check the variables
self.weekends = weekends
self.holidays = holidays

def workingtime(self, start, end, workhours=None):
if workhours is None:
workhours = (time(8), time(16))

workhours = (time.from_time(workhours[0]), time.from_time(workhours[1]))
def working_time(self, start, end, work_hours=None):
if work_hours is None:
work_hours = (Time(8), Time(16))

work_hours = (Time.from_time(work_hours[0]),
Time.from_time(work_hours[1]))

# If start > end, workingtime will be negative
if start > end:
return -1 * self.workingtime(end, start, workhours)
# If the day changes between your workhours, its easier to calculate the time your note working and substract
if workhours[0] > workhours[1]:
return (end - start) - self.workingtime(start, end, (workhours[1], workhours[0]))
return -1 * self.working_time(end, start, work_hours)

# If the day changes between your workhours, its easier to calculate
# the time your note working and substract
if work_hours[0] > work_hours[1]:
return ((end - start) -
self.working_time(start, end,
tuple(reversed(work_hours))))

# If the start time and the end time are the same we return the workhours * days between
# But it can't be done like this because start date or end date could be weekend or holiday
# If the start time and the end time are the same we
# return the workhours * days between. But it can't be done
# like this because start date or end date could be weekend or holiday

if start.date() == end.date():
# TODO implementar esta cosica
return overlap(time.from_dt(start), time.from_dt(end), *workhours)
return overlap(Time.from_dt(start), Time.from_dt(end), *work_hours)

middle_time = len(_dates_between_dates(start, end)) * (workhours[1] - workhours[0])
middle_time = (len(_dates_between_dates(start, end)) *
(work_hours[1] - work_hours[0]))

return overlap(*workhours, time.from_dt(start)) + middle_time + overlap(*workhours, None, time.from_dt(end))
return (overlap(*work_hours, Time.from_dt(start)) +
middle_time +
overlap(*work_hours, None, Time.from_dt(end)))

0 comments on commit bdb64a0

Please sign in to comment.