CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Feb 2013
    Posts
    30

    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.

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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)

  3. #3
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  4. #4
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    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.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  5. #5
    Join Date
    Feb 2013
    Posts
    30

    Re: compliing errors with timestamp code

    Quote Originally Posted by 2kaud View Post
    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?

  6. #6
    Join Date
    Jul 2010
    Posts
    6

    Re: compliing errors with timestamp code

    Quote Originally Posted by tmcfadden View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured