自纪元以来的打印时间(以纳秒为单位)
Printing Time since Epoch in Nanoseconds
所以我知道如何以秒为单位打印自纪元以来的时间,显然甚至以毫秒为单位,但是当我尝试以纳秒为单位时,我总是得到一个太小的整数的虚假输出,它有时打印的数字小于最后一个 运行.
#include <stdio.h>
#include <time.h>
int main (void)
{
long int ns;
struct timespec spec;
clock_gettime(CLOCK_REALTIME, &spec);
ns = spec.tv_nsec;;
printf("Current time: %ld nonoseconds since the Epoch\n", ns);
return 0;
}
例如,使用 运行 从纪元开始我得到了 35071471 纳秒。
如能帮助我们正确显示,我们将不胜感激。
纳秒部分就是"fractional"部分,你还要加上秒。
// otherwise gcc with option -std=c11 complaints
#define _POSIX_C_SOURCE 199309L
#include <stdio.h>
#include <time.h>
#include <stdint.h>
#include <inttypes.h>
#define BILLION 1000000000L
int main(void)
{
long int ns;
uint64_t all;
time_t sec;
struct timespec spec;
clock_gettime(CLOCK_REALTIME, &spec);
sec = spec.tv_sec;
ns = spec.tv_nsec;
all = (uint64_t) sec * BILLION + (uint64_t) ns;
printf("Current time: %" PRIu64 " nanoseconds since the Epoch\n", all);
return 0;
}
所以我知道如何以秒为单位打印自纪元以来的时间,显然甚至以毫秒为单位,但是当我尝试以纳秒为单位时,我总是得到一个太小的整数的虚假输出,它有时打印的数字小于最后一个 运行.
#include <stdio.h>
#include <time.h>
int main (void)
{
long int ns;
struct timespec spec;
clock_gettime(CLOCK_REALTIME, &spec);
ns = spec.tv_nsec;;
printf("Current time: %ld nonoseconds since the Epoch\n", ns);
return 0;
}
例如,使用 运行 从纪元开始我得到了 35071471 纳秒。
如能帮助我们正确显示,我们将不胜感激。
纳秒部分就是"fractional"部分,你还要加上秒。
// otherwise gcc with option -std=c11 complaints
#define _POSIX_C_SOURCE 199309L
#include <stdio.h>
#include <time.h>
#include <stdint.h>
#include <inttypes.h>
#define BILLION 1000000000L
int main(void)
{
long int ns;
uint64_t all;
time_t sec;
struct timespec spec;
clock_gettime(CLOCK_REALTIME, &spec);
sec = spec.tv_sec;
ns = spec.tv_nsec;
all = (uint64_t) sec * BILLION + (uint64_t) ns;
printf("Current time: %" PRIu64 " nanoseconds since the Epoch\n", all);
return 0;
}