Help with asctime_s and localtime_s

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2005
Posts: 15,648
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1498
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Help with asctime_s and localtime_s

 
0
  #11
Feb 25th, 2009
After making that adjustment you have to call mktime() to normalize the structure. Otherwise, I don't know what you mean, so post latest code.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 46
Reputation: cppnewb is an unknown quantity at this point 
Solved Threads: 0
cppnewb's Avatar
cppnewb cppnewb is offline Offline
Light Poster

Re: Help with asctime_s and localtime_s

 
0
  #12
Feb 25th, 2009
Ok then... Here is the latest code (with the thing that causes the runtime error):

class renewBooks
{
public:
	int getItems()
	{
		cout << "Please scan the barcode of the book you would like to renew:\n";
		cin >> renewBarcode;
		processItems(renewBarcode);
		return false;
	}


private:
	string renewBarcode;
	string toRenewItem;
		
#include <time.h>
#include <stdio.h>
#include <string.h>
	int processItems(string toRenewItem)
	{
   		struct tm newtime;
		char am_pm[] = "AM";
		time_t long_time;
		char timebuff [26];
		errno_t err;

		//Get time as a 64 bit integer
		time( &long_time );
		// Convert to local time
		err = _localtime64_s( &newtime, &long_time );
		if (err)
		{
			printf( "Invalid argument to _local time64_s.");
			exit(1);
		}

		if(newtime.tm_hour > 12)
			strcpy_s(am_pm, sizeof(am_pm), "PM");
		if (newtime.tm_hour > 12)
			newtime.tm_hour -= 12;
		if (newtime.tm_hour == 0)
			newtime.tm_hour = 12;

		// This is where the error is:[
		newtime.tm_mday += 14;
		// ]This is where the error ends
		
		// Convert to an ASCII representation
		err = asctime_s(timebuff, 26, &newtime);
		if (err)
		{
			printf ( "Invalid argument to asctime_s.");
			exit(1);
		}
			
		ofstream renew ("renewBooks.txt", std::ios::app);

		renew << "Date: "
			  << "\nTime: "
			  << "\nDue date: ";
	    renew printf( "%.19s %s\n", timebuff, am_pm );
	    renew << "\nBook barcode: "
			  << toRenewItem;
		
		renew.close();
		return false;
	}
};
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Help with asctime_s and localtime_s

 
0
  #13
Feb 25th, 2009
It's not newtime.tm_mday += 14; statement failed: the next one. The asctime_s assertion failed when the function processes tm_mday == 40. Obviously, this future day of month (26+14) is invalid.

To normalize "moved forward" newtime structure add some conversions:
  1. newtime.tm_mday += 14;
  2. // mktime can accept "bad" day of month
  3. time_t xtime = mktime(&newtime);
  4. // refresh newtime structure
  5. _localtime64_s(&newtime,&xtime);
Of course, you may rewrite this snippet with those funny *_s "safety" functions and long time types...
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 46
Reputation: cppnewb is an unknown quantity at this point 
Solved Threads: 0
cppnewb's Avatar
cppnewb cppnewb is offline Offline
Light Poster

Re: Help with asctime_s and localtime_s

 
0
  #14
Feb 26th, 2009
Thank you soooooooo much guys to take the time out of your day to help me with my silly questions. +1 reputation
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum


Views: 1569 | Replies: 13
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC