[LINUX] How to display the modification date of a file in C language up to nanoseconds

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.

About the environment

-Linux kernel 2.5.48 or higher -File system XFS, JFS, Btrfs, ext4 File systems other than the above may display nanoseconds as 0.

Source

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

Operation check

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

References

STAT CLOCK_GETRES

Recommended Posts

How to display the modification date of a file in C language up to nanoseconds
How to limit the API to be published in the C language shared library of Linux
How to display the regional mesh of the official statistics window (eStat) in a web browser
How to display a specified column of files in Linux (awk)
How to determine the existence of a selenium element in Python
How to check the memory size of a variable in Python
How to check the memory size of a dictionary in Python
How to output the output result of the Linux man command to a file
How to get the vertex coordinates of a feature in ArcPy
How to specify a .ui file in the dialog / widget GUI in PySide
How to put a line number at the beginning of a CSV file
I made a program to check the size of a file in Python
I tried to display the altitude value of DTM in a graph
How to calculate the volatility of a brand
How to use the C library in Python
[Python] How to change the date format (display format)
How to create a JSON file in Python
How to multi-process exclusive control in C language
Set up a UDP server in C language
How to read a file in a different directory
A story about trying to improve the testing process of a system written in C language for 20 years
Various ways to read the last line of a csv file in Python
How to pass the execution result of a shell command in a list in Python
How to mention a user group in slack notification, how to check the id of the user group
How to count the number of elements in Django and output to a template
A memorandum of how to execute the! Sudo magic command in Jupyter Notebook
How to get a list of files in the same directory with python
How to save the feature point information of an image in a file and use it for matching
Try to make a Python module in C language
How to get the number of digits in Python
How to display multiple images of galaxies in tiles
How to display DataFrame as a table in Markdown
How to identify the element with the smallest number of characters in a Python list?
How to check in Python if one of the elements of a list is in another list
[Ubuntu] How to delete the entire contents of a directory
How to find the optimal number of clusters in k-means
How to use the __call__ method in a Python class
Change the standard output destination to a file in Python
How to import a file anywhere you like in Python
How to temporarily implement a progress bar in a scripting language
I tried to illustrate the time and time in C language
How to develop in a virtual environment of Python [Memo]
How to generate a query using the IN operator in Django
[Note] Import of a file in the parent directory in Python
How to display a list of installable versions with pyenv
How to get the last (last) value in a list in Python
How to get a list of built-in exceptions in python
How to see the contents of the Jupyter notebook ipynb file
How to find the scaling factor of a biorthogonal wavelet
How to get a quadratic array of squares in a spiral!
How to connect the contents of a list into a string
[Linux] [C / C ++] How to get the return address value of a function and the function name of the caller
Get the value of a specific key up to the specified index in the dictionary list in Python
[Super easy! ] How to display the contents of dictionaries and lists including Japanese in Python
How to quickly count the frequency of appearance of characters from a character string in Python?
How to plot the distribution of bacterial composition from Qiime2 analysis data in a box plot
How to display a specified line of a file or command result on Linux (sed, awk)
How to pass the execution result of a shell command in a list in Python (non-blocking version)
How to make an interactive LINE BOT 004 (answer the closing date of a listed company)
[Python] About creating a tool to display all the pages of the website registered in the JSON file & where it got caught
Output the key list included in S3 Bucket to a file