⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vmdatetime.cpp

📁 TOOL (Tiny Object Oriented Language) is an easily-embedded, object-oriented, C++-like-language inter
💻 CPP
📖 第 1 页 / 共 4 页
字号:
*/
int VMDateTime::GetMinute( void ) const
{ 
  return( m_xTime.wMinute );
}
/* End of function "VMDateTime::GetMinute"
/*****************************************************************************/


/*****************************************************************************/
/*

     FUNCTION NAME:  VMDateTime::GetSecond

       DESCRIPTION:  

             INPUT:  void  
            OUTPUT:  none

           RETURNS:  int   
*/
int VMDateTime::GetSecond( void ) const
{ 
  return( m_xTime.wSecond ); 
}
/* End of function "VMDateTime::GetSecond"
/*****************************************************************************/


/*****************************************************************************/
/*

     FUNCTION NAME:  VMDateTime::GetDayOfWeek

       DESCRIPTION:  

             INPUT:  void  
            OUTPUT:  none

           RETURNS:  int  
*/
int VMDateTime::GetDayOfWeek( void ) const
{ 
  return( m_xTime.wDayOfWeek ); 
}
/* End of function "VMDateTime::GetDayOfWeek"
/*****************************************************************************/


/*****************************************************************************/
/*

     FUNCTION NAME:  VMDateTime::BackAdjustMDY

       DESCRIPTION:  subtraction operation assistant. Adjust MDY for when a
                     day is taken off of a datetime value

             INPUT:  rxTime - the value to check/modify
            OUTPUT:  none

           RETURNS:  void
*/
void VMDateTime::BackAdjustMDY( SYSTEMTIME& rxTime ) const
{
  if ( rxTime.wDay < 1 )
  {
    switch( rxTime.wMonth )
    {
      case 1:  // January
      {
        rxTime.wDay   = 31;
        rxTime.wMonth = 12;
        rxTime.wYear--;
      }
      break;

      case 2:
      case 4:
      case 6:
      case 8:
      case 9:
      case 11:
      {
        rxTime.wDay   = 31;
        rxTime.wMonth--;
      }
      break;

      case 3:  // March
      {
        if ( IsLeapYear( rxTime.wYear ) )
        {
           rxTime.wDay   = 29;
           rxTime.wMonth = 2;
        }
        else
        {
          rxTime.wDay   = 28;
          rxTime.wMonth = 2;
        }
      }
      break;

      case 5:
      case 7:
      case 10:
      case 12:
      {
        rxTime.wDay = 30;
        rxTime.wMonth--;
      }
      break;
    }
  }
}
/* End of function "VMDateTime::BackAdjustMDY"
/*****************************************************************************/


/*****************************************************************************/
/*

     FUNCTION NAME:  VMDateTime::operator- 

       DESCRIPTION:  subtraction operator

             INPUT:  oOffset - the time delta to subtract from another
            OUTPUT:  none

           RETURNS:  VMDateTime representing the difference
*/
VMDateTime VMDateTime::operator- ( VMTimeOffset oOffset ) const
{ 
  SYSTEMTIME xTime = m_xTime;

  long lDays = oOffset.GetDays();

  while ( lDays > 0 )
  {
    lDays--;
    xTime.wDay--;
    if ( xTime.wDay < 1 )
    {
      BackAdjustMDY( xTime );
    }
  }

  if ( oOffset.IsModByDaysZero() )
  {
    // span was days only....since days have 
    // been already subtracted above, return
    //
    return( VMDateTime( (int)xTime.wYear, 
                        (int)xTime.wMonth, 
                        (int)xTime.wDay, 
                        (int)xTime.wHour, 
                        (int)xTime.wMinute,
                        (int)xTime.wSecond ) );
  }

  long lHours = oOffset.GetHours();
  while ( lHours > 0 )
  {
    lHours--;
    xTime.wHour--;
    if ( 65535 == xTime.wHour )
    {
      xTime.wHour = 23;
      xTime.wDay--;

      if ( xTime.wDay < 1 )
      {
        BackAdjustMDY( xTime );
      }
    }
  }
  if ( oOffset.IsModByHoursZero() )
  {
    // span was days and hours only....since days
    // and hours have been already subtracted above,
    // return
    //
    return( VMDateTime( (int)xTime.wYear, 
                        (int)xTime.wMonth, 
                        (int)xTime.wDay, 
                        (int)xTime.wHour, 
                        (int)xTime.wMinute,
                        (int)xTime.wSecond ) );
  }

  long lMinutes = oOffset.GetMinutes();
  
  while ( lMinutes > 0 )
  {
    lMinutes--;
    xTime.wMinute--;

    if ( 65535 == xTime.wMinute )
    {
      xTime.wMinute = 59;
      xTime.wHour--;
      if ( 65535 == xTime.wHour )
      {
        xTime.wHour = 23;
        xTime.wDay--;

        if ( xTime.wDay < 1 )
        {
          BackAdjustMDY( xTime );
        }      
      }
    } 
  }
  if ( oOffset.IsModByMinutesZero() )
  {
    // span was days, hours and minutes only....
    // since days, hours and minutes have been 
    // already subtracted above, return
    //
    return( VMDateTime( (int)xTime.wYear, 
                        (int)xTime.wMonth, 
                        (int)xTime.wDay, 
                        (int)xTime.wHour, 
                        (int)xTime.wMinute,
                        (int)xTime.wSecond ) );
  }

  long lSeconds = oOffset.GetSeconds();

  while ( lSeconds > 0 )
  {
    lSeconds--;
    xTime.wSecond--;
    if ( 65535 == xTime.wSecond )
    {
      xTime.wSecond = 59;
      xTime.wMinute--;

      if ( 65535 == xTime.wMinute )
      {
        xTime.wMinute = 59;
        xTime.wHour--;
        if ( 65535 == xTime.wHour )
        {
          xTime.wHour = 23;
          xTime.wDay--;

          if ( xTime.wDay < 1 )
          {
            BackAdjustMDY( xTime );
          }
        }      
      } 
    } 
  }
  return( VMDateTime( (int)xTime.wYear, 
                      (int)xTime.wMonth, 
                      (int)xTime.wDay, 
                      (int)xTime.wHour, 
                      (int)xTime.wMinute,
                      (int)xTime.wSecond ) );
}
/* End of function "VMDateTime::operator-"
/*****************************************************************************/


/*****************************************************************************/
/*

     FUNCTION NAME:  VMDateTime::FrwdAdjustMDY

       DESCRIPTION:  addition operation assistant. Adjust MDY for when a
                     day is added to a datetime value

             INPUT:  rxTime - the value to check/modify
            OUTPUT:  none

           RETURNS:  void
*/
void VMDateTime::FrwdAdjustMDY( SYSTEMTIME& rxTime ) const
{
  switch( rxTime.wMonth )
  {
    case 1:  // January
    case 3:  // March
    case 5:  // May
    case 7:  // July
    case 8:  // August
    case 10: // October
    {
      if ( rxTime.wDay > 31 )
      {
        rxTime.wDay = 1;
        rxTime.wMonth++;
      }
    }
    break;

    case 12: // December
    {
      if ( rxTime.wDay > 31 )
      {
        rxTime.wDay   = 1;
        rxTime.wMonth = 1;
        rxTime.wYear++;
      }
    }
    break;

    case 4:  // April
    case 6:  // June
    case 9:  // September
    case 11: // November
    {
      if ( rxTime.wDay > 30 )
      {
        rxTime.wDay = 1;
        rxTime.wMonth++;
      }
    }
    break;

    case 2:
    {
      if ( IsLeapYear( rxTime.wYear ) )
      {
        if ( rxTime.wDay > 29 )
        {
          rxTime.wDay = 1;
          rxTime.wMonth++;
        }
      }
      else
      {
        if ( rxTime.wDay > 28 )
        {
          rxTime.wDay = 1;
          rxTime.wMonth++;
        }
      }
    }
    break;
  }
}
/* End of function "VMDateTime::FrwdAdjustMDY"
/*****************************************************************************/


/*****************************************************************************/
/*

     FUNCTION NAME:  VMDateTime::operator+ 

       DESCRIPTION:  addition operator

             INPUT:  oOffset - the time delta to add to another
            OUTPUT:  none

           RETURNS:  VMDateTime representing the sum
*/
VMDateTime VMDateTime::operator+ ( VMTimeOffset oOffset ) const
{ 
  SYSTEMTIME xTime = m_xTime;

  long lDays = oOffset.GetDays();

  while ( lDays > 0 )
  {
    lDays--;
    xTime.wDay++;
    FrwdAdjustMDY( xTime );
  }
  if ( oOffset.IsModByDaysZero() )
  {
    // span was days only....since days have 
    // been already addeded above, return
    //
    return( VMDateTime( (int)xTime.wYear, 
                        (int)xTime.wMonth, 
                        (int)xTime.wDay, 
                        (int)xTime.wHour, 
                        (int)xTime.wMinute,
                        (int)xTime.wSecond ) );
  }

  long lHours = oOffset.GetHours();
  while ( lHours > 0 )
  {
    lHours--;
    xTime.wHour++;
    if ( 24 == xTime.wHour )
    {
      xTime.wHour = 0;
      xTime.wDay++;
      FrwdAdjustMDY( xTime );
    }
  }
  if ( oOffset.IsModByHoursZero() )
  {
    // span was days and hours only....since days
    // and hours have been already added above,
    // return
    //
    return( VMDateTime( (int)xTime.wYear, 
                        (int)xTime.wMonth, 
                        (int)xTime.wDay, 
                        (int)xTime.wHour, 
                        (int)xTime.wMinute,
                        (int)xTime.wSecond ) );
  }

  long lMinutes = oOffset.GetMinutes();
  
  while ( lMinutes > 0 )
  {
    lMinutes--;
    xTime.wMinute++;

    if ( 60 == xTime.wMinute )
    {
      xTime.wMinute = 0;
      xTime.wHour++;
      if ( 24 == xTime.wHour )
      {
        xTime.wHour = 0;
        xTime.wDay++;
        FrwdAdjustMDY( xTime );
      }
    }
  }
  if ( oOffset.IsModByMinutesZero() )
  {
    // span was days, hours and minutes only....
    // since days, hours and minutes have been 
    // already subtracted above, return
    //
    return( VMDateTime( (int)xTime.wYear, 
                        (int)xTime.wMonth, 
                        (int)xTime.wDay, 
                        (int)xTime.wHour, 
                        (int)xTime.wMinute,
                        (int)xTime.wSecond ) );
  }

  long lSeconds = oOffset.GetSeconds();

  while ( lSeconds > 0 )
  {
    lSeconds--;
    xTime.wSecond++;

    if ( 60 == xTime.wSecond )
    {
      xTime.wSecond = 0;
      xTime.wMinute++;

      if ( 60 == xTime.wMinute )
      {
        xTime.wMinute = 0;
        xTime.wHour++;
        if ( 24 == xTime.wHour )
        {
          xTime.wHour = 0;
          xTime.wDay++;
          FrwdAdjustMDY( xTime );
        }      
      } 
    } 
  }
  return( VMDateTime( (int)xTime.wYear, 
                      (int)xTime.wMonth, 
                      (int)xTime.wDay, 
                      (int)xTime.wHour, 
                      (int)xTime.wMinute,
                      (int)xTime.wSecond ) );
}
/* End of function "VMDateTime::operator+"
/*****************************************************************************/


/*****************************************************************************/
/*

     FUNCTION NAME:  VMDateTime::operator+= 

       DESCRIPTION:  addition operator

             INPUT:  oOffset - the time delta to add to this
            OUTPUT:  none

           RETURNS:  this after the time span has been added to this
*/
const VMDateTime& VMDateTime::operator+= ( VMTimeOffset oOffset )
{ 
  VMDateTime oResult = *this + oOffset;

  m_xTime     = oResult.m_xTime;
  m_iDayOfYear = oResult.m_iDayOfYear;

  return( *this ); 
}
/* End of function "VMDateTime::operator+="
/*****************************************************************************/


/*****************************************************************************/
/*

     FUNCTION NAME:  VMDateTime::operator-= 

       DESCRIPTION:  subtraction operator

             INPUT:  oOffset - the time delta to subtract from this
            OUTPUT:  none

           RETURNS:  this after the time span has been subtracted from this
*/
const VMDateTime& VMDateTime::operator-= ( VMTimeOffset oOffset )
{ 
  VMDateTime oResult = *this - oOffset;

  m_xTime     = oResult.m_xTime;
  m_iDayOfYear = oResult.m_iDayOfYear;

  return( *this ); 
}
/* End of function "VMDateTime::operator-="
/*****************************************************************************/


/*****************************************************************************/
/*

     FUNCTION NAME:  VMDateTime::GetTimeSpan

       DESCRIPTION:  calculates the difference in seconds between the time in
                     this and another instance of this

             INPUT:  roOther - another instance of this to get the time span for
            OUTPUT:  none

           RETURNS:  long 
*/
const long VMDateTime::GetTimeSpan( VMDateTime& roOther )
{
  if ( *this != roOther )
  {
    double   dblThisSeconds = ( m_iDayOfYear * ( 24 * 60 * 60 ) )
                            + ( m_xTime.wYear * ( 24 * 60 * 60 * 365.25 ) ) 
                            + ( m_xTime.wHour * ( 60 * 60 ) )
                            + ( m_xTime.wMinute  * 60 )
                            +  m_xTime.wSecond;

    double   dblOtherSeconds  = ( roOther.m_iDayOfYear * ( 24 * 60 * 60 ) ) 
                              + ( roOther.m_xTime.wYear * ( 24 * 60 * 60 * 365.25 ) ) 
                              + ( roOther.m_xTime.wHour * ( 60 * 60 ) )
                              + ( roOther.m_xTime.wMinute  * 60 )
                              +  roOther.m_xTime.wSecond;
    return( (long)( dblOtherSeconds - dblThisSeconds ) );
  }
  else
  {
    return( 0L );
  }
}
/* End of function "VMDateTime::GetTimeSpan"
/*****************************************************************************/


/*****************************************************************************/
/* Check-in history */
/*
 *$Log:  $
*/
/*****************************************************************************/


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -