Solved

Adding a day not working

Posted on 2004-10-15
4
193 Views
Last Modified: 2010-04-24

I have this in the declarations:
CTimeSpan                  tTimeSpanOneDay(1, 0 , 0 , 0) ;

Later on I have this:
CTime      dNewNextPayDate(nNewYear, nNewMonth, nNewDay , 0, 0, 0) ;
tRetNextPayDate = dNewNextPayDate ;

tRetNextPayDate  is equal to 10/31/2004 at this point.

When I do this:
tRetNextPayDate = tRetNextPayDate +  tTimeSpanOneDay;

tRetNextPayDate still has 10/31/2004 in it.

If I create the date like this:
CTime      dNewNextPayDate(nNewYear, nNewMonth, nNewDay , 12, 0, 0) ;  <-- added the 12

Then the date addition yeilds the correct new date, 11/1/2004.

What's going on here?
0
Comment
Question by:bfeddish
  • 2
  • 2
4 Comments
 
LVL 16

Expert Comment

by:nonubik
ID: 12320567
I tried this code:

CTimeSpan               tTimeSpanOneDay(1, 0 , 0 , 0) ;

      CTime     dNewNextPayDate(2004, 10, 31 , 0, 0, 0) ;
      CTime      tRetNextPayDate = dNewNextPayDate ;

      //tRetNextPayDate  is equal to 10/31/2004 at this point.
      int iD = tRetNextPayDate.GetDay();
      int iy = tRetNextPayDate.GetYear();
      int im = tRetNextPayDate.GetMonth();
      int ih = tRetNextPayDate.GetHour();

      // here iD = 31, iy=2004, im=10, ih=0
      tRetNextPayDate = tRetNextPayDate +  tTimeSpanOneDay;

      iD = tRetNextPayDate.GetDay();
      iy = tRetNextPayDate.GetYear();
      im = tRetNextPayDate.GetMonth();
      ih = tRetNextPayDate.GetHour();
      // here iD = 1, iy=2004, im=11, ih=0

So it works on my side.
0
 
LVL 19

Accepted Solution

by:
drichards earned 200 total points
ID: 12327854
This is an odd one.  THe results are different most likely because of your locales.  CTime takes into account daylight savings time.  So in the original case (and mine) CTime is really 00:00:00 10/31/2004 daylight time, and the result is 23:00:00 10/31/2004 standard time since daylight time ends at 02:00:00 10/31/2004.  Apparently nonubik's locale settings are such that there is no daylight time involved.  Unfortunately, there does not appear to be an easy way to fix this since the calendar day is 25 hours long over the daylight-to-standard switch and CTime does not have a "IsDaylight()' function.

I'll give it some thought.  Maybe someone else has an idea?
0
 
LVL 16

Assisted Solution

by:nonubik
nonubik earned 100 total points
ID: 12327917
Hmm, in the light of "daylight" I see the problem different :)
I found out that: http://support.microsoft.com/default.aspx?scid=kb;en-us;109790
Maybe you can use the second workaround and turn off the Daylight Savings Time.
0
 
LVL 19

Expert Comment

by:drichards
ID: 12327987
OK, here's what I came up with:

1) Use the MS workaround from the Knowledge Base article - you'll have to be careful about side effects on this one.

2) Use ColeDateTime/COleDateTimeSpan instead (doesn't use daylight time)

3) If you don't care about the time portion (you're just doing full day calculations) set the initial CTime time to 12:00:00.  Your time will fluctuate +- 1 hour on the daylight changeovers, but the date will be correct.  If you care about partial days, this will not work.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now