-
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
-
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