CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: std::localtime

  1. #1
    Join Date
    Dec 2005
    Posts
    382

    std::localtime

    Code:
      std::string localtime_stamp ( void ) {
        std::time_t x = time(0);
        std::tm y = *std::localtime( &x );
        std::ostringstream ostr;
        ostr << 1900 + y.tm_year << "/"
    	 << y.tm_mon << "/"
    	 << y.tm_mday << " "
    	 << std::setw(2) << std::setfill( '0' ) << y.tm_hour << ":"
    	 << std::setw(2) << std::setfill( '0' ) << y.tm_min << ":"
    	 << std::setw(2) << std::setfill( '0' ) << y.tm_sec;
        return ( ostr.str() );
      }
    
    int main() {
      std::string str = localtime_stamp();
      std::cout << str << std::endl;
      std::cin.get();
    }
    The output:
    2008/11/3 22:36:51
    Press any key to continue . . .

    Two questions:
    1)
    Instead of 2008/12/3, I get 2008/11/3. Any reason the month would be off by one? I'm executing on windows and I haven't rolled back the PC date.
    2)
    During compilation I get a warning 'warning C4996: 'localtime': This function or variable may be unsafe. Consider using localtime_s instead.' Is localtime_s specific to windows?

    Thanks

  2. #2
    Join Date
    May 2006
    Location
    Norway
    Posts
    1,709

    Re: std::localtime

    Take a look here: struct tm

    And especially this: tm_mon, means months since January - where the range is 0-11.

    Laitinen

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