捕获以毫秒为单位的时间

发布于 2024-07-27 13:28:57 字数 463 浏览 2 评论 0原文

下面的代码用于打印日志中的时间:

#define PRINTTIME() struct tm  * tmptime;
time_t     tmpGetTime;
time(&tmpGetTime);
tmptime = localtime(&tmpGetTime);
cout << tmptime->tm_mday << "/" <<tmptime->tm_mon+1 << "/" << 1900+tmptime->tm_year << " " << tmptime->tm_hour << ":" << tmptime->tm_min << ":" << tmptime->tm_sec<<">>";

有什么方法可以添加毫秒吗?

The following piece of code is used to print the time in the logs:

#define PRINTTIME() struct tm  * tmptime;
time_t     tmpGetTime;
time(&tmpGetTime);
tmptime = localtime(&tmpGetTime);
cout << tmptime->tm_mday << "/" <<tmptime->tm_mon+1 << "/" << 1900+tmptime->tm_year << " " << tmptime->tm_hour << ":" << tmptime->tm_min << ":" << tmptime->tm_sec<<">>";

Is there any way to add milliseconds to this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

但可醉心 2024-08-03 13:28:57

要获得毫秒精度,您必须使用特定于您的操作系统的系统调用。

在 Linux 中,您可以使用

#include <sys/time.h>

timeval tv;
gettimeofday(&tv, 0);
// then convert struct tv to your needed ms precision

具有微秒精度的 timeval

在 Windows 中,您可以使用:

#include <Windows.h>

SYSTEMTIME st;
GetSystemTime(&st);
// then convert st to your precision needs

当然您可以使用 Boost 来为您做到这一点:)

To have millisecond precision you have to use system calls specific to your OS.

In Linux you can use

#include <sys/time.h>

timeval tv;
gettimeofday(&tv, 0);
// then convert struct tv to your needed ms precision

timeval has microsecond precision.

In Windows you can use:

#include <Windows.h>

SYSTEMTIME st;
GetSystemTime(&st);
// then convert st to your precision needs

Of course you can use Boost to do that for you :)

茶底世界 2024-08-03 13:28:57

//C++11 风格:

cout << "Time in Milliseconds =" << 
 chrono::duration_cast<chrono::milliseconds>(chrono::steady_clock::now().time_since_epoch()).count() 
 << std::endl;

cout << "Time in MicroSeconds=" << 
 chrono::duration_cast<chrono::microseconds>(chrono::steady_clock::now().time_since_epoch()).count() 
 << std::endl;

//C++11 Style:

cout << "Time in Milliseconds =" << 
 chrono::duration_cast<chrono::milliseconds>(chrono::steady_clock::now().time_since_epoch()).count() 
 << std::endl;

cout << "Time in MicroSeconds=" << 
 chrono::duration_cast<chrono::microseconds>(chrono::steady_clock::now().time_since_epoch()).count() 
 << std::endl;
寂寞陪衬 2024-08-03 13:28:57

您需要一个更高分辨率的计时器才能捕获毫秒。 试试这个:

int cloc = clock();
//do something that takes a few milliseconds
cout << (clock() - cloc) << endl;

这当然取决于您的操作系统。

You need a timer with a higher resolution in order to capture milliseconds. Try this:

int cloc = clock();
//do something that takes a few milliseconds
cout << (clock() - cloc) << endl;

This is of course dependent on your OS.

猥琐帝 2024-08-03 13:28:57

高分辨率计时器在 Linux 风格平台上通常是 gettimeofday,在 Windows 平台上通常是 QueryPerformanceCounter。

您应该意识到,对单个操作的持续时间进行计时(即使使用高分辨率计时器)也不会产生准确的结果。 有太多的随机因素在起作用。 为了获得可靠的计时信息,您应该循环运行要计时的任务并计算平均任务时间。 对于这种类型的计时,clock() 函数应该足够了。

The high resolution timers are usually gettimeofday on Linux style platforms and QueryPerformanceCounter on Windows.

You should be aware that timing the duration of a single operation (even with a high resolution timer) will not yield accurate results. There are too many random factors at play. To get reliable timing information, you should run the task to be timed in a loop and compute the average task time. For this type of timing, the clock() function should be sufficient.

泪意 2024-08-03 13:28:57

如果您不想使用任何特定于操作系统的代码,可以使用 ACE 包,它为大多数标准操作系统提供 ACE_OS::gettimeofday 函数。
例如:

ACE_Time_Value startTime = ACE_OS::gettimeofday();

do_something();

ACE_Time_Value endTime = ACE_OS::gettimeofday();

cout << "Elapsed time: " << (endTime.sec() - startTime.sec()) << " seconds and " << double(endTime.usec() - startTime.usec()) / 1000 << " milliseconds." << endl;

无论您的操作系统如何,此代码都可以工作(只要 ACE 支持此操作系统)。

If you don't want to use any OS-specific code, you can use the ACE package which supplies the ACE_OS::gettimeofday function for most standard operating systems.
For example:

ACE_Time_Value startTime = ACE_OS::gettimeofday();

do_something();

ACE_Time_Value endTime = ACE_OS::gettimeofday();

cout << "Elapsed time: " << (endTime.sec() - startTime.sec()) << " seconds and " << double(endTime.usec() - startTime.usec()) / 1000 << " milliseconds." << endl;

This code will work regardless of your OS (as long as ACE supports this OS).

断念 2024-08-03 13:28:57

在 Ubuntu 16.04 中,这对我有用...

const std::string currentDateTime() {
   char            fmt[64], buf[64];
   struct timeval  tv;
   struct tm       *tm;

   gettimeofday(&tv, NULL);
   tm = localtime(&tv.tv_sec);
   strftime(fmt, sizeof fmt, "%Y-%m-%d %H:%M:%S.%%06u", tm);
   snprintf(buf, sizeof buf, fmt, tv.tv_usec);
   return buf;
}

然后,...

std::cout << currentDateTime();

我得到...

2016-12-29 11:09:55.331008

In Ubuntu 16.04 this worked for me...

const std::string currentDateTime() {
   char            fmt[64], buf[64];
   struct timeval  tv;
   struct tm       *tm;

   gettimeofday(&tv, NULL);
   tm = localtime(&tv.tv_sec);
   strftime(fmt, sizeof fmt, "%Y-%m-%d %H:%M:%S.%%06u", tm);
   snprintf(buf, sizeof buf, fmt, tv.tv_usec);
   return buf;
}

Then, with...

std::cout << currentDateTime();

I get...

2016-12-29 11:09:55.331008
请止步禁区 2024-08-03 13:28:57

使用 C++11 或 C++14 和这个免费开源库

#include "tz.h"
#include <iostream>

int
main()
{
    using namespace date;
    using namespace std;
    using namespace std::chrono;
    auto now = make_zoned(current_zone(), floor<milliseconds>(system_clock::now()));
    cout << format("%e/%m/%Y %T", now) << '\n';
}

这只是为我输出:

16/01/2017 15:34:32.167

这是我当前的本地日期和时间,精确到毫秒。 通过消除 floor(),您将自动获得 system_clock 的精度。

如果您想要结果为 UTC 时间戳而不是本地时间戳,那就更简单了:

    auto now = floor<milliseconds>(system_clock::now());
    cout << format("%e/%m/%Y %T", now) << '\n';

如果您想要 UTC 时间戳并且对精度或格式不挑剔,您可以:

cout << system_clock::now() << '\n';

这只是为我输出:

2017-01-16 20:42:11.267245

New answer for old question using C++11 or C++14 and this free, open-source library:

#include "tz.h"
#include <iostream>

int
main()
{
    using namespace date;
    using namespace std;
    using namespace std::chrono;
    auto now = make_zoned(current_zone(), floor<milliseconds>(system_clock::now()));
    cout << format("%e/%m/%Y %T", now) << '\n';
}

This just output for me:

16/01/2017 15:34:32.167

which is my current local date and time to millisecond precision. By eliminating the floor<milliseconds>() you will automatically get whatever precision your system_clock has.

If you wanted the result as a UTC timestamp instead of a local timestamp, it is even easier:

    auto now = floor<milliseconds>(system_clock::now());
    cout << format("%e/%m/%Y %T", now) << '\n';

And if you want a UTC timestamp and you aren't picky about the precision or the format, you can just:

cout << system_clock::now() << '\n';

which just output for me:

2017-01-16 20:42:11.267245
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文