Commit 40f3ff87 authored by Guillaume Michon's avatar Guillaume Michon

Added getAccountableYearFraction() and roundDate()


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@5877 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent b6e1df28
...@@ -39,9 +39,10 @@ number_of_days_in_year = 365. ...@@ -39,9 +39,10 @@ number_of_days_in_year = 365.
hour = 1/24. hour = 1/24.
same_movement_interval = hour same_movement_interval = hour
accountable_days_in_month = 30.
accountable_months_in_year = 12.
#def addToDate(date, to_add={'year':0, 'month':0, 'day':0, 'hour':0, 'minute':0, 'second':0},year=0,month=0,day=0,
# hour=0,minute=0,second=0):
def addToDate(date,to_add=None, **kw): def addToDate(date,to_add=None, **kw):
""" """
Return a new DateTime object with the corresponding added values. Return a new DateTime object with the corresponding added values.
...@@ -235,6 +236,35 @@ def getYearFraction(days=None, months=None, days_in_year=number_of_days_in_year) ...@@ -235,6 +236,35 @@ def getYearFraction(days=None, months=None, days_in_year=number_of_days_in_year)
return days / days_in_year return days / days_in_year
def getAccountableYearFraction(from_date=None, to_date=None):
"""
Returns a year fraction according to accounting rules,
i.e. 30 days per month
"""
from_date = roundDate(from_date)
to_date = roundDate(to_date)
months = getMonthAndDaysBetween(from_date, to_date)['month']
days = getMonthAndDaysBetween(from_date, to_date)['day']
new_from_date = addToDate(from_date, month=months)
if days != 0:
if new_from_date.month() == to_date.month():
days_before = new_from_date.day() - 1
else:
days_before = days_before = (accountable_days_in_month+1) - new_from_date.day()
days_after = to_date.day() - 1
if days_before < 0:
days_before = 0
if days_after > accountable_days_in_month:
days_after = accountable_days_in_month
days = days_before + days_after
else:
days = 0
year_fraction = months / accountable_months_in_year
year_fraction += (1 / accountable_months_in_year) * ( days / accountable_days_in_month)
return year_fraction
def getBissextilCompliantYearFraction(from_date=None, to_date=None, reference_date=DateTime('2000/01/01')): def getBissextilCompliantYearFraction(from_date=None, to_date=None, reference_date=DateTime('2000/01/01')):
""" """
Returns a ratio corresponding to the fraction of the year Returns a ratio corresponding to the fraction of the year
...@@ -284,3 +314,9 @@ def roundMonthToGreaterEntireYear(months_number): ...@@ -284,3 +314,9 @@ def roundMonthToGreaterEntireYear(months_number):
years_number += 1 years_number += 1
return int(years_number) * 12 return int(years_number) * 12
def roundDate(date):
"""
Returns a date at 0:00
"""
return DateTime('%s/%s/%s' % (date.year(), date.month(), date.day()))
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment