linux和Windows平台下获取时间戳方式

简介: linux和Windows平台下获取时间戳方式

文章目录

获取时间间隔方式:

API说明

Windows平台下使用 GetLocalTime

linux平台下使用 gettimeofday

代码示例


获取时间间隔方式:

API说明

Windows平台下使用 GetLocalTime

VOID GetLocalTime(
  LPSYSTEMTIME lpSystemTime //address of system times structure
);


参数说明:


  • lpSystemTime: 指向一个用户自定义包含日期和时间信息的类型为 SYSTEMTIME 的变量,该变量用来保存函数获取的时间信息。

此函数会把获取的系统时间信息存储到SYSTEMTIME结构体里边

typedef struct _SYSTEMTIME
{
  WORD wYear;//年
  WORD wMonth;//月
  WORD wDayOfWeek;//星期,0为星期日,1为星期一,2为星期二……
  WORD wDay;//日
  WORD wHour;//时
  WORD wMinute;//分
  WORD wSecond;//秒
  WORD wMilliseconds;//毫秒
}SYSTEMTIME,*PSYSTEMTIME;


  • 使用示例:
SYSTEMTIME stTime;
GetLocalTime(&stTime);
WORD wYear = stTime.wYear;
WORD wMonth = stTime.wMonth;
WORD wDay = stTime.wDay;
WORD wHour = stTime.wHour;
WORD wMinute = stTime.wMinute;
WORD wSecond = stTime.wSecond;
CString m_date;
//m_date字符串即为当前时间。如:2010年4月23日 11:12:45
m_date.Format("%4d年%2d月%2d日 %2d:%2d:%2d", wYear, wMonth, wDay, wHour, wMinute, wSecond);


  • 执行结果

20200110112559291.png

linux平台下使用 gettimeofday

#include <sys/time.h>
int gettimeofday(struct timeval*tv, struct timezone *tz);


其参数tv是保存获取时间结果的结构体,参数tz用于保存时区结果:


struct timezone{
  int tz_minuteswest;/*格林威治时间往西方的时差*/
  int tz_dsttime;/*DST 时间的修正方式*/
}


timezone 参数若不使用则传入NULL即可。

而结构体timeval的定义为:


struct timeval{
  long int tv_sec; // 秒数
  long int tv_usec; // 微秒数
}


它获得的时间精确到微秒(1e-6 s)量级。在一段代码前后分别使用gettimeofday可以计算代码执行时间:


struct timeval tv_begin, tv_end;
gettimeofday(&tv_begin, NULL);
foo();
gettimeofday(&tv_end, NULL);


函数执行成功后返回0,失败后返回-1,错误代码存于errno中。


代码示例

#include <winsock2.h>
#include <time.h>
using namespace cv;
using namespace std;
unsigned long long GetCurrentTimeMsec()
{
#ifdef _WIN32
  struct timeval tv;
  time_t clock;
  struct tm tm;
  SYSTEMTIME wtm;
  GetLocalTime(&wtm);
  tm.tm_year = wtm.wYear - 1900;
  tm.tm_mon = wtm.wMonth - 1;
  tm.tm_mday = wtm.wDay;
  tm.tm_hour = wtm.wHour;
  tm.tm_min = wtm.wMinute;
  tm.tm_sec = wtm.wSecond;
  tm.tm_isdst = -1;
  clock = mktime(&tm);
  tv.tv_sec = clock;
  tv.tv_usec = wtm.wMilliseconds * 1000;
  return ((unsigned long long)tv.tv_sec * 1000 + (unsigned long long)tv.tv_usec / 1000);
#else
  struct timeval tv;
  gettimeofday(&tv, NULL);
  return ((unsigned long long)tv.tv_sec * 1000 + (unsigned long long)tv.tv_usec / 1000);
#endif
}



相关文章
|
29天前
|
安全 Linux 生物认证
Nexpose 8.25.0 for Linux & Windows - 漏洞扫描
Nexpose 8.25.0 for Linux & Windows - 漏洞扫描
78 0
Nexpose 8.25.0 for Linux & Windows - 漏洞扫描
|
1月前
|
监控 编译器 Windows
Qt5实现Windows平台串口通信
Qt5实现Windows平台串口通信
|
1月前
|
安全 Linux iOS开发
Binary Ninja 5.1.8104 (macOS, Linux, Windows) - 反编译器、反汇编器、调试器和二进制分析平台
Binary Ninja 5.1.8104 (macOS, Linux, Windows) - 反编译器、反汇编器、调试器和二进制分析平台
312 53
Binary Ninja 5.1.8104 (macOS, Linux, Windows) - 反编译器、反汇编器、调试器和二进制分析平台
|
1月前
|
安全 Linux 生物认证
Nexpose 8.24.0 for Linux & Windows - 漏洞扫描
Nexpose 8.24.0 for Linux & Windows - 漏洞扫描
143 1
Nexpose 8.24.0 for Linux & Windows - 漏洞扫描
|
1月前
|
SQL 安全 Linux
Metasploit Pro 4.22.8-20251014 (Linux, Windows) - 专业渗透测试框架
Metasploit Pro 4.22.8-20251014 (Linux, Windows) - 专业渗透测试框架
107 1
Metasploit Pro 4.22.8-20251014 (Linux, Windows) - 专业渗透测试框架
|
1月前
|
Linux 网络安全 iOS开发
Metasploit Framework 6.4.95 (macOS, Linux, Windows) - 开源渗透测试框架
Metasploit Framework 6.4.95 (macOS, Linux, Windows) - 开源渗透测试框架
173 1
Metasploit Framework 6.4.95 (macOS, Linux, Windows) - 开源渗透测试框架
|
1月前
|
Linux API iOS开发
Binary Ninja 4.2.6455 (macOS, Linux, Windows) - 反编译器、反汇编器、调试器和二进制分析平台
Binary Ninja 4.2.6455 (macOS, Linux, Windows) - 反编译器、反汇编器、调试器和二进制分析平台
178 14
Binary Ninja 4.2.6455 (macOS, Linux, Windows) - 反编译器、反汇编器、调试器和二进制分析平台
|
30天前
|
Linux 虚拟化 iOS开发
VMware Remote Console 13.0.1 for macOS, Linux, Windows - vSphere 虚拟机控制台的桌面客户端
VMware Remote Console 13.0.1 for macOS, Linux, Windows - vSphere 虚拟机控制台的桌面客户端
331 0
VMware Remote Console 13.0.1 for macOS, Linux, Windows - vSphere 虚拟机控制台的桌面客户端
|
1月前
|
Linux iOS开发 计算机视觉
GIMP 3.0.6 (Linux, macOS, Windows) 发布 - 免费开源图像编辑器
GIMP 3.0.6 (Linux, macOS, Windows) 发布 - 免费开源图像编辑器
176 0
Linux CentOS 平台安装 rar unrar 命令
Linux CentOS 平台安装 rar unrar 命令
1777 0