I am writing a program to put time-stamps on images taken with a camera. To do that I am using the Windows 7 system time. I have used GetSystemTimeAsFileTime()
in the code below:
FILETIME ft;
GetSystemTimeAsFileTime(&ft);
long long ll_now = (LONGLONG)ft.dwLowDateTime + ((LONGLONG)(ft.dwHighDateTime) << 32LL);
What I want to do is to get the number of seconds gone in the day (0- 86400) with millisecond resolution so it will be something like 12345.678. Is this the right way to do it? If so, how to I convert this integer to get the number of seconds gone in the current day? I will be displaying the time in a string and using fstream
to put the times in a text file.
Thanks
I don't know Window API
but the C++
standard libraries (since C++11) can be used like this:
#include <ctime>
#include <chrono>
#include <string>
#include <sstream>
#include <iomanip>
#include <iostream>
std::string stamp_secs_dot_ms()
{
using namespace std::chrono;
auto now = system_clock::now();
// tt stores time in seconds since epoch
std::time_t tt = system_clock::to_time_t(now);
// broken time as of now
std::tm bt = *std::localtime(&tt);
// alter broken time to the beginning of today
bt.tm_hour = 0;
bt.tm_min = 0;
bt.tm_sec = 0;
// convert broken time back into std::time_t
tt = std::mktime(&bt);
// start of today in system_clock units
auto start_of_today = system_clock::from_time_t(tt);
// today's duration in system clock units
auto length_of_today = now - start_of_today;
// seconds since start of today
seconds secs = duration_cast<seconds>(length_of_today); // whole seconds
// milliseconds since start of today
milliseconds ms = duration_cast<milliseconds>(length_of_today);
// subtract the number of seconds from the number of milliseconds
// to get the current millisecond
ms -= secs;
// build output string
std::ostringstream oss;
oss.fill('0');
oss << std::setw(5) << secs.count();
oss << '.' << std::setw(3) << ms.count();
return oss.str();
}
int main()
{
std::cout << stamp_secs_dot_ms() << '\n';
}
Example Output:
13641.509