[Erp5-report] r24498 - /erp5/trunk/products/ERP5Type/DateUtils.py

nobody at svn.erp5.org nobody at svn.erp5.org
Wed Nov 5 17:00:49 CET 2008


Author: nicolas
Date: Wed Nov  5 17:00:49 2008
New Revision: 24498

URL: http://svn.erp5.org?rev=24498&view=rev
Log:
return the last time for a given date inside a given period

Modified:
    erp5/trunk/products/ERP5Type/DateUtils.py

Modified: erp5/trunk/products/ERP5Type/DateUtils.py
URL: http://svn.erp5.org/erp5/trunk/products/ERP5Type/DateUtils.py?rev=24498&r1=24497&r2=24498&view=diff
==============================================================================
--- erp5/trunk/products/ERP5Type/DateUtils.py [utf8] (original)
+++ erp5/trunk/products/ERP5Type/DateUtils.py [utf8] Wed Nov  5 17:00:49 2008
@@ -40,7 +40,7 @@
     'getMonthFraction', 'getYearFraction', 'getAccountableYearFraction',
     'getBissextilCompliantYearFraction', 'getIntervalListBetweenDates',
     'getDecimalNumberOfYearsBetween','roundMonthToGreaterEntireYear',
-    'roundDate', 'convertDateToHour', 'getNumberOfDayInMonth')
+    'roundDate', 'convertDateToHour', 'getNumberOfDayInMonth', 'atTheEndOfPeriod')
 
 millis = DateTime('2000/01/01 12:00:00.001') - DateTime('2000/01/01 12:00:00')
 centis = millis * 10
@@ -495,3 +495,32 @@
     if date.isLeapYear():
       return 29
     return 28
+
+def atTheEndOfPeriod(date, period):
+  """
+  return the last time value for a given date
+  and a given period
+  year, month, week, day
+  note that a week is ended at Sunday
+  exemple:
+  2000/01/01, year => 2001/01/01
+  2000/01/15, month => 2000/02/01
+  2000/01/18, week => 2000/01/24
+  2000/01/20, day => 2000/01/21
+  """
+  if period == 'year':
+    end = addToDate(DateTime(date.strftime('%Y/01/01 00:00:00')), **{period:1})
+  elif period == 'month':
+    end = addToDate(DateTime(date.strftime('%Y/%m/01 00:00:00')), **{period:1})
+  elif period == 'day':
+    end = addToDate(DateTime(date.strftime('%Y/%m/%d 00:00:00')), **{period:1})
+  elif period == 'week':
+    day_of_week = date.strftime('%A')
+    end = DateTime(date.strftime('%Y/%m/%d 00:00:00'))
+    while day_of_week != 'Sunday':
+      end = addToDate(end, day=1)
+      day_of_week = end.strftime('%A')
+    end = addToDate(end, day=1)
+  else:
+    raise NotImplementedError, 'Period "%s" not Handled yet' % period
+  return end




More information about the Erp5-report mailing list