how to print current local time in one line of code?

as the title and just only use C++98.
and as better as format it!
thx!
Dunno if this counts as "one line of code" but...
1
2
3
4
5
6
7
8
9
10
#include <ctime>
#include <iostream>

const time_t ctt = time(0);

int main()
{
  std::cout << asctime(localtime(&ctt)) << std::endl;
  return 0;
}
thank u!
i want to log something included time with std::ofstream, so befor calling asctime(localtime(&ctt)); i should call time(0), so it's not "one line of code", but thank u again!
> i want to log something included time with std::ofstream

Write a function, perhaps?
1
2
3
4
5
6
7
std::string now( const char* format = "%c" )
{
    std::time_t t = std::time(0) ;
    char cstr[128] ;
    std::strftime( cstr, sizeof(cstr), format, std::localtime(&t) ) ;
    return cstr ;
}


And then:
1
2
3
std::cout << now() << '\n' ;
file << "some log message - posted at:" << now( "%I:%M:%S %p %Z" ) << '\n' ;
// etc. 

thank you all!
and i known if there no other code u can not output current time with one line of code in C++.

some disappoint.
Topic archived. No new replies allowed.