2012-08-01 28 views
32

我想获取当前的时间戳并使用fprintf将其打印出来。如何获得C中的unix时间戳作为int?

+2

有什么特别的原因它需要特别是一个int? – 2012-08-01 18:29:05

+0

@ DennisMeng如果我不得不猜测,他可能想要得到时间戳作为返回码,并删除输出处理。 – 2017-01-07 11:09:41

+0

对不起,necrobump这个,但*技术上*所有这些答案都是不正确的,因为自从Unix纪元以来''时间'**不保证**;根据维基百科,它可以是供应商选择的任何时代。它也表示1900有时被使用。不过,我认为这是自所有即使远程智能系统上的Unix时代开始。 – Markasoftware 2017-05-25 01:10:37

回答

42

对于32位系统:

fprintf(stdout, "%u\n", (unsigned)time(NULL)); 

对于64位系统:

fprintf(stdout, "%lu\n", (unsigned long)time(NULL)); 
+0

如果我需要它几微秒? – Tim 2012-08-01 21:26:54

+2

如果您想要微秒,您应该使用以下函数之一:带CLOCK_MONOTONIC时钟ID或gettimeofday的clock_gettime。但要小心gettimeofday可以返​​回递减的时间值。 – 2012-08-01 21:34:07

+0

所以你的意思是,gettimeofday可能在时间T返回N,但在时间T + 1返回N-1? – Tim 2012-08-01 23:47:55

10

具有第二精度,您可以打印tv_sec字段timeval结构,您从gettimeofday()函数获得的结构。例如:

#include <sys/time.h> 
#include <stdio.h> 

int main() 
{ 
    struct timeval tv; 
    gettimeofday(&tv, NULL); 
    printf("Seconds since Jan. 1, 1970: %ld\n", tv.tv_sec); 
    return 0; 
} 

编译和运行的实施例:

$ gcc -Wall -o test ./test.c 
$ ./test 
Seconds since Jan. 1, 1970: 1343845834 

但请注意,它是自纪元一段时间,所以long int用于拟合的秒数这些天。

也有打印人类可读时间的功能。详情请参阅this manual page。在这里不用使用ctime()一个例子:

#include <time.h> 
#include <stdio.h> 

int main() 
{ 
    time_t clk = time(NULL); 
    printf("%s", ctime(&clk)); 
    return 0; 
} 

实施例运行&输出:

$ gcc -Wall -o test ./test.c 
$ ./test 
Wed Aug 1 14:43:23 2012 
$ 
+0

'gettimeofday'是实际的Linux系统调用。 – 2017-10-07 02:55:40

20

只是把由time()

#include <stdio.h> 
#include <time.h> 

int main(void) { 
    printf("Timestamp: %d\n",(int)time(NULL)); 
    return 0; 
} 

你想要什么返回值?

$ gcc -Wall -Wextra -pedantic -std=c99 tstamp.c && ./a.out 
Timestamp: 1343846167 

要得到微秒从新纪元,从C11上,便携的方法是使用

int timespec_get(struct timespec *ts, int base) 

不幸的是,C11尚未提供无处不在,所以截至目前,最接近便携是使用POSIX函数clock_gettimegettimeofday(在POSIX.1-2008中标记为过时,其中推荐clock_gettime)中的一个。

两个函数的代码几乎是相同的:

#include <stdio.h> 
#include <time.h> 
#include <stdint.h> 
#include <inttypes.h> 

int main(void) { 

    struct timespec tms; 

    /* The C11 way */ 
    /* if (! timespec_get(&tms, TIME_UTC)) { */ 

    /* POSIX.1-2008 way */ 
    if (clock_gettime(CLOCK_REALTIME,&tms)) { 
     return -1; 
    } 
    /* seconds, multiplied with 1 million */ 
    int64_t micros = tms.tv_sec * 1000000; 
    /* Add full microseconds */ 
    micros += tms.tv_nsec/1000; 
    /* round up if necessary */ 
    if (tms.tv_nsec % 1000 >= 500) { 
     ++micros; 
    } 
    printf("Microseconds: %"PRId64"\n",micros); 
    return 0; 
} 
+1

如果我需要微秒,该怎么办? – Tim 2012-08-01 21:26:44

+0

这就意味着'int'在几乎所有常见系统上都不是合适的类型。使用典型的'INT_MAX = 2147483647',如果你想要微秒,你可以将不足36分钟的时间放入'int'中。当然,你通常不能使用'time()'。可移植的是,你必须使用'timespec_get()'。你想要什么? Unix时间戳或微秒(自epoch以来)? – 2012-08-01 21:39:10

+0

微秒以来时代将是最好的。 – Tim 2012-08-01 23:47:30

0
#include <stdio.h> 
#include <time.h> 

int main() 
{ 
    time_t seconds; 

    seconds = time(NULL); 
    printf("Seconds since January 1, 1970 = %ld\n", seconds); 

    return(0); 
} 

而且会得到类似的结果:
秒自1970年1月1日= 1476107865

相关问题