compliing errors with timestamp code
I've written a simple program that output's my name, course, and a time \ date stamp.
Having researched the code to do so, I've used the includes for both the ctime and time.h libraries.
I leveraged localtime_s and asctime_s to actually convert to string etc., and the program runs fine when I step through it in Visual Studio.
However, we're working with CGI and this program will ultimately be called up from the cgi-bin directory of a web server. The first thing
that needs to be done after copying it to the webserver is to compile it. That's where my problem occurs.
Using the console command: g++ lab5a.cpp -o lab5a.exe the file is supposed to compile and be converted to a .exe.
However, instead I receive errors:
localtime_s was not declared in this scope
asctime_s was not declared in this scope
I "believe" it's because of the included libraries, but not sure.
They are:
HTML Code:
#include <iostream>
#include <ctime>
#include <time.h>
Here's the actual timestamp block of code:
HTML Code:
//Begin date and time instructions
time_t curTime;
struct tm locTime;
const int TimeStrLen = 26;
char timeStr[ TimeStrLen ];
if ( ( -1 != time( &curTime ) ) // Seconds since 01-01-1970
&& ( 0 == localtime_s( &locTime, &curTime ) ) // Convert to local time
&& ( 0 == asctime_s( timeStr, TimeStrLen, &locTime ) ) // Convert to string
)
Of course, I go on to error check and complete the block, but you get the picture.
Can somebody shed light on the errors and what might stop this code from compiling.
All that I've read points to deprecated code, specifically in the _s lines.
Re: compliing errors with timestamp code
You don't include ctime and time.h. ctime and time.h are the same except that ctime has the declarations in namespace std (and includes time.h).
Re: compliing errors with timestamp code
Quote:
Originally Posted by tmcfadden
localtime_s was not declared in this scope
asctime_s was not declared in this scope
localtime_s and asctime_s are non-standard.
Re: compliing errors with timestamp code
As far as I know the _s variants of various functions are MS specifics. If you intend to compile your code on a *nix platform use the variant without _s instead.
Re: compliing errors with timestamp code
Quote:
Originally Posted by
2kaud
You don't include ctime and time.h. ctime and time.h are the same except that ctime has the declarations in namespace std (and includes time.h).
Right. - Actually I didn't include both.. I tried the first one, it didn't work, so I rem'd it out and tried the second.
One other post mentioned that those are "not standard," which is what I've read.
The question is, is there a "newer" method of extracting a date and time stamp?
Re: compliing errors with timestamp code
Quote:
Originally Posted by
tmcfadden
The question is, is there a "newer" method of extracting a date and time stamp?
If you can use boost, there's the date_time_io: http://www.boost.org/doc/libs/1_43_0...e_time_io.html
Otherwise, no; the standard C library is what you'll have to use.
Edit: Just discovered http://en.cppreference.com/w/cpp/io/manip/get_time if you have c++11 available