-
March 28th, 2013, 11:07 AM
#1
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.
-
March 28th, 2013, 12:02 PM
#2
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).
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
March 28th, 2013, 12:04 PM
#3
Re: compliing errors with timestamp code
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.
-
March 28th, 2013, 12:06 PM
#4
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.
-
March 28th, 2013, 12:26 PM
#5
Re: compliing errors with timestamp code
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?
-
March 28th, 2013, 01:36 PM
#6
Re: compliing errors with timestamp code
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
Last edited by Septic; March 28th, 2013 at 01:57 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|