Help with asctime_s and localtime_s

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

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

Help with asctime_s and localtime_s

 
0
  #1
Feb 22nd, 2009
Hi. I'm making a class in an include file. I ran into some touble...When i try to compile, this is what comes up:

  1. c:\...\renew.h(25) : error C2660: 'localtime_s' : function does not take 1 arguments
  2. c:\...\renew.h(36) : error C2660: 'asctime_s' : function does not take 1 arguments

Here is the code for the class ([#include time.h is in the main cpp file)

  1. class renewBooks
  2. {
  3. public:
  4. int getItems()
  5. {
  6. cout << "Please scan the barcode of the book you would like to renew.";
  7. cin >> renewBarcode;
  8. processItems(renewBarcode);
  9. return false;
  10. }
  11.  
  12.  
  13. private:
  14. string renewBarcode;
  15. string toRenewItem;
  16.  
  17. int processItems(string toRenewItem)
  18. {
  19. char date[9];
  20. _strdate_s(date);
  21. char time2 [9];
  22. _strtime_s(time2);
  23.  
  24. time_t now = time(NULL);
  25. struct tm* tm = localtime_s(&now);
  26. tm->tm_mday +=14;
  27. // asctime(tm);
  28.  
  29. ofstream renew ("renewBooks.txt", std::ios::app);
  30.  
  31. renew << "Date: "
  32. << date
  33. << "\nTime: "
  34. << time2
  35. << "\nDue date: "
  36. << asctime_s(tm)
  37. << "\nBook barcode: "
  38. << toRenewItem;
  39.  
  40. renew.close();
  41. return false;
  42. }
  43. };
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,408
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: 1469
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
  #2
Feb 22nd, 2009
those error messages are correct. Did you look up those functions in msdn to see what are their parameters? If not, then you should do so. You have to be careful about those non-standard Microsoft functions. Microsoft declared many standard C functions depreciated, the c and c++ standards made no such statement. If you want to use the _s functions you need to also understand that other compilers don't support them. So if you turn in that assignment to your teacher and he/she doesn't use the same compiler then your program won't compile and you may get a lower grade.
Last edited by Ancient Dragon; Feb 22nd, 2009 at 10:23 pm.
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
  #3
Feb 22nd, 2009
Hi. First, I program for the fun of it and to make things of use to me. Second, I did look up the function in the time.h file and all i saw was...well...gibberish. I tried adding another argument (tm, false) to the code, but that just generated more errors. Please help.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,408
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: 1469
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
  #4
Feb 22nd, 2009
you won't find those functions in time.h because they are non-standard functions, as I previously said. You have to look them up at the Microsoft web site. Just use google and you will find them (just click that link).
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
  #5
Feb 23rd, 2009
Ok... so, I looked it up on themicrosoft's msdn website and this is what i wrote, but i still got ERREORS !
Here is the code and the errors

  1. class renewBooks
  2. {
  3. public:
  4. int getItems()
  5. {
  6. cout << "Please scan the barcode of the book you would like to renew:\n";
  7. cin >> renewBarcode;
  8. processItems(renewBarcode);
  9. return false;
  10. }
  11.  
  12.  
  13. private:
  14. string renewBarcode;
  15. string toRenewItem;
  16.  
  17. #include <time.h>
  18. #include <stdio.h>
  19. #include <string.h>
  20. int processItems(string toRenewItem)
  21. {
  22. struct tm newtime;
  23. char am_pm[] = "AM";
  24. _time64_t long_time;
  25. char timebuff [26];
  26. errno_t err;
  27.  
  28. //Get time as a 64 bit integer
  29. _time64( &long_time );
  30. // Convert to local time
  31. err = _localtime64_s( &newtime, &long_time );
  32. if (err)
  33. {
  34. printf( "Invalid argument to _local time64_s.");
  35. exit(1);
  36. }
  37.  
  38. if(newtime.tm_hour > 12)
  39. strcpy_s(am_pm, sizeof(am_pm), "PM");
  40. if (newtime.tm_hour > 12)
  41. newtime.tm_hour -= 12;
  42. if (newtime.tm_hour == 0)
  43. newtime.tm_hour = 12;
  44.  
  45. // Convert to an ASCII representation
  46. err = asctime_s(timebuff, 26, &newtime);
  47. if (err)
  48. {
  49. printf ( "Invalid argument to asctime_s.");
  50. exit(1);
  51. }
  52.  
  53. ofstream renew ("renewBooks.txt", std::ios::app);
  54.  
  55. renew << "Date: "
  56. << "\nTime: "
  57. << "\nDue date: ";
  58. renew printf( "%.19s %s\n", timebuff, am_pm );
  59. renew << "\nBook barcode: "
  60. << toRenewItem;
  61.  
  62. renew.close();
  63. return false;
  64. }
  65. };
  66.  
  67.  
  68.  
  69.  
  70. ------ Build started: Project: Lib. Application Framework, Configuration: Debug Win32 ------
  71. Compiling...
  72. mainSystemFramework.cpp
  73. c:\...\renew.h(24) : error C2065: '_time64_t' : undeclared identifier
  74. c:\...\renew.h(24) : error C2146: syntax error : missing ';' before identifier 'long_time'
  75. c:\...\renew.h(24) : error C2065: 'long_time' : undeclared identifier
  76. c:\...\renew.h(29) : error C2065: 'long_time' : undeclared identifier
  77. c:\...\renew.h(31) : error C2065: 'long_time' : undeclared identifier
  78. c:\...\renew.h(58) : error C2146: syntax error : missing ';' before identifier 'printf'
  79. Lib. Application Framework - 6 error(s), 0 warning(s)
  80. ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
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
  #6
Feb 24th, 2009
This secure C library stuff is in unstable state now so better don't waste a time with these *_s functions. They have the same functionality as old good standard functions.

You will have troubles with 32-bit time values after 2038 year only so better forgive that _time64 and time_64_t names. Don't worry, year 2038 won't turn up for a while yet ...

If you need platform-independend codes, define some obvious macros to convert *_s and time64 names to standard names in Linux environment - it's well-known and productive approach...
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,408
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: 1469
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
  #7
Feb 24th, 2009
I don't know about other compilers but Microsoft has already converted standard time_t to a 64-bit number unless you specifically ask for a 32-bit number
  1. //
  2. // extracted from time.h
  3. //
  4. #ifndef _TIME_T_DEFINED
  5. #ifdef _USE_32BIT_TIME_T
  6. typedef __time32_t time_t; /* time value */
  7. #else
  8. typedef __time64_t time_t; /* time value */
  9. #endif
  10. #define _TIME_T_DEFINED /* avoid multiple def's of time_t */
  11. #endif

So using _time64_t isn't necessary. Just use standard time_t and your program will be find.
Last edited by Ancient Dragon; Feb 24th, 2009 at 9:27 am.
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
  #8
Feb 24th, 2009
Thanks guys, you great. Now I have one more question... Is there any way to physically open the text file for the user to print and save as they wish after it has been wirtten to? If there is a way, that would be great. Thank you!
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
  #9
Feb 24th, 2009
On Windows try this (with #include <windows.h>, of course):
  1. void editFile(const char* filepath)
  2. {
  3. if (filepath) {
  4. HWND wnd = ::GetDesktopWindow();
  5. ::ShellExecute(wnd,"open",filepath,0,0,0);
  6. }
  7. }
Let a user plays with his/her preferred text editor...
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
  #10
Feb 25th, 2009
Ot, then, i tried to add days to my date by putting in:

  1. newtime.tm_mday += 14;

and then, I got a huge runtime error. I took it out, and it was fine. Is there a better way?
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
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC