如何在 c 中以毫秒为单位获取时间-ag捕鱼王app官网

如何在 c 中以毫秒为单位获取时间

作者:迹忆客 最近更新:2024/01/02 浏览次数:

本文将介绍多种 c 方法,介绍如何以毫秒为单位获取时间。


使用 std::chrono::system_clock::now() 方法在 c 中获取以毫秒为单位的时间

std::chrono::system_clock 类是 c 中获取全系统实时挂钟的接口。大多数系统使用 unix 时间,它表示为从 1970 年 1 月 1 日 00:00:00 utc 开始的秒数,称为 unix 纪元。请注意,闰秒被忽略了。因此 unix 时间并不是 utc 的真正准确表示。

首先,调用 now() 方法来返回当前的时间点。接下来调用的方法是 time_since_epoch 来检索*this 和时钟的纪元之间的时间量,但它返回的是一个 std::chrono::duration 类对象。这个对象应该调用 count 方法来返回实际的 ticks 数,并以毫秒来表示。结果使用 duration_cast 进行投射。

#include #include #include #include using std::cout;
using std::endl;
using std::chrono::duration_cast;
using std::chrono::milliseconds;
using std::chrono::seconds;
using std::chrono::system_clock;
int main() {
  auto millisec_since_epoch =
      duration_cast<milliseconds>(system_clock::now().time_since_epoch())
          .count();
  auto sec_since_epoch =
      duration_cast<seconds>(system_clock::now().time_since_epoch()).count();
  cout << "seconds since epoch: " << sec_since_epoch << endl;
  cout << "milliseconds since epoch: " << millisec_since_epoch << endl;
  return exit_success;
}

输出:

seconds since epoch: 1603612778
milliseconds since epoch: 1603612778389

使用 gettimeofday() 函数在 c 中获得以毫秒为单位的时间

gettimeofday 是符合 posix 标准的函数,用于检索系统时钟。它以 struct timeval 对象的地址作为第一个参数来存储时间值。值是 tv_sec 代表秒数,tv_usec 代表 unix 纪元以来的微秒数。gettimeofday 返回 int0 表示成功,-1 表示失败,提供错误处理功能。函数的第二个参数是 struct timezone,但是由于它已经被贬值了,你应该只传递一个 nullptr。注意,你需要在函数定义中加入 头文件。

#include #include #include #include using std::cout;
using std::endl;
int main() {
  struct timeval time_now {};
  gettimeofday(&time_now, nullptr);
  time_t msecs_time = (time_now.tv_sec * 1000)  (time_now.tv_usec / 1000);
  cout << "seconds since epoch: " << time_now.tv_sec << endl;
  cout << "milliseconds since epoch: " << msecs_time << endl << endl;
  return exit_success;
}

输出:

seconds since epoch: 1603612778
milliseconds since epoch: 1603612778389

c 中使用 time() 函数获取时间(毫秒)

在 c 中,另一种符合 posix 标准的检索系统时间的方法是调用 time 函数。time 接受一个类型为 time_t*的可选参数,返回的时间值就存储在这个参数中。另外,也可以使用函数返回值存储在单独声明的变量中。在后一种情况下,可以传递 nullptr 作为参数。注意,这个调用并没有像前面的调用那样以同样的精度返回时间。

#include #include #include #include using std::cout;
using std::endl;
int main() {
  time_t now = time(nullptr);
  time_t mnow = now * 1000;
  cout << "seconds since epoch: " << now << endl;
  cout << "milliseconds since epoch: " << mnow << endl << endl;
  return exit_success;
}

输出:

seconds since epoch: 1603612778
milliseconds since epoch: 1603612778000

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

arduino 中停止循环

发布时间:2024/03/13 浏览次数:444 分类:c

可以使用 exit(0),无限循环和 sleep_n0m1 库在 arduino 中停止循环。

arduino 复位

发布时间:2024/03/13 浏览次数:315 分类:c

可以通过使用复位按钮,softwarereset 库和 adafruit sleepydog 库来复位 arduino。

发布时间:2024/03/13 浏览次数:181 分类:c

可以使用简单的方法 toint()函数和 serial.parseint()函数将 char 转换为 int。

arduino 串口打印多个变量

发布时间:2024/03/13 浏览次数:381 分类:c

可以使用 serial.print()和 serial.println()函数在串口监视器上显示变量值。

arduino if 语句

发布时间:2024/03/13 浏览次数:123 分类:c

可以使用 if 语句检查 arduino 中的不同条件。

arduino icsp

发布时间:2024/03/13 浏览次数:214 分类:c

icsp 引脚用于两个 arduino 之间的通信以及对 arduino 引导加载程序进行编程。

发布时间:2024/03/13 浏览次数:151 分类:c

可以使用 arduino 中的循环制作计数器。

使用 c 编程 arduino

发布时间:2024/03/13 浏览次数:127 分类:c

本教程将讨论使用 arduino ide 在 c 中对 arduino 进行编程。

arduino 中的子程序

发布时间:2024/03/13 浏览次数:168 分类:c

可以通过在 arduino 中声明函数来处理子程序。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便
网站地图