It is a method to display the modification date and time of the file in C language up to nanoseconds. The purpose is to check for regular updates of files.
-Linux kernel 2.5.48 or higher -File system XFS, JFS, Btrfs, ext4 File systems other than the above may display nanoseconds as 0.
nsec_show.c
#include <stdio.h>
#include <sys/stat.h>
#include <errno.h>
#include <time.h>
int main(int argc, char *argv[])
{
int i;
struct stat st;
char date_str[256];
/* argc check */
if (argc != 2) {
printf("USAGE:%s <FILE PATH>\n", argv[0]);
return 1;
}
/* get stat */
if (stat(argv[1], &st) != 0) {
printf("%s\n", strerror(errno));
return 1;
}
/* show */
strftime(date_str, 255, "%Y%m%d%H%I%M%S", localtime(&st.st_mtime));
printf("%s.%09ld\n",date_str, st.st_mtim.tv_nsec);
return 0;
}
After compiling, when you execute it, it will be displayed as follows.
$ gcc nsec_show.c -o nsec_show
$ ./nsec_show ./nsec_show.c
20170305063637.656058532
By the way, it matches the nanoseconds displayed by the ls command.
$ ls ./nsec_show.c --full-time
-rw-rw-r--. 1 miyabi miyabi 581 2017-03-05 06:36:37.656058532 +0900 ./nsec_show.c
Recommended Posts