Link to home
Start Free TrialLog in
Avatar of c_louise
c_louise

asked on

How to set COleDateTime

I have a dialog program and I want to get the current date and time by using COleDateTime. I then want to calculate thirty days in advance of the current time. I have this code which I got from another question in this forum but when I do a debug and a step through I cannot get the current time.
Any suggestions.

UpdateData(TRUE);
      char buffer[20];
      int DaysLeftBeforeExpires;
      COleDateTime timeStart=COleDateTime::GetCurrentTime();
      COleDateTimeSpan timeEnd= COleDateTimeSpan( 5, 0, 0, 0 );
      COleDateTime newtimeStart = timeStart + timeEnd;

any suggestions as to why it is not picking up the correct date. Also is this a good way to go about adding the date.

Louise
Avatar of captainkirk
captainkirk

The line
COleDateTime timeStart=COleDateTime::GetCurrentTime();

should give you the correct current date and time...

The line:

COleDateTimeSpan timeEnd= COleDateTimeSpan( 5, 0, 0, 0 );

will add only 5 days to your timeStart variable, though...

use timeStart.Format("%m/%d/%Y %H:%M:%S") to give you a string representation
of the date and time and see if it is correct... let me know what happens...
How do you know that it is not?
Add these lines just for testing:
CString str1 = timeStart.Format(_T("%A, %B %d, %Y"));

String str2 = newtimeStart.Format(_T("%A, %B %d, %Y"));

(Also, you are saying you want to add 30 days, but you are actually adding only 5, in COleDateTimeSpan( 5, 0, 0, 0 );

Felix.
Avatar of c_louise

ASKER

I tried using the CString and seeing what I was getting but I am still getting no value. The way I noticed first that I was not getting a value was that I did a step through and held the mouse over timeStart and newtimeStart but all I get back is timeStart ={...}. I realized after I had posted the question that I had a 5 instead of a 30, but that was not the main problem, the problem is that it will not give me the current date.

Louise
The "timeStart = {..}" just tells you that you are dealing with a structure that needs to be expanded to be looked at. Use the QuickWatch menu item in the debugger to look at all of the members. A caveat, though - a COleDateTime object will not show you anything "human readable" - this is why I suggested you use the Format() call to look at something you can read...
Hi there..
I am putting some code stuff that I have done to solve this problem..Pls make any comment if u don't find it help full with error . I will give u more information

Class Member defined...
COleDateTime m_odtStartDate;  COleDateTime m_odtStopDate;
Initalized in constructor
m_odtStartDate = COleDateTime::GetCurrentTime();

void OnDays(int nDays)
{
    UpdateData(true);
    m_odtStop = m_odtStart;
  COleDateTimeSpan dt(nDays,0,0,0);
    m_odtStop += dt;
    UpdateData(false);
}

Check this function with giving 30 = ndays...

I hope it will work...
Regards
Keshav
Could not get that function to work properly.

I did figure out that I was getting the correct time, and am back to this code, it will work when I put a 0 as the first parameter, but when I put 1, and then change the system clock, I do not get the message box.

Here is the code now

      int DaysLeftBeforeExpires;
      
      CTime timeStart=CTime::GetCurrentTime();

      CTimeSpan timeEnd = CTimeSpan(1,0,0,0);
      timeEnd= CTimeSpan( 1, 0, 0, 0 );
      CTime newtimeStart = timeStart + timeEnd;
      DaysLeftBeforeExpires = atoi(newtimeStart.Format("%j"))-atoi(timeStart.Format("%j"));

      if(DaysLeftBeforeExpires<=0)
      {
            MessageBox("This program has expired");
            
      
            return 1;
      }
Any other suggestions.

Louise
Louise - it looks like you want to store the start date somewhere, because every time you run the program, the CTime::GetCurrentTime() call will always give you today's date, and then you are calculating a delta from that. You will never see the dialog box!!!
So where would I store the start date. I thought of that but was not sure if that was the problem. Would I put it in an ini file or the registry or where.

Louise
Either one of those would work, or you could even store it in some data file. If this is for software security, I would make sure that, for any solution, it is encrypted somehow so that users cannot readily read it and change it.

Use WritePrivateProfileString() to output it to an INI file...

Use CRegkey::SetKeyValue() to write something to the Registry...

hope that helps...
That helps a lot, but now that I have it saved as an ini file, how do I count down the days until the time runs out. Say 5 days.

Here is the code I have for entering the current time into the ini file

CString strMainScreen;
      int DaysLeftBeforeExpires;
      CString d;
      CTime timeStart=CTime::GetCurrentTime();
      d = timeStart.Format("%m/%d/%Y %H:%M:%S");
      
      theApp.m_pszProfileName=(const char*)g_strMainScreen;

      theApp.WriteProfileString(strMainScreen, g_strMainScreenSection,d);
      
What next?
1) note that WriteProfileString() will save to Win.INI... as noted above, you might want to use WritePrivateProfileString() ao that you can use whatever INI file you want...

2) use GetPrivateProfileString() to retrieve your saved date..

CString csStartDate = _T("");

DWORD dwNumBytesReturned = GetPrivateProfileString(  szSectionName,           szKeyName,        
szDefault,        
(LPTSTR)csStartDate.GetBuffer(128) ,  // points to destination buffer
dwSize,              // size of destination buffer
_T("YourINIFileName")
);
 
csStartDate.ReleaseBuffer();

3) construct a start time (using COleDateTime because of the string parsing and for extended date range) from your retrieved date and compute difference between then and now:

COleDateTime ctStartTime;
COleDateTime ctNow;
ColeDateTimeSpan ctDelta;

int nDays = 0;  // total days until expiration

ctStartTime.ParseDateTime(csStartTime);
ctNow.GetCurrentTime();
ctDelta = ctNow - ctStartTime;

if (ctDelta.GetTotalDays() > nDays)
{
    // do something...
} // end if


I have not run this code, but the basics of it are there... let me know if you have any problems...


I tried this but it doesn't seem to be working, here is the code I have, perhaps you can see where I am going wrong. I get an assertion error

      char buffer[20];
      CString strMainScreen;
      CString d;
      CTime timeStart=CTime::GetCurrentTime();
      d = timeStart.Format("%m/%d/%Y %H:%M:%S");
      theApp.m_pszProfileName=(const char*)g_strMainScreen;

      theApp.WriteProfileString(strMainScreen, g_strMainScreenSection,d);
      
      
      CString csStartDate = d;
      CString szKeyName;
      CString szDefault;
      DWORD dwSize = 30;
      DWORD dwNumBytesReturned = GetPrivateProfileString(strMainScreen, szKeyName,  
      szDefault,        
      (LPTSTR)csStartDate.GetBuffer(128) ,  // points to destination buffer
      dwSize,              // size of destination buffer
      _T("MainScreen.pmb") );
 
      csStartDate.ReleaseBuffer();

      //3) construct a start time (using COleDateTime because of the string parsing and for extended date range) from your retrieved date and compute difference between then and now:

      COleDateTime ctStartTime;
      COleDateTime ctNow;       
      COleDateTimeSpan ctDelta;

      int nDays = 0;  // total days until expiration

      ctStartTime.ParseDateTime(csStartDate);
      ctNow.GetCurrentTime();
      ctDelta = ctNow - ctStartTime;

      if (ctDelta.GetTotalDays() > nDays)
      {
    MessageBox("This program has expired");
            CDialog::OnCancel();
      }


Louise
ASKER CERTIFIED SOLUTION
Avatar of captainkirk
captainkirk

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Comment accepted as answer
Thanks for all you help.
Louise
glad it worked for you...