【发布时间】:2016-11-17 08:01:31
【问题描述】:
我需要将当前时间(以毫秒为单位)转换为人类可读的时间格式。我有以下代码
#include <netinet/in.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <sys/time.h>
#include <time.h>
int Cnvrt_To_Time_Frmt(char *Epochval)
{
unsigned long epoch = 0;
time_t tt = 0;
char timestamp[64],usec_buf[20];
if (!sscanf(Epochval, "%lu", &epoch))
{
return 1;
}
tt = epoch;
strftime(timestamp, 64, "%c", localtime(&tt));
printf("%s\n",timestamp);
return 0;
}
int main()
{
uint64_t Epoch_time=1468496250207;
char str_ms[256];
sprintf(str_ms, "%llu", (Epoch_time/1000));
Cnvrt_To_Time_Frmt(str_ms);
}
它产生结果:Thu Jul 14 17:07:30 2016.
但我需要用毫秒打印结果。比如 2016 年 7 月 14 日星期四 17:07:30:40。(17 小时 07 分 30 秒 40 毫秒)
这怎么可能?
【问题讨论】:
-
C 还是 C++?不一样...
-
在 C 代码中。我用 gcc 命令编译
标签: c timestamp epoch milliseconds