DatabaseLayer SetDateTimeFromTm bug

Talk here about issues with one of the components hosted at wxCode, or suggest features for it.
bone
Experienced Solver
Experienced Solver
Posts: 74
Joined: Fri Nov 30, 2007 10:11 am
Location: Oz

DatabaseLayer SetDateTimeFromTm bug

Postby bone » Sun Apr 11, 2010 7:22 am

Code: Select all

void FirebirdResultSet::SetDateTimeFromTm(wxDateTime& dateReturn, struct tm& timeInTm)
{
  dateReturn.Set(timeInTm.tm_mday, wxDateTime::Month(timeInTm.tm_mon), timeInTm.tm_year + 1900, timeInTm.tm_hour, timeInTm.tm_min, timeInTm.tm_sec);
}


Parameter struct tm& timeInTm is a time struct so adding 1900 to the year in wxDateTime where day = 0 and month = 0 causes an assert in dateReturn.Set(...)

Version 1.7.5 Odd that it has not been reported before. I guess everyone uses datetimestamp.

I used

Code: Select all

dateReturn.Set( timeInTm.tm_hour, timeInTm.tm_min, timeInTm.tm_sec);


Unless I am wrong there are no milliseconds in struct tm& timeInTm which is a pity as Firebird at least can store 9999 milliseconds.

Cheers, John

bone
Experienced Solver
Experienced Solver
Posts: 74
Joined: Fri Nov 30, 2007 10:11 am
Location: Oz

Postby bone » Mon May 03, 2010 1:19 am

This is what I ended up with although it probably still needs a check for valid data before adding 1900 to the year in the SQL_TIMESTAMP and SQL_TYPE_DATE branches.
Regards, John

Code: Select all

wxDateTime FirebirdResultSet::GetResultDate(int nField)
{
  ResetErrorCodes();

  wxDateTime dateReturn = wxInvalidDateTime;
  XSQLVAR* pVar = &(m_pFields->sqlvar[nField-1]);
  if (IsNull(pVar))
  {
    // The column is NULL
    dateReturn = wxInvalidDateTime;
  }
  else
  {
    short nType = pVar->sqltype & ~1;
    if (nType == SQL_TIMESTAMP)
    {
      struct tm timeInTm;
        isc_decode_timestamp((ISC_TIMESTAMP *)pVar->sqldata, &timeInTm);
          dateReturn.Set(timeInTm.tm_mday, wxDateTime::Month(timeInTm.tm_mon), timeInTm.tm_year + 1900, timeInTm.tm_hour, timeInTm.tm_min, timeInTm.tm_sec);
      //SetDateTimeFromTm(dateReturn, timeInTm);
    }
    else if (nType == SQL_TYPE_DATE)
    {
      struct tm timeInTm;
       isc_decode_sql_date((ISC_DATE *)pVar->sqldata, &timeInTm);
        dateReturn.Set(timeInTm.tm_mday, wxDateTime::Month(timeInTm.tm_mon), timeInTm.tm_year + 1900);
      //SetDateTimeFromTm(dateReturn, timeInTm);
    }
    else if (nType == SQL_TYPE_TIME)
    {
      struct tm timeInTm;
     isc_decode_sql_time((ISC_TIME *)pVar->sqldata, &timeInTm);
      dateReturn.Set( timeInTm.tm_hour, timeInTm.tm_min, timeInTm.tm_sec);
      //SetDateTimeFromTm(dateReturn, timeInTm);
    }
    else
    {
      // Incompatible field type
      // Set error codes and throw an exception here
      dateReturn = wxInvalidDateTime;

      SetErrorMessage(_("Invalid field type"));
      SetErrorCode(DATABASE_LAYER_INCOMPATIBLE_FIELD_TYPE);

      ThrowDatabaseException();
    }
  }

  return dateReturn;
}


Return to “wxCode”

Who is online

Users browsing this forum: No registered users and 3 guests