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

📄 vmdatetime.cpp

📁 TOOL (Tiny Object Oriented Language) is an easily-embedded, object-oriented, C++-like-language inter
💻 CPP
📖 第 1 页 / 共 4 页
字号:

  return( VMDateTime( xNow ) );
}
/* End of function "VMDateTime::CurrentTime"
/*****************************************************************************/


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

     FUNCTION NAME:  VMDateTime::VMDateTime

       DESCRIPTION:  construct from a string

             INPUT:  pchDate - the date string
                     const char* pchPattern - 
            OUTPUT:  

           RETURNS:  none
*/
VMDateTime::VMDateTime( const char* pchDate )
{
  char achInput[ 100 ];
  strcpy( achInput, pchDate );
  char* pchInput = achInput;

  // mjs: you can do better here to cover all cases

  if ( NULL != strchr( pchInput, '/' ) )
  {
    // hopefully will follow mm/dd/yyyy hh:mm:ss
    //
    char* pchDelim = strchr( pchInput, '/' );
    *pchDelim = 0;
    m_xTime.wMonth = atoi( pchInput );

    pchInput = pchDelim + 1;
    pchDelim = strchr( pchInput, '/' );
    *pchDelim = 0;
    m_xTime.wDay = atoi( pchInput );
 
    pchInput = pchDelim + 1;
    pchDelim = strchr( pchInput, ' ' );
    *pchDelim = 0;
    m_xTime.wYear = atoi( pchInput );

    pchInput = pchDelim + 1;
    pchDelim = strchr( pchInput, ':' );
    *pchDelim = 0;
    m_xTime.wHour = atoi( pchInput );

    pchInput = pchDelim + 1;
    pchDelim = strchr( pchInput, ':' );
    *pchDelim = 0;
    m_xTime.wMinute = atoi( pchInput );
    
    pchInput = pchDelim + 1;
    m_xTime.wSecond = atoi( pchInput );
  }
  else
  if ( ( strstr( pchInput, "Jan"  ) )
    || ( strstr( pchInput, "Feb"  ) )
    || ( strstr( pchInput, "Mar"  ) )
    || ( strstr( pchInput, "Apr"  ) )
    || ( strstr( pchInput, "May"  ) )
    || ( strstr( pchInput, "Jun"  ) )
    || ( strstr( pchInput, "Jul"  ) )
    || ( strstr( pchInput, "Aug"  ) )
    || ( strstr( pchInput, "Sep"  ) )
    || ( strstr( pchInput, "Oct"  ) )
    || ( strstr( pchInput, "Nov"  ) )
    || ( strstr( pchInput, "Dec"  ) ) )
  {
    char* pchDelim = strchr( pchInput, ' ' );
    *pchDelim = 0;
    if ( 0 != strstr( pchInput, "Jan" ) )
    {
      m_xTime.wMonth = 01;
    }
    else
    if ( 0 != strstr( pchInput, "Feb" ) )
    {
      m_xTime.wMonth = 1;
    }
    else
    if ( 0 != strstr( pchInput, "Mar" ) )
    {
      m_xTime.wMonth = 2;
    }
    else
    if ( 0 != strstr( pchInput, "Apr" ) )
    {
      m_xTime.wMonth = 3;
    }
    else
    if ( 0 != strstr( pchInput, "May" ) )
    {
      m_xTime.wMonth = 4;
    }
    else
    if ( 0 != strstr( pchInput, "Jun" ) )
    {
      m_xTime.wMonth = 6;
    }
    else
    if ( 0 != strstr( pchInput, "Jul" ) )
    {
      m_xTime.wMonth = 7;
    }
    else
    if ( 0 != strstr( pchInput, "Aug" ) )
    {
      m_xTime.wMonth = 8;
    }
    else
    if ( 0 != strstr( pchInput, "Sep" ) )
    {
      m_xTime.wMonth = 9;
    }
    else
    if ( 0 != strstr( pchInput, "Oct" ) )
    {
      m_xTime.wMonth = 10;
    }
    else
    if ( 0 != strstr( pchInput, "Nov" ) )
    {
      m_xTime.wMonth = 11;
    }
    else
    if ( 0 != strstr( pchInput, "Dec" ) )
    {
      m_xTime.wMonth = 12;
    }
    pchInput = pchDelim + 1;
    pchDelim = strchr( pchInput, ' ' );
    *pchDelim = 0;
    m_xTime.wDay = atoi( pchInput );
  
    pchInput = pchDelim + 1;
    pchDelim = strchr( pchInput, ' ' );
    *pchDelim = 0;
    m_xTime.wYear = atoi( pchInput );

    pchInput = pchDelim + 1;
    pchDelim = strchr( pchInput, ':' );
    *pchDelim = 0;
    m_xTime.wHour = atoi( pchInput );

    pchInput = pchDelim + 1;
     
    pchDelim = strchr( pchInput, 'P' );
    if ( pchDelim != NULL )
    {
      if ( m_xTime.wHour < 13 )
      {
        m_xTime.wHour += 12;
      }
      *pchDelim = 0;
      m_xTime.wMinute = atoi( pchInput );
    }
    else
    {
      pchDelim = strchr( pchInput, 'A' );
      if ( NULL != pchDelim )
      {
        *pchDelim = 0;
      }
      m_xTime.wMinute = atoi( pchInput );
    }
    m_xTime.wSecond = 0; 
  }

  SetDays();
}
/* End of function "VMDateTime::VMDateTime"
/*****************************************************************************/


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

     FUNCTION NAME:  VMDateTime::VMDateTime

       DESCRIPTION:  ctor

             INPUT:  iYear -   the 
                     iMonth -  current
                     iDay -    time values
                     iHour -   to assign
                     iMin -    to
                     iSec -    this
            OUTPUT:  

           RETURNS:  none
*/
VMDateTime::VMDateTime( int iYear, int iMonth, int iDay, int iHour, int iMin, int iSec )
{
  assert( iDay   >= 1 && iDay   <= 31 );
  assert( iMonth >= 1 && iMonth <= 12 );
  assert( iHour  >= 0 && iHour  < 24  );
  assert( iMin   >= 0 && iMin   < 60  );
  assert( iSec   >= 0 && iSec   < 60  );

  switch ( iMonth )
  {
    case 1:  // January
    case 3:  // March
    case 5:  // May
    case 7:  // July
    case 8:  // August
    case 10: // October
    case 12: // December
      assert( iDay <= 31 );
    break;

    case 4:  // April
    case 6:  // June
    case 9:  // September
    case 11: // November
      assert( iDay <= 30 );
    break;

    case 2:
      assert( ( iDay <= 28 ) || ( IsLeapYear( iYear) && ( iDay <= 29 ) ) );
    break;

    default: // Bad month
      assert( FALSE );
    break;
   }

   m_xTime.wYear   = iYear;
   m_xTime.wMonth  = iMonth;
   m_xTime.wDay    = iDay;
   m_xTime.wHour   = iHour;
   m_xTime.wMinute = iMin;
   m_xTime.wSecond = iSec;

   SetDays();
}
/* End of function "VMDateTime::VMDateTime"
/*****************************************************************************/


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

     FUNCTION NAME:  VMDateTime::VMDateTime

       DESCRIPTION:  ctor

             INPUT:  rxFileTime - file time to set this from
            OUTPUT:  none

           RETURNS:  none 
*/
VMDateTime::VMDateTime( const FILETIME& rxFileTime )
{
  // first convert file time (UTC time) to local time
  //
  FILETIME xLocalTime;
  if ( !FileTimeToLocalFileTime( &rxFileTime, &xLocalTime ) )
  {
    memset( &m_xTime, 0, sizeof( m_xTime ) );
    return;
  }

  // then convert that time to system time
  //
  SYSTEMTIME xSystemTime;
  if ( !FileTimeToSystemTime( &xLocalTime, &xSystemTime ) )
  {
    memset( &m_xTime, 0, sizeof(m_xTime) );
    return;
  }
  m_xTime = xSystemTime;
}
/* End of function "VMDateTime::VMDateTime"
/*****************************************************************************/


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

     FUNCTION NAME:  VMDateTime::VMDateTime

       DESCRIPTION:  ctor

             INPUT:  void 
            OUTPUT:  none

           RETURNS:  none
*/
VMDateTime::VMDateTime( void )
{
  GetLocalTime( &m_xTime );
  SetDays();
}
/* End of function "VMDateTime::VMDateTime"
/*****************************************************************************/


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

     FUNCTION NAME:  VMDateTime::VMDateTime

       DESCRIPTION:  copy ctor

             INPUT:  roOther - another instance to set this from
            OUTPUT:  none

           RETURNS:  none
*/
VMDateTime::VMDateTime( const VMDateTime& roOther )
{ 
  m_xTime = roOther.m_xTime;
  SetDays(); 
}
/* End of function "VMDateTime::VMDateTime"
/*****************************************************************************/


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

     FUNCTION NAME:  VMDateTime::VMDateTime

       DESCRIPTION:  ctor.

             INPUT:  rxTime - SYSTEMTIME to assign to this
            OUTPUT:  none

           RETURNS:  none
*/
VMDateTime::VMDateTime( const SYSTEMTIME& rxTime )
{ 
  m_xTime = rxTime; 
  SetDays();
}
/* End of function "VMDateTime::VMDateTime"
/*****************************************************************************/


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

     FUNCTION NAME:  VMDateTime::SetDays

       DESCRIPTION:  sets the day of the week and the day of the year in this

             INPUT:  void  
            OUTPUT:  none

           RETURNS:  void  
*/
void VMDateTime::SetDays( void )
{
  int   aiDaysToMonth[ 12 ] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };

  m_iDayOfYear = aiDaysToMonth[ m_xTime.wMonth - 1 ] + m_xTime.wDay;

  // Adjust for leap years
  //
  if ( ( IsLeapYear( m_xTime.wYear ) ) && ( m_xTime.wMonth > 2 ) )
  {
    m_iDayOfYear++;
  }

  // Calculate the number of days since AD 1
  //
  long  lDayNum;
  int   iYear = m_xTime.wYear;

  // Approximate the number of days since 1/1/1
  //
  lDayNum = ( iYear ) * 365;

  // Add days for the leap years
  //
  lDayNum += ( iYear - 1) / 4;

  // Subtract years which are divisible by 100
  //
  lDayNum -= ( iYear - 1) / 100;

  // But replace years which are divisible by 400
  //
  lDayNum += ( iYear - 1) / 400;

  // This class is 1900-based, but 1582 is algorithmically significant.
  //
  assert( iYear > 1582 );

  // But before 1582 all centuries were leap years. 
  // In 1582, Pope Gregory XIII mandated that October 4 would
  // be followed by October 15.  This accounts for those facts.
  //
  //lDayNum += 2;

  // Now add the days to this point this year
  //
  lDayNum += m_iDayOfYear;

  // Finally, calculate the day of the week based on what we now know.
  //
  m_xTime.wDayOfWeek = static_cast< WORD >( ( ( ( lDayNum % 7 ) + 6 ) % 7) );// 2b|!2b==?
}
/* End of function "VMDateTime::SetDays"
/*****************************************************************************/


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

     FUNCTION NAME:  VMDateTime::IsLeapYear

       DESCRIPTION:  helper function to determine leap year ness for the given
                     year

             INPUT:  iYear - the year to check
            OUTPUT:  none

           RETURNS:  true if it is, false if not
*/
bool VMDateTime::IsLeapYear( int iYear )
{
  // This will work for years before 1900, but it won't
  //   work for tm_years which are 1900-based so that
  //   2000 is represented as 100. 
  //
  assert( iYear > 1900 );

  if ( ( iYear % 4 ) != 0 )
  {
    // Years not divisible by four are not leap years
    //
    return( FALSE );
  }
  else
  {
    // Years divisible by 4 are leap years
    // Unless they are also divisible by 100
    // and not divisible by 400.
    // Pope Gregory XIII didn't like programmers.
    //
    if ( ( iYear % 100 ) != 0 )
    {
      return( TRUE );
    }
    else
    {
      if ( ( iYear % 400 ) != 0 )
      {
        return( FALSE );
      }
      else
      {
        return( TRUE );
      }
    }
  }
}
/* End of function "VMDateTime::IsLeapYear"
/*****************************************************************************/


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

     FUNCTION NAME:  VMDateTime::Format

       DESCRIPTION:  string format kinda like sprintf...does not handle daylight
                     savings time format argument tho...

             INPUT:  pchFormat - pointer to the format string
            OUTPUT:  none

           RETURNS:  VMString containing result
*/
VMString VMDateTime::Format( const char* pchFormat ) const
{
  char  achBuffer[ ciMaxTimeBufferSize ];
  char  aChar;
  char* pchBuffer = achBuffer;

  while ( ( aChar = *pchFormat++ ) != '\0')
  {
    assert( pchBuffer < &achBuffer[ ciSafeTimeBufferSize ] );

    // Just because we've asserted it doesn't always make it true;
    // The code below may put us over the safe buffer size.  If
    // we aren't to the safe-point yet, anything below will fit.
    //
    if ( pchBuffer >= &achBuffer[ ciSafeTimeBufferSize ] )
    {
      return( "" );
    }

    if ( aChar == '%' )
    {
      switch ( aChar = *pchFormat++ )
      {
        default:
          assert( FALSE );
        break;

        case '%':
          *pchBuffer++ = aChar;
        break;

        case 'a':
        {
          char  achDays[ 7 ][ 4 ] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
          strcpy( pchBuffer, achDays[ m_xTime.wDayOfWeek ] );
          pchBuffer += strlen( achDays[ m_xTime.wDayOfWeek ] );
        }
        break;

        case 'A':
        {
          char  achDays[ 7 ][ 10 ] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
          strcpy( pchBuffer, achDays[ m_xTime.wDayOfWeek ] );
          pchBuffer += strlen( achDays[ m_xTime.wDayOfWeek ] );
        }
        break;

        case 'b':
        {
          char  achMonths[ 12 ][ 4 ] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
                                         "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
          strcpy( pchBuffer, achMonths[ m_xTime.wMonth - 1 ] );
          pchBuffer += strlen( achMonths[ m_xTime.wMonth - 1 ] );
        }
        break;
        case 'B':
        {
          char  achMonths[ 12 ][ 10 ] = { "January", "February", "March",     "April",   "May",      "June",
                                          "July",    "August",   "September", "October", "November", "December" };
          strcpy( pchBuffer, achMonths[ m_xTime.wMonth - 1 ] );
          pchBuffer += strlen( achMonths[ m_xTime.wMonth - 1 ] );
        }
        break;
        case 'c':
        {
           pchBuffer += sprintf( pchBuffer,
                                 "%02u/%02u/%02u %02u:%02u:%02u", 
                                 m_xTime.wMonth, 
                                 m_xTime.wDay, 
                                 m_xTime.wYear % 100, 
                                 m_xTime.wHour,
                                 m_xTime.wMinute,
                                 m_xTime.wSecond );
        }
        break;
        case 'd':
          pchBuffer += sprintf( pchBuffer, "%02u", m_xTime.wDay );
        break;
        case 'H':
          pchBuffer += sprintf( pchBuffer, "%02u", m_xTime.wHour );
        break;
        case 'I':
          pchBuffer  += sprintf( pchBuffer, "%02u", ( m_xTime.wHour % 13 ) + ( ( m_xTime.wHour > 12 ) ? 1 : 0 ) );
        break;
        case 'j':
          pchBuffer += sprintf( pchBuffer, "%03d",  m_iDayOfYear );
        break;
        case 'm':
          pchBuffer += sprintf( pchBuffer, "%02u", m_xTime.wMonth );
        break;
        case 'M':
          pchBuffer += sprintf( pchBuffer, "%02u", m_xTime.wMinute );
        break;
        case 'p':
          pchBuffer += sprintf( pchBuffer, "%s", ( ( m_xTime.wHour > 12 ) ? "PM" : "AM" ) );
        break;
        case 'S':
          pchBuffer += sprintf( pchBuffer, "%02u", m_xTime.wSecond );

⌨️ 快捷键说明

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