简体   繁体   English

如何将 C 中的 unix 时间戳作为 int 获取?

[英]How do I get the unix timestamp in C as an int?

我想获取当前时间戳并使用fprintf其打印出来。

For 32-bit systems:对于 32 位系统:

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

For 64-bit systems:对于 64 位系统:

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

Is just casting the value returned by time()只是转换time()返回的值

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

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

what you want?你想要什么?

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

To get microseconds since the epoch, from C11 on, the portable way is to use要获得自纪元以来的微秒,从 C11 开始,可移植的方法是使用

int timespec_get(struct timespec *ts, int base)

Unfortunately, C11 is not yet available everywhere, so as of now, the closest to portable is using one of the POSIX functions clock_gettime or gettimeofday (marked obsolete in POSIX.1-2008, which recommends clock_gettime ).不幸的是,C11 尚未随处可用,因此到目前为止,最接近便携式的是使用 POSIX 函数之一clock_gettimegettimeofday (在 POSIX.1-2008 中标记为过时,推荐使用clock_gettime )。

The code for both functions is nearly identical:这两个函数的代码几乎相同:

#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;
}

With second precision, you can print tv_sec field of timeval structure that you get from gettimeofday() function.使用第二精度,您可以打印从gettimeofday()函数获得的timeval结构的tv_sec字段。 For example:例如:

#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;
}

Example of compiling and running:编译运行示例:

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

Note, however, that its been a while since epoch and so long int is used to fit a number of seconds these days.但是请注意,自 epoch 以来已经有一段时间了,现在使用如此long int来适应几秒钟。

There are also functions to print human-readable times.还有一些功能可以打印人类可读的时间。 See this manual page for details.有关详细信息,请参阅此手册页 Here goes an example using ctime() :这是一个使用ctime()的例子:

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

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

Example run & output:示例运行和输出:

$ gcc -Wall -o test ./test.c 
$ ./test 
Wed Aug  1 14:43:23 2012
$ 
#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);
}

And will get similar result:并会得到类似的结果:
Seconds since January 1, 1970 = 1476107865自 1970 年 1 月 1 日以来的秒数 = 1476107865

An important point is to consider if you perform tasks based on difference between 2 timestamps because you will get odd behavior if you generate it with gettimeofday() , and even clock_gettime(CLOCK_REALTIME,..) at the moment where you will set the time of your system.重要的一点是要考虑是否根据 2 个时间戳之间的差异执行任务,因为如果在设置时间时使用gettimeofday()甚至clock_gettime(CLOCK_REALTIME,..)生成它,则会出现奇怪的行为你的系统。

To prevent such problem, use clock_gettime(CLOCK_MONOTONIC_RAW, &tms) instead.为防止出现此类问题,请改用clock_gettime(CLOCK_MONOTONIC_RAW, &tms)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM