简体   繁体   English

以毫秒为单位获取当前时间,或 HH:MM:SS:MMM 格式

[英]Get current time in milliseconds, or HH:MM:SS:MMM format

I've written a c++ function to get the current time in HH:MM:SS format.我编写了一个 C++ 函数来以HH:MM:SS格式获取当前时间。 How can I add milliseconds or nanoseconds, so I can have a format like HH:MM:SS:MMM ?如何添加毫秒或纳秒,以便我可以使用类似HH:MM:SS:MMM的格式? If not possible, a function that returns current time in ms would also be good.如果不可能,以毫秒为单位返回当前时间的函数也很好。 I can then calculate the relative time distances between two log points myself.然后我可以自己计算两个日志点之间的相对时间距离。

string get_time()
{
    time_t t = time(0);   // get time now
    struct tm * now = localtime(&t);
    std::stringstream sstm;
    sstm << (now->tm_hour) << ':' << (now->tm_min) << ':' << now->tm_sec;
    string s = sstm.str();
    return s;
}

This is a portable method using the C++11 chrono library:这是使用C++11 chrono 库的可移植方法:

#include <chrono>
#include <ctime>
#include <iomanip>
#include <sstream>
#include <string>

// ...

std::string time_in_HH_MM_SS_MMM()
{
    using namespace std::chrono;

    // get current time
    auto now = system_clock::now();

    // get number of milliseconds for the current second
    // (remainder after division into seconds)
    auto ms = duration_cast<milliseconds>(now.time_since_epoch()) % 1000;

    // convert to std::time_t in order to convert to std::tm (broken time)
    auto timer = system_clock::to_time_t(now);

    // convert to broken time
    std::tm bt = *std::localtime(&timer);

    std::ostringstream oss;

    oss << std::put_time(&bt, "%H:%M:%S"); // HH:MM:SS
    oss << '.' << std::setfill('0') << std::setw(3) << ms.count();

    return oss.str();
}

This is a cleaner solution using HowardHinnant's date library.这是使用 HowardHinnant日期库的更简洁的解决方案。

std::string get_time()
{
    using namespace std::chrono;
    auto now = time_point_cast<milliseconds>(system_clock::now());
    return date::format("%T", now);
}

For windows maybe:对于窗户,也许:

#include <iostream>
#include <Windows.h>
#include <strsafe.h>

int main()
{
    CHAR sysTimeStr[13] = {};
    SYSTEMTIME systemTime;
    GetLocalTime(&systemTime);
    sprintf_s(sysTimeStr,
        "%u:%02u:%02u:%03u",
        systemTime.wHour,
        systemTime.wMinute,
        systemTime.wSecond,
        systemTime.wMilliseconds);

    std::cout << sysTimeStr;
}

Instead of using time() (seconds since the epoch), try gettimeofday() .不要使用time() (自纪元以来的秒数),而是尝试gettimeofday() Gives you a structure that includes a microseconds field.为您提供一个包含微秒字段的结构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有没有更简单的方法以 hh:mm:ss 格式获取当前时间? - Is there an easier way to get the current time in hh:mm:ss format? 时间格式hh:mm:ss输入 - time format hh:mm:ss input 在C ++中将日期格式Www Mmm dd hh:mm:ss yyyy转换为dd hh:mm:ss字符串 - convert date format Www Mmm dd hh:mm:ss yyyy to dd hh:mm:ss string in c++ 如何使用将 stable_clock 时间格式化为 HH:MM:SS.Milliseconds<chrono> 来自 c++ stdl?</chrono> - How to format steady_clock time into HH:MM:SS.Milliseconds using <chrono>from c++ stdl? 一行 function 将 hh:mm:ss.zzz 时间 QString 转换为毫秒? - A one line function to convert a hh:mm:ss.zzz time QString to milliseconds? 将当前时间作为YYYY-MM-DD-HH-MM-SS字符串 - Getting the current time as a YYYY-MM-DD-HH-MM-SS string 使用 std::chrono::from_stream() 解析时间格式“DD/MM/YYYY at hh:mm:ss”和其他格式 - Parse time format "DD/MM/YYYY at hh:mm:ss" & others using std::chrono::from_stream() QCustomPlot 以 HH:MM:SS 显示时间 - QCustomPlot display time in HH:MM:SS 两个时间之间以24小时格式表示的经过时间hh:mm:ss - Elapsed time between two time in 24hr format hh:mm:ss 如何比较“月日期hh:mm:ss”格式的两个时间戳来检查+ ve或-ve值 - How to compare two time stamp in format “Month Date hh:mm:ss” to check +ve or -ve value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM