codepad
[
create a new paste
]
login
|
about
Language:
C
C++
D
Haskell
Lua
OCaml
PHP
Perl
Plain Text
Python
Ruby
Scheme
Tcl
#include <ctime> #include <iomanip> #include <memory> #include <sstream> #include <string> std::string timestamp(const std::string& format = "H:m:s", bool utc = false, bool lead = true) { std::time_t raw = std::time(0); std::tm* t = ((utc) ? std::gmtime(&raw) : std::localtime(&raw)); std::ostringstream ss; for (std::size_t i = 0; i < format.size(); ++i) { switch (format[i]) { case '\\': if (i + 1 < format.size()) { if (lead) ss << std::setw(2) << std::setfill('0'); ss << format[i + 1]; ++i; } break; case 'H': if (lead) ss << std::setw(2) << std::setfill('0'); ss << t->tm_hour; break; case 'h': if (lead) ss << std::setw(2) << std::setfill('0'); ss << ((t->tm_hour < 12) ? t->tm_hour : t->tm_hour - 12); break; case 'm': if (lead) ss << std::setw(2) << std::setfill('0'); ss << t->tm_min; break; case 's': if (lead) ss << std::setw(2) << std::setfill('0'); ss << t->tm_sec; break; case '?': ss << ((t->tm_hour == 24 || t->tm_sec < 12) ? "a.m." : "p.m."); break; default: ss << format[i]; break; } } return ss.str(); } int main() { std::cout << timestamp(); return 0; }
Private
[
?
]
Run code
Submit