ShiftDate_Month |
Post Reply |
Author | |
zitz
Senior Member Joined: 05 October 2008 Status: Offline Points: 112 |
Post Options
Thanks(0)
Posted: 28 February 2009 at 2:30am |
Hello. I use Xtreme ToolkitPro v13.0.0
Function works wrong at release (Visual C++ 6.0) for nMonthCount = -1, I think because inline For what? nMonthNew -= 12 * (nMonthCount / abs(nMonthCount)); It is unnecessary here i think. nMonthCount / abs(nMonthCount) == 1 always, or not? |
|
mdoubson
Senior Member Joined: 17 November 2008 Status: Offline Points: 1705 |
Post Options
Thanks(0)
|
I will check this - sorry - old strange class - i already fixed here serious bugs recently. But there was no complains on this function.
Btw - n / abs(n) can also be -1 and compiler shoud understand it.
|
|
zitz
Senior Member Joined: 05 October 2008 Status: Offline Points: 112 |
Post Options
Thanks(0)
|
No, it can't. If nMountCount < 0 then if (nMonthNew > 12) <---- FALSEI think it was without "if" in former times, for example int nYearNew = sysDate.wYear + nMonthCount / 12;and if nMountCount == 0 --> crash |
|
mdoubson
Senior Member Joined: 17 November 2008 Status: Offline Points: 1705 |
Post Options
Thanks(0)
|
No - I checked SVN - this version identical to Nov 2008 version (Rev 12.1)
I can cofirm the bug you found.
this is old code:
AFX_INLINE BOOL CXTPCalendarUtils::ShiftDate_Month(COleDateTime &refDate, int nMonthCount) int nYearNew = sysDate.wYear + nMonthCount / 12; if (nMonthNew > 12) ASSERT(nMonthNew >= 1 && nMonthNew <= 12); return COleDateTime::valid == refDate.SetDate(nYearNew, nMonthNew, 1); } Unfortunately % operator in MS doing not proper modulo arithm - in math it suppose to give you positive result or nul - in MS - NOT:
COleDateTime refDate(2000,1,1,0,0,0); SYSTEMTIME sysDate; if (GETASSYSTEMTIME_DT(refDate, sysDate)){ int nYearNew, nMonthNew, nMonthCount; for (nMonthCount = -20; nMonthCount < 20; nMonthCount++){
//modulo operation debug:
nYearNew = sysDate.wYear + (sysDate.wMonth + nMonthCount) / 12;
nMonthNew = 1 + ((int) (sysDate.wMonth) + nMonthCount - 1) % 12; ===> it give nMonthNew = -7 in first loop step!
//this is old code debug:
nYearNew = sysDate.wYear + nMonthCount / 12; nMonthNew = sysDate.wMonth + nMonthCount % 12; nMonthNew -= 12 * (nMonthCount / abs(nMonthCount)); nYearNew += nMonthCount / abs(nMonthCount); ASSERT(nMonthNew >= 1 && nMonthNew <= 12); this code give ASSERTION with nMonthNew = 13
} |
|
mdoubson
Senior Member Joined: 17 November 2008 Status: Offline Points: 1705 |
Post Options
Thanks(0)
|
The formula is very simple:
AFX_INLINE BOOL CXTPCalendarUtils::ShiftDate_Month(COleDateTime &refDate, int nMonthCount){ int nYearOld = refDate.GetYear(); int nMonthOld = refDate.GetMonth(); int nYearNew = nYearOld + (-12 + nMonthOld + nMonthCount) / 12; int nMonthCalc = nMonthOld + nMonthCount - 1; if (nMonthCalc < 0) int nMonthNew = 1 + nMonthCalc % 12; return COleDateTime::valid == refDate.SetDate(nYearNew, nMonthNew, 1); } I already update SVN. Thank you! |
|
Post Reply | |
Tweet
|
Forum Jump | Forum Permissions You cannot post new topics in this forum You cannot reply to topics in this forum You cannot delete your posts in this forum You cannot edit your posts in this forum You cannot create polls in this forum You cannot vote in polls in this forum |