C++ 获得毫秒的时间
我需要从计时器中获取毫秒C++ 获得毫秒的时间,c++,C++,我需要从计时器中获取毫秒 // get timer part time_t timer = time(NULL); struct tm now = *localtime( &timer ); char timestamp[256]; // format date time strftime(timestamp, sizeof(timestamp), "%Y-%m-%d_%H.%M.%S", &now); 我想在C#中做到这一点:
// get timer part
time_t timer = time(NULL);
struct tm now = *localtime( &timer );
char timestamp[256];
// format date time
strftime(timestamp, sizeof(timestamp), "%Y-%m-%d_%H.%M.%S", &now);
我想在C#中做到这一点:
<>我尝试使用%F或%F,但它确实与C++兼容。如何从tm获取毫秒的%f?您可以使用
boost::posix\u time::ptime
class。
它的。有函数getTimeOfDay()。以毫秒为单位返回时间 检查此处:
#包括
typedef std::chrono::系统时钟;
自动现在=时钟::现在();
自动秒=标准::计时::时间\点\投射(现在);
自动分数=现在-秒;
time\u t cnow=时钟::到时间(现在);
然后你可以以秒的精度打印出时间,然后打印出分数代表的内容。可以是毫秒、微秒或其他。要具体获取毫秒,请执行以下操作:
auto milliseconds = std::chrono::duration_cast<std::chrono::milliseconds>(fraction);
std::cout << milliseconds.count() << '\n';
auto毫秒=std::chrono::duration\u cast(分数);
std::cout在Windows上使用Win32 API
会给你毫秒。那么,你应该用它来获得时间。
像这样:
#include <windows.h>
int main()
{
SYSTEMTIME stime;
//structure to store system time (in usual time format)
FILETIME ltime;
//structure to store local time (local time in 64 bits)
FILETIME ftTimeStamp;
char TimeStamp[256];//to store TimeStamp information
GetSystemTimeAsFileTime(&ftTimeStamp); //Gets the current system time
FileTimeToLocalFileTime (&ftTimeStamp,<ime);//convert in local time and store in ltime
FileTimeToSystemTime(<ime,&stime);//convert in system time and store in stime
sprintf(TimeStamp, "%d:%d:%d:%d, %d.%d.%d",stime.wHour,stime.wMinute,stime.wSecond,
stime.wMilliseconds, stime.wDay,stime.wMonth,stime.wYear);
printf(TimeStamp);
return 0;
}
#包括
int main()
{
系统时间;
//存储系统时间的结构(通常的时间格式)
文件时间;
//存储本地时间的结构(本地时间为64位)
文件时间标签;
char TimeStamp[256];//用于存储时间戳信息
GetSystemTimeAsFileTime(&ftTimeStamp);//获取当前系统时间
FileTimeToLocalFileTime(&ftTimeStamp,<ime);//转换为本地时间并存储在ltime中
FileTimeToSystemTime(<ime,&stime);//在系统时间中转换并存储在stime中
sprintf(时间戳,%d:%d:%d:%d,%d.%d.%d),时间间隔wHour,时间间隔wMinute,时间间隔wssecond,
时间W毫秒,时间W天,时间W月,时间wYear);
printf(时间戳);
返回0;
}
我个人使用这个:这里是另一个c++11答案:
#include <iostream>
#include <iomanip>
#include <chrono>
#ifdef WIN32
#define localtime_r(_Time, _Tm) localtime_s(_Tm, _Time)
#endif
int main()
{
tm localTime;
std::chrono::system_clock::time_point t = std::chrono::system_clock::now();
time_t now = std::chrono::system_clock::to_time_t(t);
localtime_r(&now, &localTime);
const std::chrono::duration<double> tse = t.time_since_epoch();
std::chrono::seconds::rep milliseconds = std::chrono::duration_cast<std::chrono::milliseconds>(tse).count() % 1000;
std::cout << (1900 + localTime.tm_year) << '-'
<< std::setfill('0') << std::setw(2) << (localTime.tm_mon + 1) << '-'
<< std::setfill('0') << std::setw(2) << localTime.tm_mday << ' '
<< std::setfill('0') << std::setw(2) << localTime.tm_hour << ':'
<< std::setfill('0') << std::setw(2) << localTime.tm_min << ':'
<< std::setfill('0') << std::setw(2) << localTime.tm_sec << '.'
<< std::setfill('0') << std::setw(3) << milliseconds
<< std::endl;
}
#包括
#包括
#包括
#ifdef WIN32
#定义localtime\r(\u Time,\u Tm)localtime\u s(\u Tm,\u Time)
#恩迪夫
int main()
{
tm本地时间;
std::chrono::system_clock::time_point t=std::chrono::system_clock::now();
time\u t now=std::chrono::system\u clock::to\u time\u t(t);
本地时间(现在和本地时间);
const std::chrono::duration tse=t.time_-since_-epoch();
std::chrono::seconds::rep毫秒=std::chrono::duration_cast(tse).count()%1000;
std::cout在MS Visual Studio c/c++下包括sys/timeb.h和use _ftime(_ftime_s)。
检索包含时间结构和毫秒的结构timeb(_ftime64),请参见MSDN
#包括
结构\u timeb timebuffer;
_ftime(时间缓冲);
timebuffer.millitm;//毫秒
timebuffer.time;//与结构时间相同
尝试使用以下代码:
struct tvTime;
gettimeofday(&tvTime, NULL);
int iTotal_seconds = tvTime.tv_sec;
struct tm *ptm = localtime((const time_t *) & iTotal_seconds);
int iHour = ptm->tm_hour;;
int iMinute = ptm->tm_min;
int iSecond = ptm->tm_sec;
int iMilliSec = tvTime.tv_usec / 1000;
int iMicroSec = tvTime.tv_usec;
time()
相反。这需要仅在Windows上工作还是跨平台工作?对于unix,请查看我在此处发布的另一个答案中的借用。您可以将其分解为毫秒。我不想因为使用时间而获得boost库的依赖性这是C++11方法据我所知,您无法直接使用请对时间点应用duration\u cast
。@KyleStrand你说得对。我已经发布了一个更正。仔细研究了一下,我想你要找的是time\u point\u cast
。从2018-05-16开始,链接是正常的。显示的代码有严重缺陷。它由4个标题和int main(void){char buffer[30]组成;struct timeval tv;time\t curtime;gettimeofday(&tv,NULL);curtime=tv.tv\u sec;strftime(buffer,30,“%m-%d-%Y%t.”,localtime(&curtime));printf(“%s%ld\n”,buffer,tv.tv\u usec);返回0;}
--使用%ld
格式化微秒(a)无法满足问题的毫秒格式化目标;(b)无法精确打印微秒。当tv\u usec
保持101微秒时,它打印.101
,而不是.000101
-printf(“%s%.3ld\n”,buffer,tv.tv\u usec/1000”);
此代码不编译。您可能指的是structTimeVal tvTime;
,这样就有机会编译。
#include <iostream>
#include <iomanip>
#include <chrono>
#ifdef WIN32
#define localtime_r(_Time, _Tm) localtime_s(_Tm, _Time)
#endif
int main()
{
tm localTime;
std::chrono::system_clock::time_point t = std::chrono::system_clock::now();
time_t now = std::chrono::system_clock::to_time_t(t);
localtime_r(&now, &localTime);
const std::chrono::duration<double> tse = t.time_since_epoch();
std::chrono::seconds::rep milliseconds = std::chrono::duration_cast<std::chrono::milliseconds>(tse).count() % 1000;
std::cout << (1900 + localTime.tm_year) << '-'
<< std::setfill('0') << std::setw(2) << (localTime.tm_mon + 1) << '-'
<< std::setfill('0') << std::setw(2) << localTime.tm_mday << ' '
<< std::setfill('0') << std::setw(2) << localTime.tm_hour << ':'
<< std::setfill('0') << std::setw(2) << localTime.tm_min << ':'
<< std::setfill('0') << std::setw(2) << localTime.tm_sec << '.'
<< std::setfill('0') << std::setw(3) << milliseconds
<< std::endl;
}
#include <sys/timeb.h>
struct _timeb timebuffer;
_ftime(&timebuffer);
timebuffer.millitm; //milli seconds
timebuffer.time; //the same like struct time_t
struct tvTime;
gettimeofday(&tvTime, NULL);
int iTotal_seconds = tvTime.tv_sec;
struct tm *ptm = localtime((const time_t *) & iTotal_seconds);
int iHour = ptm->tm_hour;;
int iMinute = ptm->tm_min;
int iSecond = ptm->tm_sec;
int iMilliSec = tvTime.tv_usec / 1000;
int iMicroSec = tvTime.tv_usec;