计算 2 个 TDateTime 之间的时间,有一点不同

发布于 2024-07-18 06:59:04 字数 3612 浏览 5 评论 0原文

我需要找出如何获取两次之间的时间,但前提是它在工作时间内(存储在数据库中)

这是我现在得到的,但它是完全错误的。 总数不会正确。

int __fastcall Organisasjon::CalculateResponsetimeInOpeninghours(std::auto_ptr<DBCommand> 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 <= (int)endtimeIn)
{//for each day i period.
    if((int)MailThisDayStart != (int)endtimeIn)
    {
        MailThisDayEnd = (double)((long)MailThisDayEnd) + 1;
}
    cmd->setCommandText(s);
    cmd->Param( "date" ).setAsDateTime() = DBDatabase::ConvertToSADateTime(MailThisDayStart);
    cmd->Param("orgid").setAsLong() = orgid;
    cmd->Execute();
    if (!(cmd->isResultSet() && cmd->FetchNext()))
    {
        cmd->setCommandText(s2);
        cmd->Param( "date" ).setAsDateTime() = DBDatabase::ConvertToSADateTime(MailThisDayStart);
        cmd->Execute();
    }
    if(cmd->isResultSet() && cmd->FetchNext())
    {
      sIsWorkDay = String(cmd->Field("isworkday").asString());
    }
    else
    {
        int dayOfTheWeek = DayOfTheWeek(MailThisDayStart);
        cmd->setCommandText(s3);
        cmd->Param("weekdayindex").setAsLong() = dayOfTheWeek;
        cmd->Param("orgid").setAsLong() = orgid;
        cmd->Execute();
        if(cmd->isResultSet() && cmd->FetchNext())
        {
            sIsWorkDay = "T";
  }
    }
    if(sIsWorkDay == "T")
    {
        TDateTime tmpOpeningStart =  TDateTime(cmd->Field("workstarttime").asDateTime());
        TDateTime tmpOpeningEnd = TDateTime(cmd->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<openingTimeStart)
   {
       return 0;
       }
   if(MailTimeStart<(DayToCompute+openingTimeStart))
   {
       MailTimeStart=openingTimeStart;
   }
   else
   {
        MailTimeStart=MailTimeStart-(int)MailTimeStart;
   }
   if(MailTimeEnd>(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(&hour,&min,&sec,&milli);
   int total = hour*3600;
   total += min*60;
   total += sec;
   return total;
}

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)

This is what I got for now, but it is totally wrong. the total won't bee correct.

int __fastcall Organisasjon::CalculateResponsetimeInOpeninghours(std::auto_ptr<DBCommand> 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 <= (int)endtimeIn)
{//for each day i period.
    if((int)MailThisDayStart != (int)endtimeIn)
    {
        MailThisDayEnd = (double)((long)MailThisDayEnd) + 1;
}
    cmd->setCommandText(s);
    cmd->Param( "date" ).setAsDateTime() = DBDatabase::ConvertToSADateTime(MailThisDayStart);
    cmd->Param("orgid").setAsLong() = orgid;
    cmd->Execute();
    if (!(cmd->isResultSet() && cmd->FetchNext()))
    {
        cmd->setCommandText(s2);
        cmd->Param( "date" ).setAsDateTime() = DBDatabase::ConvertToSADateTime(MailThisDayStart);
        cmd->Execute();
    }
    if(cmd->isResultSet() && cmd->FetchNext())
    {
      sIsWorkDay = String(cmd->Field("isworkday").asString());
    }
    else
    {
        int dayOfTheWeek = DayOfTheWeek(MailThisDayStart);
        cmd->setCommandText(s3);
        cmd->Param("weekdayindex").setAsLong() = dayOfTheWeek;
        cmd->Param("orgid").setAsLong() = orgid;
        cmd->Execute();
        if(cmd->isResultSet() && cmd->FetchNext())
        {
            sIsWorkDay = "T";
  }
    }
    if(sIsWorkDay == "T")
    {
        TDateTime tmpOpeningStart =  TDateTime(cmd->Field("workstarttime").asDateTime());
        TDateTime tmpOpeningEnd = TDateTime(cmd->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<openingTimeStart)
   {
       return 0;
       }
   if(MailTimeStart<(DayToCompute+openingTimeStart))
   {
       MailTimeStart=openingTimeStart;
   }
   else
   {
        MailTimeStart=MailTimeStart-(int)MailTimeStart;
   }
   if(MailTimeEnd>(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(&hour,&min,&sec,&milli);
   int total = hour*3600;
   total += min*60;
   total += sec;
   return total;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鱼窥荷 2024-07-25 06:59:04

您希望在total中看到什么?

试试这个例子:

TDateTime MailTimeEnd = TDateTime::CurrentTime();;
MailTimeEnd += 1.0 / 24;
TDateTime MailTimeStart = TDateTime::CurrentTime();
TDateTime dt = TDateTime((MailTimeEnd - MailTimeStart));
unsigned short milli;
unsigned short sec;
unsigned short min;
unsigned short hour;
dt.DecodeTime(&hour,&min,&sec,&milli);
int total = hour*3600;
total += min*60;
total += sec;

总计 == 3600。 这是正确的。

What do you want to see in total?

Try this example:

TDateTime MailTimeEnd = TDateTime::CurrentTime();;
MailTimeEnd += 1.0 / 24;
TDateTime MailTimeStart = TDateTime::CurrentTime();
TDateTime dt = TDateTime((MailTimeEnd - MailTimeStart));
unsigned short milli;
unsigned short sec;
unsigned short min;
unsigned short hour;
dt.DecodeTime(&hour,&min,&sec,&milli);
int total = hour*3600;
total += min*60;
total += sec;

total == 3600. It's right.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文