title: Calculate time between 2 TDateTime, with a twist
body: <p>I need to find out how to get the time between 2 times, but only if it is within work hours(Stored in a database)</p>
  
  <p>This is what I got for now, but it is totally wrong. the total won't bee correct.</p>
  
  <pre><code>int __fastcall Organisasjon::CalculateResponsetimeInOpeninghours(std::auto_ptr&lt;DBCommand&gt; cmd, long orgid, TDateTime starttimeIn, TDateTime endtimeIn)
  {
  int totalTimeInQueue = 0;
  String sIsWorkDay = "";
  
  String s  =  "SELECT o.isworkday, o.workdate, o.workstarttime, o.workendtime " \
                   "FROM orgwdexcep o " \
                   "WHERE o.workdate = :date " \
                   "AND o.orgid = :orgid ";
  String s2 =  "SELECT o.isworkday, o.workdate, o.workstarttime, o.workendtime " \
                   "FROM globalwodexcep o " \
                   "WHERE o.workdate = :date ";
  String s3 =  "SELECT o.workstarttime, o.workendtime " \
                   "FROM organizationworkday o " \
                   "WHERE o.weekdayindex = :weekdayindex " \
                   "AND o.orgid = :orgid ";
  double MailThisDayStart = starttimeIn;
  double MailThisDayEnd = endtimeIn;
  
  while ((int)MailThisDayStart &lt;= (int)endtimeIn)
  {//for each day i period.
      if((int)MailThisDayStart != (int)endtimeIn)
      {
          MailThisDayEnd = (double)((long)MailThisDayEnd) + 1;
  }
      cmd-&gt;setCommandText(s);
      cmd-&gt;Param( "date" ).setAsDateTime() = DBDatabase::ConvertToSADateTime(MailThisDayStart);
      cmd-&gt;Param("orgid").setAsLong() = orgid;
      cmd-&gt;Execute();
      if (!(cmd-&gt;isResultSet() &amp;&amp; cmd-&gt;FetchNext()))
      {
          cmd-&gt;setCommandText(s2);
          cmd-&gt;Param( "date" ).setAsDateTime() = DBDatabase::ConvertToSADateTime(MailThisDayStart);
          cmd-&gt;Execute();
      }
      if(cmd-&gt;isResultSet() &amp;&amp; cmd-&gt;FetchNext())
      {
        sIsWorkDay = String(cmd-&gt;Field("isworkday").asString());
      }
      else
      {
          int dayOfTheWeek = DayOfTheWeek(MailThisDayStart);
          cmd-&gt;setCommandText(s3);
          cmd-&gt;Param("weekdayindex").setAsLong() = dayOfTheWeek;
          cmd-&gt;Param("orgid").setAsLong() = orgid;
          cmd-&gt;Execute();
          if(cmd-&gt;isResultSet() &amp;&amp; cmd-&gt;FetchNext())
          {
              sIsWorkDay = "T";
    }
      }
      if(sIsWorkDay == "T")
      {
          TDateTime tmpOpeningStart =  TDateTime(cmd-&gt;Field("workstarttime").asDateTime());
          TDateTime tmpOpeningEnd = TDateTime(cmd-&gt;Field("workendtime").asDateTime());
          double dtmpOpeningStart = tmpOpeningStart- (int)tmpOpeningStart;
          double dtmpOpeningEnd = tmpOpeningEnd- (int)tmpOpeningEnd;
  
          totalTimeInQueue +=  Organisasjon::CountHours(MailThisDayStart, MailThisDayEnd, dtmpOpeningStart, dtmpOpeningEnd,(int)MailThisDayStart);
      }
      MailThisDayStart++;//increase date by one
      MailThisDayStart = (double)((long)MailThisDayStart);
  }
  return totalTimeInQueue;
  }
  
  int __fastcall Organisasjon::CountHours(double MailTimeStart, double MailTimeEnd, double openingTimeStart, double openingTimeEnd, int DayToCompute)
  {
     if(MailTimeEnd&lt;openingTimeStart)
     {
         return 0;
         }
     if(MailTimeStart&lt;(DayToCompute+openingTimeStart))
     {
         MailTimeStart=openingTimeStart;
     }
     else
     {
          MailTimeStart=MailTimeStart-(int)MailTimeStart;
     }
     if(MailTimeEnd&gt;(DayToCompute+openingTimeEnd))
     {
      MailTimeEnd=openingTimeEnd;
     }
     else
     {
      MailTimeEnd=MailTimeEnd-(int)MailTimeEnd;
     }
     TDateTime dt = TDateTime((MailTimeEnd - MailTimeStart));
     unsigned short milli;
     unsigned short sec;
     unsigned short min;
     unsigned short hour;
     dt.DecodeTime(&amp;hour,&amp;min,&amp;sec,&amp;milli);
     int total = hour*3600;
     total += min*60;
     total += sec;
     return total;
  }
  </code></pre>