// cpp 20
//c++ - Number of seconds since midnight - Stack Overflow
//https://stackoverflow.com/questions/61273498/number-of-seconds-since-midnight
int main(int argc, char** argv)
{
std::system("chcp 65001");
using namespace std;
using namespace std::chrono;
// chrono time point to string
auto chronoNow = chrono::system_clock::now();
cout << chronoNow << endl;
std::time_t tt = chrono::system_clock::to_time_t(chronoNow);
char buffer[32];
std::tm ttm;
localtime_s(&ttm, &tt);
strftime(buffer, 32, "%Y-%m-%d %H:%M:%S", &ttm);
cout << buffer << endl;
auto remainder = chrono::duration_cast<chrono::microseconds>(chronoNow.time_since_epoch()) % chrono::seconds(1);
cout << remainder.count() << endl;
string timeStr(buffer);
timeStr += std::format(".{:06d}", remainder.count());
cout << timeStr << endl;
// milliseconds since epoch
auto millisec_since_epoch = chrono::duration_cast<chrono::milliseconds>(chrono::system_clock::now().time_since_epoch());
cout << millisec_since_epoch << endl;
cout << millisec_since_epoch.count() << endl;
cout << millisec_since_epoch / std::chrono::milliseconds(1) << endl;
// milliseconds since start of the day
auto now = chrono::system_clock::now();
auto today = chrono::floor<chrono::days>(now);
auto hours_since_midninght = duration_cast<hours>(now - today);
int hours_since_midninght_int = hours_since_midninght.count();
cout << hours_since_midninght << endl;
cout << hours_since_midninght_int << endl;
// chrono in local zone
auto local_now = chrono::current_zone()->to_local(chrono::system_clock::now());
auto local_today = chrono::floor<chrono::days>(local_now);
auto local_hours_elapsed = chrono::duration_cast<chrono::hours>(local_now - local_today);
cout << local_hours_elapsed << endl;
cout << local_hours_elapsed.count() << endl;
return 0;
}
标签:cout,chrono,int,since,basic,cpp,now,local
From: https://www.cnblogs.com/yusisc/p/17158357.html