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

nobody at svn.erp5.org nobody at svn.erp5.org
Thu Aug 16 02:46:27 CEST 2007


Author: jp
Date: Thu Aug 16 02:46:26 2007
New Revision: 15687

URL: http://svn.erp5.org?rev=15687&view=rev
Log:
convertDateToHour (still must be reviewed). It is required by the crawling API based on alarms.

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=15687&r1=15686&r2=15687&view=diff
==============================================================================
--- erp5/trunk/products/ERP5Type/DateUtils.py (original)
+++ erp5/trunk/products/ERP5Type/DateUtils.py Thu Aug 16 02:46:26 2007
@@ -30,6 +30,7 @@
 
 from AccessControl import ModuleSecurityInfo
 from DateTime import DateTime
+from datetime import datetime
 from zLOG import LOG
 
 security = ModuleSecurityInfo('Products.ERP5Type.DateUtils')
@@ -39,7 +40,7 @@
     'getMonthFraction', 'getYearFraction', 'getAccountableYearFraction',
     'getBissextilCompliantYearFraction',
     'getDecimalNumberOfYearsBetween','roundMonthToGreaterEntireYear',
-    'roundDate')
+    'roundDate', 'convertDateToHour')
 
 millis = DateTime('2000/01/01 12:00:00.001') - DateTime('2000/01/01 12:00:00')
 centis = millis * 10
@@ -52,7 +53,8 @@
 same_movement_interval = hour
   
 accountable_days_in_month = 30.
-accountable_months_in_year = 12.
+accountable_months_in_year = 12
+number_of_hours_in_year  = 8760
 
 def addToDate(date, to_add=None, **kw):
   """
@@ -337,3 +339,26 @@
   warnings.warn('ERP5Type.DateUtils.roundDate is deprecated, use'
                 ' DateTime.earliestTime instead', DeprecationWarning)
   return date.earliestTime()
+
+def convertDateToHour(date=None):
+  """
+  converts the date passed as parameter into hours
+  """
+  if date is None:
+    date = DateTime()
+  # The Zope DateTime object passed as parameter must be transformed into 
+  # python datetime object, to use toordinal method in conversion to hours
+  creation_date_dict = {}
+  for key in ('year', 'month', 'day'):
+    method = getattr(date, key)
+    creation_date_dict[key] = method()
+  # formating the date from Zope DateTime format to python datetime format
+  # and this provides the use of toordinal method.
+  formatted_creation_date = datetime(creation_date_dict['year'],creation_date_dict['month'],creation_date_dict['day'])
+  # reference date which is the first day of creation date year
+  reference_date = datetime(creation_date_dict['year'], 01, 1)
+  # calculate the ordinal date of the creation date and the reference date
+  ordinal_date = datetime.toordinal(formatted_creation_date)
+  ordinal_reference_date = datetime.toordinal(reference_date)
+  hour = (ordinal_date - ordinal_reference_date) * number_of_hours_in_day + number_of_hours_in_day + date.hour()
+  return int(hour)




More information about the Erp5-report mailing list