Wednesday, September 15, 2010

Rounding

http://codingforums.com/showthread.php?t=10827

SYSTEMTIME Structure

 http://msdn.microsoft.com/en-us/library/ms724950(VS.85).aspx
 typedef struct _SYSTEMTIME {   WORD wYear;   WORD wMonth;   WORD wDayOfWeek;   WORD wDay;   WORD wHour;   WORD wMinute;   WORD wSecond;   WORD wMilliseconds; } SYSTEMTIME, *PSYSTEMTIME;
 

Constants


http://www.cplusplus.com/doc/tutorial/constants/

Literals

75         // decimal 0113       // octal 0x4b       // hexadecimal 


75 // int 75u // unsigned int 75l // long 75ul // unsigned long

3.14159 // 3.14159 // default double 6.02e23 // 6.02 x 10^23 1.6e-19 // 1.6 x 10^-19 3.0 // 3.0

 3.14159L   // long double 6.02e23f   // float 
 
 
 'z' 'p' "Hello world" "How do you do?" 
 
\n newline
\r carriage return
\t tab
\v vertical tab
\b backspace
\f form feed (page feed)
\a alert (beep)
\' single quote (')
\" double quote (")
\? question mark (?)
\\ backslash (\)


For example: 

1
2
3
4
'\n' '\t' "Left \t Right" "one\ntwo\nthree" 

More at link.

fmod

http://www.cplusplus.com/reference/clibrary/cmath/fmod/

     double fmod (      double numerator,      double denominator );       float fmod (       float numerator,       float denominator ); long double fmod ( long double numerator, long double denominator );
 

COleDatTime::COleDateTime

For older unmanaged code this handles the DATE type.



 COleDateTime( ) throw( );  COleDateTime(    const VARIANT& varSrc  ) throw( ); COleDateTime(    DATE dtSrc  ) throw( ); COleDateTime(    time_t timeSrc  ) throw( ); COleDateTime(    __time64_t timeSrc  ) throw( ); COleDateTime(    const SYSTEMTIME& systimeSrc  ) throw( ); COleDateTime(    const FILETIME& filetimeSrc  ) throw( ); COleDateTime(    int nYear,    int nMonth,    int nDay,    int nHour,    int nMin,    int nSec  ) throw( ); COleDateTime(    WORD wDosDate,    WORD wDosTime  ) throw( ); COleDateTime(       const DBTIMESTAMP& dbts ) throw();
 

Re: Time DBTIMESTAMP

 http://msdn.microsoft.com/en-us/library/c3zzz087(v=VS.80).aspx
 Function for getting the DBTIMESTAMP.
 bool GetAsDBTIMESTAMP(       DBTIMESTAMP& dbts ) const throw();

On Wed, Sep 15, 2010 at 9:41 AM, Stan Hughes <hughes.stan@gmail.com> wrote:

The fraction time is from 0 to 999,999,999.

Not really sure what good this does since you can only get it for the current time and any translation of time rounds to the nearest second.

DBTIMESTAMP

The DBTIMESTAMP structure typedef is defined as follows:

 typedef struct tagDBTIMESTAMP {     SHORT  year;     USHORT month;     USHORT day;     USHORT hour;     USHORT minute;     USHORT second;     ULONG  fraction } DBTIMESTAMP; 

Members

year
The year (0 to 9999) is measured from 0 A.D.
month
The month ranges from 1 to 12 representing January through December.
day
The day ranges from 1 to a maximum of 31, depending on the number of days in the month.
hour
The hour ranges from 0 to 23.
minute
The minute ranges from 0 to 59.
second
The second ranges from 0 to 59.
fraction
The fraction represents billionths of a second ranging from 0 to 999,999,999.

Time DBTIMESTAMP


The fraction time is from 0 to 999,999,999.

Not really sure what good this does since you can only get it for the current time and any translation of time rounds to the nearest second.

DBTIMESTAMP

The DBTIMESTAMP structure typedef is defined as follows:

 typedef struct tagDBTIMESTAMP {     SHORT  year;     USHORT month;     USHORT day;     USHORT hour;     USHORT minute;     USHORT second;     ULONG  fraction } DBTIMESTAMP; 

Members

year
The year (0 to 9999) is measured from 0 A.D.
month
The month ranges from 1 to 12 representing January through December.
day
The day ranges from 1 to a maximum of 31, depending on the number of days in the month.
hour
The hour ranges from 0 to 23.
minute
The minute ranges from 0 to 59.
second
The second ranges from 0 to 59.
fraction
The fraction represents billionths of a second ranging from 0 to 999,999,999.

Tuesday, September 07, 2010

DATE Delta

#include <FLOAT.H> // for the FLT_EPSILON unmanaged code

DATE date1;
DATE date2;

DATE dEpsilon = date1 - date2;

if (dEpsilon > FLT_EPSILON)
{
   COleDateTime clTempEpsilon (depsilon);
   CString strEpsilon = clTempEpsilon.Format("%H:%M:%S %m/%d%Y");
   TRACE (strEpsilon);
}

OLE Date/Time

http://www.newobjects.com/pages/ndl/SQLite2\SQL-OleDate.htm

Related to SQL.

Starts from 30 December 1899 00:00:00


It would seem that the newer, OLD DB Date/Time, SQL Server 2008 R2, support milliseconds.

'yyyy-mm-dd hh:mm:ss[.999]

The time structure is has greater resolution:

'hh:mm:ss[.9999999]


Time Management, Conversion of system time to CTime


CTime Class

8 bytes, with an upper limit of January 18, 2038.


Time Management

#define _BASE_YEAR 70L /* 1970 is the base year */
#define _MAX_YEAR 138: /* 2038 is the max year */

Note that the _make__time64_t() function insist that the year is between 1969 and 2038.  If it is not a error value of 22 is set and a value of -1 is returned.

This is slight inconsistent with the behavior of CTime which set the time to 1970 is the time is earlier than 1900 and than creates the CTime from system time with no error.

This is a know bug.

atltime.inl

if (sysTime.wYear < 1900)
{
   __time64_t time0 = 0L;
   CTime timeT(time0);
   ...
}
else
{
   CTime timeT(
      (int) sysTime.wYear, (int)sysTime,wMonth, (int) sysTime.wDay,
      (int) sysTime.wHour, (int)sysTime.wMinute, (int) sysTime,wSecond,
      nDST);
   ...
}

The SYSTEMTIME structure does have milliseconds, but it is not used by CTime.

::VariantTimeToSystemTime() also seem to be dropping the milliseconds.

Friday, September 03, 2010

Static Variables

http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=/com.ibm.xlcpp8a.doc/language/ref/cplr038.htm

 class C {       static int i;       static int j;       static int k;       static int l;       static int m;       static int n;       static int p;       static int q;       static int r;       static int s;       static int f() { return 0; }       int a; public:       C() { a = 0; }       };

 C c; int C::i = C::f();    // initialize with static member function int C::j = C::i;      // initialize with another static data member int C::k = c.f();     // initialize with member function from an object int C::l = c.j;       // initialize with data member from an object int C::s = c.a;       // initialize with nonstatic data member int C::r = 1;         // initialize with a constant value

 class Y : private C {} y;  int C::m = Y::f(); int C::n = Y::r; int C::p = y.r;       // error int C::q = y.f();     // error

Asserts, Exceptions

This is rather random, but I need to save the search results.


This is a discussion of when to use asserts and when to use exceptions.

Asserts only exist in debug.

Asserts terminate normal program execution.

Rule of thumb: 

Can the error be managed?
Yes - throw exception and handle it.
No - use assert.

Can you write a unit test that is able to throw the exception?
Yes - exception
No - assert

Are you protecting against a coding error?
Yes - assert
No - exception

Are you protecting against 'normal program' (i.e. file not writable etc.):
Yes - exception
No - assert

Exception example:


http://msdn.microsoft.com/en-us/library/ac9f67ah.aspx

 try {    throw CSomeOtherException(); } catch(...) {  // Handle all exceptions    // Respond (perhaps only partially) to exception    throw;       // Pass exception to some other handler }



Tuesday, August 31, 2010

CArray Members

http://msdn.microsoft.com/en-us/library/7y7b8fx3.aspx

GetCount, GetSize, GetUpperBound seem to be all the same.

CString Methods at MSDN

http://msdn.microsoft.com/ja-jp/library/ms908314.aspx

Can use 'Find' to find a sub string in another string.


Copying a BSTR to a CString.

Note that the BSTR must be converted to a _bstr_t first.  If this is not done you only get the first character of the BSTR in the new CString.


BSTR GetBSTR() 
{     
_bstr_t bstr1(_T("This is the test string."));          
BSTR bstr;      bstr = bstr1.copy();      
   return bstr; 
}     
CComBSTR GetComBSTR() 
{     
CComBSTR bstr("This is the test string.");      
   return bstr; 
}   

void CVbsDlg::ShowBSTR(BSTR bstr) 
{     
_bstr_t bstrStart(bstr);           
CString s;      
s.Format(_T("%s"), (LPCTSTR)bstrStart);      AfxMessageBox(s);  
} 

COleDateTime:: Format

http://msdn.microsoft.com/en-us/library/bab3feb1(VS.80).aspx

The lowest resolution of the formating is in seconds.


DATE Description and Use


This is a 8-byte floating-point number.  Days are represented by whole number increments starting with 30 December 1899.

0.00 is 20 Dec 1899, midnight
-1.00 is 29 December 1899, midnight
5.50 is 4 January 1900, noon.



For comparing two dates:

if ((date2 - date1)  < FLT_EPSILON)
{
      printf("same date");
}

FLT_EPSILON is defined in the LIMITS.H include file for .NET. 

Defined in FLOAT.H for unmanaged code.



Friday, August 27, 2010

CArray CString Example


http://www.codersource.net/mfc/mfc-tutorials/carray.aspx

#include <Afxwin.h>
#include <Afxtempl.h>

void main()
{

CString l_strValue;
CArray<CString,CString> l_CArray;

for(int i=0;i< 20; i++)
{

  //Use the CString format function to create different values 
  l_strValue.Format("Value %d",i);
  //Add the formatted CString to CArray
  l_CArray.Add(l_strValue);

}

}


#include <Afxwin.h>
#include <Afxtempl.h>

void main()
{

CString l_strValue;
CArray<CString,CString> l_CArray;

for(int i=0;i< 20; i++)
{

  //Use the CString format function to create different values 
  l_strValue.Format("Value %d",i);
  //Add the formatted CString to CArray
  l_CArray.Add(l_strValue);

}

//This part takes care of accessing and printing the data from CArray
CString l_strGetVal;
for(i=0;i<=l_CArray.GetUpperBound();i++)
{
    l_strGetVal = l_CArray.GetAt(i);
    printf("%s\n",l_strGetVal);
}
 

}


 l_CArray.RemoveAll() //This will remove all the elements from CArray

           l_CArray.RemoveAt(10); //This will remove the 10th element from CArray


Friday, July 09, 2010

Resetting Language Environment in VS 2008

This is done from the command line:

> DevEnv /resetuserdata

Your can than reselect the environment you want to use.



Tuesday, June 22, 2010

How to Set Properties Sheet in VS 2010

This took me a while to figure out.

First you need to be able to look at the property sheets.  They are ASCII files so you can look at them with an editor.  They have the file extension 'props'.

To access in VS 2010, select View ==> Property Manager option.

The property manager contains a list of projects.  Select the project you want to add a new or existing property sheet.  Right click on the project to see the options.  Adding are creating, puts the sheet in both debug and Release configurations.

Right click the property page and the property page will appear.  Select the User Macros property.  This displays a pane where you can add or remove macros.  The macro is added under both debug and release.




How to Ignore an Exception in C++

try
{
    // try something that might throw an exception here.
}
catch (...)
{
   // exception will be caught but ignored.
}