Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

How to set COleDateTime

Posted on 2000-05-15
16
1,067 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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
 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Strange date stored 22 151
Named range not carried over 10 65
conditional code and condition difference 9 84
EvenOdd challenge 10 125
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
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.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

808 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