Solved

How to set COleDateTime

Posted on 2000-05-15
16
1,056 Views
Last Modified: 2013-11-20
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
Comment
Question by:c_louise
16 Comments
 
LVL 4

Expert Comment

by:captainkirk
ID: 2811761
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
 
LVL 1

Expert Comment

by:fbog
ID: 2811779
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
 

Author Comment

by:c_louise
ID: 2811837
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
 
LVL 4

Expert Comment

by:captainkirk
ID: 2811917
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
 
LVL 1

Expert Comment

by:kkarunakar
ID: 2811928
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
 

Author Comment

by:c_louise
ID: 2837125
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
 
LVL 4

Expert Comment

by:captainkirk
ID: 2837190
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
 

Author Comment

by:c_louise
ID: 2837233
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 4

Expert Comment

by:captainkirk
ID: 2837254
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
 

Author Comment

by:c_louise
ID: 2838024
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
 
LVL 4

Expert Comment

by:captainkirk
ID: 2838133
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
 

Author Comment

by:c_louise
ID: 2862192
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
 
LVL 4

Accepted Solution

by:
captainkirk earned 75 total points
ID: 2863691
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
 

Author Comment

by:c_louise
ID: 2867591
Comment accepted as answer
0
 

Author Comment

by:c_louise
ID: 2867592
Thanks for all you help.
Louise
0
 
LVL 4

Expert Comment

by:captainkirk
ID: 2868070
glad it worked for you...
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

744 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

16 Experts available now in Live!

Get 1:1 Help Now