美文网首页
Linux c语言打印当前时间精确到毫秒

Linux c语言打印当前时间精确到毫秒

作者: CodingCode | 来源:发表于2023-06-23 05:06 被阅读0次
#include <stdio.h>
#include <time.h>
#include <sys/time.h>

void printtimestamp() {
  struct timeval tv;
  struct tm t;

  gettimeofday(&tv, NULL);
  int milli = tv.tv_usec / 1000;

  char buffer [80] = { '\0' };
  localtime_r(&tv.tv_sec, &t);
  strftime(buffer, 80, "%Y-%m-%d %H:%M:%S", &t);

  printf("%s:%03d\n", buffer, milli);
}

int main() {
  printtimestamp();
}

相关文章

网友评论

      本文标题:Linux c语言打印当前时间精确到毫秒

      本文链接:https://www.haomeiwen.com/subject/jwwmydtx.html