• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1117
  • Last Modified:

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
0
c_louise
Asked:
c_louise
1 Solution
 
captainkirkCommented:
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...
0
 
fbogCommented:
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.
0
 
c_louiseAuthor Commented:
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
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
captainkirkCommented:
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...
0
 
kkarunakarCommented:
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
0
 
c_louiseAuthor Commented:
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
0
 
captainkirkCommented:
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!!!
0
 
c_louiseAuthor Commented:
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
0
 
captainkirkCommented:
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...
0
 
c_louiseAuthor Commented:
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?
0
 
captainkirkCommented:
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...


0
 
c_louiseAuthor Commented:
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
0
 
captainkirkCommented:
Louise - here's a revamped section of code that reads from "MainScreen.ini" which, for convenience, I put on the root of my D drive... it reads the start date from the ini file and checks to see if a 30 day period has elapsed and brings up a dialog if so.


char g_strMainScreenSection[128] =  _T("MainScreenSection");  // section name in INI file

CString  strMainScreen;
CString  csStartDate = _T("");
CString  csEndDate   = _T("");
CString  csDefault   = _T("");
CString  csKeyName   = _T("StartDate");

COleDateTime       ctStartDate;
COleDateTime       ctNow;
COleDateTimeSpan ctDelta;

COleDateTime ctEndDate = COleDateTime::GetCurrentTime();
csEndDate = ctEndDate.Format(_T("%m/%d/%Y %H:%M:%S"));
csDefault = csEndDate;


DWORD dwNumBytesReturned = GetPrivateProfileString(g_strMainScreenSection,
csKeyName,
csDefault,
(LPTSTR)(LPCSTR)csStartDate,  
128,              
_T("d:\\MainScreen.ini"));
   

int nDays = 30;  // total days until expiration - say the program had a 30 day trial period...

ctStartDate.ParseDateTime(csStartDate);
ctDelta = ctEndDate - ctStartDate;

int nTotDays = (int)ctDelta.GetTotalDays();

if (nTotDays > nDays)
{
     AfxMessageBox("This program has expired");
} // end if



hope that helps...
0
 
c_louiseAuthor Commented:
Comment accepted as answer
0
 
c_louiseAuthor Commented:
Thanks for all you help.
Louise
0
 
captainkirkCommented:
glad it worked for you...
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now