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

2023-06-23  本文已影响0人  CodingCode
#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();
}
上一篇 下一篇

猜你喜欢

热点阅读