How to print current time in shell script:
$date
For example, if you want to print current seconds since Epoch, and current nanoseconds. %s means print seconds, and %N means print nanoseconds
$date +%s%N
If you want to see the details, please see man page of date
How to print current time in C:
You could use time() function, please see man(3) page
time_t could print out by using %ld
e.g.
time_t result;
result = time(NULL); // or time(&result);
printf("%ld", result); // or printf("%s\n", ctime(&result))
If you want to print out the sec and nsec of current time.
e.g.
#include <time.h>
void printCurrentTimeNsec()
{
struct timespec tp;
clock_gettime(CLOCK_REALTIME, &tp);
printf("%ld %ld\n", tp.tv_sec, tp.tv_nsec);
}
When you compile this program, you need to add -lrt because of clock_gettime() function.
No comments:
Post a Comment