C++ / 获得系统实时时间ms

系统时间的获取_ms

1.windows

1
2
3
4
5
6
7
#include <windows.h>
string getTime() {
SYSTEMTIME st;
GetLocalTime(&st);
string tmp = to_string(st.wYear) + "-" + to_string(st.wMonth) + "-" + to_string(st.wDay) + "-" + to_string(st.wHour) + "-" + to_string(st.wMinute) + "-" + to_string(st.wSecond) + "-" + to_string(st.wMilliseconds);
return tmp;
}

2.linux

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <time.h>
#include <sys/time.h>
string get_time()
{
string time;
timeval tv;
gettimeofday(&tv, NULL);

struct tm* ptm;
ptm = localtime(&(tv.tv_sec));

char time_string[40];
long milliseconds;

strftime(time_string, sizeof(time_string), "%Y-%m-%d-%H-%M-%S-", ptm);
milliseconds = tv.tv_usec / 1000;

time = time_string + to_string(milliseconds);

return time;
}
文章作者: Inter
文章链接: https://zuizichuan.cn/2020/07/11/time/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Zichuan365' Blog