说明
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
|
|
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| #define _CRT_SECURE_NO_WARNINGS #include <ctime> #include <iostream>
int main() { time_t tm1 = std::time(nullptr); clock_t cl1 = std::clock(); char* pc = std::ctime(&tm1);
std::cout << "time:\t" << tm1 << std::endl; std::cout << "clock:\t" << cl1 << std::endl; std::cout << "ctime:\t" << pc << std::endl;
char buffer[80]; wchar_t wcbuffer[80]; std::cout << "local " << std::endl; std::tm* plocal = std::localtime(&tm1); time_t mk1 = std::mktime(plocal); std::cout << "mktime:\t" << mk1 << std::endl; std::strftime(buffer, 80, "%Y-%m-%d %H:%M:%S", plocal); std::cout << "strftime:\t" << buffer << std::endl; std::wcsftime(wcbuffer, 80, L"%Y-%m-%d %H:%M:%S", plocal); std::wcout << L"wcsftime:\t" << wcbuffer << std::endl; char* asc = std::asctime(plocal); std::cout << "asctime:\t" << asc << std::endl;
std::cout << "Universal Coordinated" << std::endl; std::tm* ptime = std::gmtime(&tm1); time_t mk2 = std::mktime(ptime); std::cout << "mktime:\t" << mk2 << std::endl; std::strftime(buffer, 80, "%Y-%m-%d %H:%M:%S", ptime); std::cout << "strftime:\t" << buffer << std::endl; std::wcsftime(wcbuffer, 80, L"%Y-%m-%d %H:%M:%S", ptime); std::wcout << L"wcsftime:\t" << wcbuffer << std::endl; char* asctime = std::asctime(ptime); std::cout << "asctime:\t" << asctime << std::endl;
timespec ts; timespec_get(&ts, TIME_UTC); std::strftime(buffer, sizeof(buffer), "%D %T", std::gmtime(&ts.tv_sec)); std::cout << "Current time:\t" << buffer << '.' << ts.tv_nsec << " UTC" << std::endl; time_t tm2 = std::time(nullptr); double d = std::difftime(tm2, tm1); std::cout << "Wall time passed:\t " << d << " s." << std::endl; return 0; }
|
参考
https://en.cppreference.com/w/