Skip to content

Commit

Permalink
Five days from today: ttcal.Today() + ttcal.Period(years=5)
Browse files Browse the repository at this point in the history
  • Loading branch information
theotherbjorn committed May 22, 2020
1 parent 320497b commit fd7ed7f
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 3 deletions.
65 changes: 65 additions & 0 deletions tests/test_addition.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# -*- coding: utf-8 -*-
from __future__ import print_function
import ttcal


def test_period_cmp():
assert ttcal.Period(months=5) == ttcal.Period(months=5)
assert ttcal.Period(months=4) != ttcal.Period(months=5)
assert ttcal.Period(months=4) < ttcal.Period(months=5)
assert ttcal.Period(months=4) <= ttcal.Period(months=5)
assert ttcal.Period(months=5) > ttcal.Period(months=4)
assert ttcal.Period(months=5) >= ttcal.Period(months=4)


def test_period_add():
print("1m =", ttcal.Period(months=1))
print("1m + 1m =", ttcal.Period(months=1) + ttcal.Period(months=1))
assert ttcal.Period(months=1) + ttcal.Period(months=1) == ttcal.Period(months=2)


def test_add_month():
assert ttcal.Day(2020, 1, 30) + ttcal.Period(months=1) == ttcal.Day(2020, 2, 29)
assert ttcal.Day(2020, 1, 30) + ttcal.Period(months=2) == ttcal.Day(2020, 3, 30)
assert ttcal.Day(2020, 1, 30) + ttcal.Period(months=3) == ttcal.Day(2020, 4, 30)
assert ttcal.Day(2020, 1, 30) + ttcal.Period(months=4) == ttcal.Day(2020, 5, 30)
assert ttcal.Day(2020, 1, 30) + ttcal.Period(months=5) == ttcal.Day(2020, 6, 30)


def test_add_year_leap():
assert ttcal.Day(2020, 2, 29) + ttcal.Period(years=1) == ttcal.Day(2021, 2, 28)
assert ttcal.Day(2020, 2, 29) + ttcal.Period(years=2) == ttcal.Day(2022, 2, 28)
assert ttcal.Day(2020, 2, 29) + ttcal.Period(years=3) == ttcal.Day(2023, 2, 28)
assert ttcal.Day(2020, 2, 29) + ttcal.Period(years=4) == ttcal.Day(2024, 2, 29)
assert ttcal.Day(2020, 2, 29) + ttcal.Period(years=5) == ttcal.Day(2025, 2, 28)


def test_add_month_lastday():
jan1 = ttcal.Day(2020, 1, 1)
jan31 = ttcal.Day(2020, 1, 31)
feb29 = ttcal.Day(2020, 2, 29)
months1 = ttcal.Period(months=1)
months2 = ttcal.Period(months=2)
months3 = ttcal.Period(months=3)
months4 = ttcal.Period(months=4)
months5 = ttcal.Period(months=5)
years1 = ttcal.Period(years=1)

print("jan1 + 1month=", jan1 + months1)
assert jan1 + months1 == ttcal.Day(2020, 2, 1)
assert feb29 + years1 == ttcal.Day(2021, 2, 28)
assert feb29 + months1 == ttcal.Day(2020, 3, 29)
assert jan31 + months1 == feb29
assert jan31 + months2 == ttcal.Day(2020, 3, 31)
assert jan31 + months3 == ttcal.Day(2020, 4, 30)
assert jan31 + months4 == ttcal.Day(2020, 5, 31)
assert jan31 + months5 == ttcal.Day(2020, 6, 30)

# assert ttcal.Period(months=1) + ttcal.Period(months=1) == ttcal.Period(months=2)

print("feb29 + months1 + months1 = ", feb29 + (months1 + months1))
assert feb29 + (months1 + months1) == ttcal.Day(2020, 4, 29)

print("(d + 1m) + 1m =", (feb29 + months1) + months1)
assert feb29 + months1 == ttcal.Day(2020, 3, 29)
assert (feb29 + months1) + months1 == ttcal.Day(2020, 4, 29)
2 changes: 1 addition & 1 deletion ttcal/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"""
__version__ = '1.0.6'
from .day import Day, Days, Today
from .duration import Duration
from .duration import Duration, Period
from .calfns import chop, isoweek
from .month import Month
from .week import Week
Expand Down
4 changes: 3 additions & 1 deletion ttcal/day.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import re
import six
from .calfns import rangecmp, rangetuple
from .duration import Duration
from .duration import Duration, Period


class fstr(str):
Expand Down Expand Up @@ -228,6 +228,8 @@ def datetuple(self):
return self.year, self.month, self.day

def __add__(self, n):
if isinstance(n, Period):
return n.add_to_day(Day, self)
return Day.fromordinal(self.toordinal() + n)

# make first and last properties, because
Expand Down
38 changes: 37 additions & 1 deletion ttcal/duration.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,44 @@
import re


class Period(object):
"""A semantic time period which doesn't need to be of fixed duration,
e.g. a month or a year.
"""
def __init__(self, years=0, months=0):
self.months = months + 12 * years

def add_to_day(self, cls, d):
ym = d.Month + self.months
return cls(ym.year, ym.month, min(d.day, ym.daycount))

def __repr__(self):
if self.months >= 12:
return "Period(%d years, %d months)" % divmod(self.months, 12)
else:
return "Period(%d months)" % self.months

def __add__(self, other):
return Period(months=self.months + other.months)

def __eq__(self, other):
return self.months == other.months

def __neq__(self, other):
return self.months != other.months

def __gt__(self, other):
return self.months > other.months

def __ge__(self, other):
return self.months >= other.months

def __lt__(self, other):
return self.months < other.months


class Duration(datetime.timedelta):
"""A duration of time.
"""A fixed duration of time.
"""

@classmethod
Expand Down

0 comments on commit fd7ed7f

Please sign in to comment.