C ++将系统时间作为一天中的秒数

时间:2015-09-07 01:06:50

标签: c++ time system-clock

我正在编写一个程序,在用相机拍摄的图像上加上时间戳。为此,我使用的是Windows 7系统时间。我在下面的代码中使用了GetSystemTimeAsFileTime()

FILETIME ft;
GetSystemTimeAsFileTime(&ft);
long long ll_now = (LONGLONG)ft.dwLowDateTime + ((LONGLONG)(ft.dwHighDateTime) << 32LL);

我想要做的是以毫秒的分辨率获得当天(0- 86400)的秒数,因此它将类似于12345.678。这是正确的方法吗?如果是这样,如何转换此整数以获取当天的秒数?我将在字符串中显示时间,并使用fstream将时间放在文本文件中。

由于

1 个答案:

答案 0 :(得分:0)

我不知道C++但是#include <ctime> #include <chrono> #include <string> #include <sstream> #include <iomanip> 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'; } 标准库(自C ++ 11以来)可以像这样使用:

13641.509

示例输出:

Caused by: org.postgresql.util.PSQLException: This connection has been closed.
at org.postgresql.jdbc2.AbstractJdbc2Connection.checkClosed(AbstractJdbc2Connection.java:822)
at org.postgresql.jdbc2.AbstractJdbc2Connection.rollback(AbstractJdbc2Connection.java:839)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.ibatis.common.jdbc.SimpleDataSource$SimplePooledConnection.invoke(SimpleDataSource.java:958)
at com.sun.proxy.$Proxy0.rollback(Unknown Source)
at com.ibatis.sqlmap.engine.transaction.jdbc.JdbcTransaction.rollback(JdbcTransaction.java:72)
at com.ibatis.sqlmap.engine.transaction.TransactionManager.end(TransactionManager.java:87)
at com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.endTransaction(SqlMapExecutorDelegate.java:734)
at com.ibatis.sqlmap.engine.impl.SqlMapSessionImpl.endTransaction(SqlMapSessionImpl.java:176)
at com.ibatis.sqlmap.engine.impl.SqlMapClientImpl.endTransaction(SqlMapClientImpl.java:153)
at com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.autoEndTransaction(SqlMapExecutorDelegate.java:835)
at com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.insert(SqlMapExecutorDelegate.java:410)
at com.ibatis.sqlmap.engine.impl.SqlMapSessionImpl.insert(SqlMapSessionImpl.java:82)
at com.ibatis.sqlmap.engine.impl.SqlMapClientImpl.insert(SqlMapClientImpl.java:58)
at mblib.dao.TrxLogDAO.insertLog(TrxLogDAO.java:32)
... 10 more