11

I want to write a C program to display the file last modification time in microsecond or millisecond. How could I do? Could you give me a help?

Thanks very much.

2
  • 1
    Just out of curiosity, Why do you need this?
    – Alphaneo
    Feb 23, 2011 at 2:54
  • @Alphaneo: In fact, I want to display all the files` last modification time in some directory. According to this, I can sort my files. thx
    – JavaMobile
    Feb 23, 2011 at 8:44

5 Answers 5

12

The stat() function is used. In sufficiently recent versions of glibc, st_mtim (note: no trailing e) is a field of type struct timespec that holds the file modification time:

struct stat st;

if (stat(filename, &st)) {
    perror(filename);
} else {
    printf("%s: mtime = %lld.%.9ld\n", filename, (long long)st.st_mtim.tv_sec, st.st_mtim.tv_nsec);
}

You should check for the presence of st_mtim in struct stat in your build system, and be ready to fall back to st_mtime (which has type time_t, and only 1 second resolution) if it is not present.

2
  • Thanks so much. It is wonderful. But the output is disorder, is it possible to sort them according to the file names?
    – JavaMobile
    Feb 24, 2011 at 4:46
  • @JavaMobile: Sure - put pointers to the filenames into an array and sort them, then run through that array and call stat().
    – caf
    Feb 24, 2011 at 12:16
4

You may use stat() function, it will return struct stat which contains time of last modification of a file. Here is the man page http://linux.die.net/man/2/stat. As to precision, it depends on whether your file system supports sub-second timestamps or not.

5
  • Is it possible to get time in Milli or Microsecond range using stat ... I am afraid not ;)
    – Alphaneo
    Feb 23, 2011 at 2:52
  • 2
    @Alphaneo: I looked at that Web page and it says that stat returns times with nanosecond precision. Feb 23, 2011 at 2:54
  • Yes, stat() on Linux has extra fields to add nanosecond resolution, since kernel 2.5.48. However, as @ZelluX points out, accuracy != precision -- it really depends on what the filesystem supports.
    – payne
    Feb 23, 2011 at 3:05
  • @payne: I also use stat() function, but it seems to only display one file. I want to display the last modification time for all the files in some directory. How to do it?
    – JavaMobile
    Feb 23, 2011 at 8:47
  • @JavaMobile You can use getdents() or readdir() to get all the files in some directory.
    – ZelluX
    Feb 23, 2011 at 10:04
1

JFS, XFS, ext4, and Btrfs support nanosecond timestamps.

The book "The Linux Programming Interface" by Michael Kerrisk has a good section on File attributes

1

There is a stat command, which you can use directly

http://www.thegeekstuff.com/2009/07/unix-stat-command-how-to-identify-file-attributes/

1
  • This answer is for a shell, but the OP asked for a C program. Nevertheless, I found this question while looking for shell commands and stat(1) was just what I needed! :)
    – cxw
    Apr 6, 2016 at 15:27
0

To complete answers by Andrew and ZelluX.
The limitation is in file system. For Linux ext3 is commonly used, and you can see in wikipedia:

Date resolution   1s

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.