I would like to get the current timestamp and print it out using fprintf
.
我想要得到当前的时间戳并使用fprintf打印出来。
4 个解决方案
#1
42
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));
#2
20
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_gettime或gettimeofday(在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;
}
#3
10
With second precision, you can print tv_sec
field of timeval
structure that you get from gettimeofday()
function. For example:
通过第二个精度,您可以打印从gettimeofday()函数获得的tv_sec时间结构字段。例如:
#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.
然而,请注意,这是一段时间以来的新纪元,如此长的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
$
#4
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);
}
And will get similar result:
Seconds since January 1, 1970 = 1476107865
并将得到类似的结果:自1970年1月1日以来的秒= 1476107865
#1
42
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));
#2
20
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_gettime或gettimeofday(在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;
}
#3
10
With second precision, you can print tv_sec
field of timeval
structure that you get from gettimeofday()
function. For example:
通过第二个精度,您可以打印从gettimeofday()函数获得的tv_sec时间结构字段。例如:
#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.
然而,请注意,这是一段时间以来的新纪元,如此长的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
$
#4
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);
}
And will get similar result:
Seconds since January 1, 1970 = 1476107865
并将得到类似的结果:自1970年1月1日以来的秒= 1476107865