[prev in list] [next in list] [prev in thread] [next in thread] 

List:       koffice-devel
Subject:    patch for datedif
From:       Sascha Pfau <mrpeacock () gmail ! com>
Date:       2006-12-17 19:14:57
Message-ID: 200612172014.57705.MrPeacock () gmail ! com
[Download RAW message or body]

hello,

another patch for a missing function. this time datedif is it. i know the 
calcution looks strange and the patch ugly. the doctumentation is missing 
too.

i did my best, but because i dont have running excel, i cant verify the 
function.

best regards

Sascha Pfau

["kspread-add-datedif.patch" (text/x-diff)]

--- ./datetime.cpp.org	2006-12-17 20:00:45.000000000 +0100
+++ ./datetime.cpp	2006-12-17 20:12:43.000000000 +0100
@@ -35,6 +35,7 @@
 Value func_currentDateTime (valVector args, ValueCalc *calc, FuncExtra *);
 Value func_currentTime (valVector args, ValueCalc *calc, FuncExtra *);
 Value func_date (valVector args, ValueCalc *calc, FuncExtra *);
+Value func_dateDif (valVector args, ValueCalc *calc, FuncExtra *);
 Value func_datevalue (valVector args, ValueCalc *calc, FuncExtra *);
 Value func_day (valVector args, ValueCalc *calc, FuncExtra *);
 Value func_dayname (valVector args, ValueCalc *calc, FuncExtra *);
@@ -88,6 +89,8 @@
   repo->add (f);
   f = new Function ("DATE",  func_date);
   f->setParamCount (3);
+  f = new Function ("DATEDIF",  func_dateDif);
+  f->setParamCount (3);
   repo->add (f);
   f = new Function ("DATEVALUE",  func_datevalue);
   repo->add (f);
@@ -686,3 +689,119 @@
   //kDebug(36002) << "weeknum = [startday(" << startday << ") + base(7) + New Year(" \
<< date1.dayOfWeek() <<") + days(" << days << ")] / 7 = " << \
(startday+7+date1.dayOfWeek()+days)/7 << endl;   return Value( \
(int)(startday+7+date1.dayOfWeek()+days)/7 );  }
+
+// Function: dateDif
+//
+// 		interval difference type	descrition 	
+// default: 	m	 months
+//		d	 days
+//		y	 complete years
+//		ym	 months excluding years
+//		yd	 days excluding years
+//		md	 days excluding months and years
+//
+Value func_dateDif (valVector args, ValueCalc *calc, FuncExtra *)
+{
+  int y,m,d;
+  QDate Temp1, Temp2;
+  QDate date0(1899,12,31);
+
+  Value v( calc->conv()->asDate (args[0]).asDate( calc->doc() ), calc->doc() );
+  if (v.isError()) return v;
+  QDate date1 = v.asDate( calc->doc() );
+
+  if (!date1.isValid())
+      return Value::errorVALUE();
+
+  Value v1( calc->conv()->asDate (args[1]).asDate( calc->doc() ), calc->doc() );
+  if (v1.isError()) return v1;
+  QDate date2 = v1.asDate( calc->doc() );
+
+  if (!date2.isValid())
+      return Value::errorVALUE();
+
+  if ( date2 < date1 )
+  {
+    return Value::errorVALUE();
+  }
+  
+  QString interval = calc->conv()->asString (args[2]).asString();
+
+  //  
+  // calculate 
+  //
+  
+  // Temp1 = DateSerial(Year(Date2), Month(Date1), Day(Date1))
+  Temp1.setDate(date2.year(), date1.month(), date1.day());
+
+  // Y = Year(Date2) - Year(Date1) + (Temp1 > Date2)
+  y = date2.year() - date1.year() + (date0.daysTo(Temp1) > \
date0.daysTo(date2)?-1:0); +  
+  // M = Month(Date2) - Month(Date1) - (12 * (Temp1 > Date2))
+  m = date2.month() - date1.month() - (12 * (Temp1 > date2?-1:0));
+  
+  // D = Day(Date2) - Day(Date1)
+  d = date2.day() - date1.day();
+  
+  if ( d < 0)
+  {
+    // M = M - 1
+    m--;
+    // D = Day(DateSerial(Year(date2), Month(date2), 0)) + D
+    Temp2.setDate(date2.year(), date2.month()-1, 1);
+    d = Temp2.daysInMonth()+d;
+  }
+  
+  //kDebug(36002) << "y = " << y << endl;
+  //kDebug(36002) << "m = " << m << endl;
+  //kDebug(36002) << "d = " << d << endl;
+  
+  //
+  // output
+  //
+  
+  if ( interval == "y" )
+  {
+    // year
+    //kDebug (36002) << "year = " << y << endl;
+    return Value( y );
+  } 
+  else if ( interval == "m" )
+  {
+    // month
+    //kDebug (36002) << "month = " << 12*y+m << endl;
+    return Value( 12*y+m );
+  } 
+  else if ( interval == "d" )
+  {
+    // days
+    int days = date0.daysTo(date2)-date0.daysTo(date1);
+    //kDebug(36002) << "days = " << days << endl;
+    return Value( days );
+  } 
+  else if ( interval == "ym" )
+  {
+    // month excl. years
+    //kDebug (36002) << "months excl. years = " << m << endl;
+    return Value( m );
+  } 
+  else if ( interval == "yd" )
+  {
+    // days excl. years
+    QDate Temp3(date2.year(), date1.month(), date1.day());
+    int days = date0.daysTo(date2)-date0.daysTo(Temp3);
+    //kDebug (36002) << "days excl. years = " << days << endl;
+    return Value( days );
+  } 
+  else if ( interval == "md" )
+  {
+    // days excl. month and years
+    //kDebug (36002) << "days excl. months and years = " << d << endl;
+    return Value( d );
+  } 
+  else
+  {
+    // error
+    return Value::errorVALUE();
+  }
+}
--- ./datetime.xml.org	2006-12-17 20:00:45.000000000 +0100
+++ ./datetime.xml	2006-12-17 20:03:45.000000000 +0100
@@ -616,6 +616,29 @@
 	</Help>
     </Function>
 
+    <Function>
+	<Name>DATEDIF</Name>
+	<Type>Int</Type>
+	<Parameter>
+	    <Comment>First date</Comment>
+	    <Type>String</Type>
+	</Parameter>
+	<Parameter>
+	    <Comment>Second date</Comment>
+	    <Type>String</Type>
+	</Parameter>
+	<Parameter>
+	    <Comment>interval</Comment>
+	    <Type>String</Type>
+	</Parameter>
+	<Help>
+	    <Text>The DATEDIF() function returns the difference between two dates.</Text>
+	    <Syntax>DATEDIF(first date; second date; interval)</Syntax>
+	    <Example>DATEDIF() </Example>
+	    <Example>DATEDIF() </Example>
+	</Help>
+    </Function>
+
   </Group>
 
 </KSpreadFunctions>



_______________________________________________
koffice-devel mailing list
koffice-devel@kde.org
https://mail.kde.org/mailman/listinfo/koffice-devel


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic