Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

238
Views
Compare the date and time of two files in C

I am writing a program in a linux system where I have to find the newest modified file (ie: the most recent file which has been modified) based on the timestamp in my current directory.

In the example below, I have to look through all the files in the directory and find the one with the latest timestamp (i.e. File.txt).

/root/MyProgram       <- Current Directory
 -Test1.txt    25/10/2019  14:30:26
 -TEST2.bin    15/01/2020  18:12:36
 -Test3.bin    06/05/2021  08:45:10
 -File.txt     06/12/2021  03:10:55

I am able to get the timestamp of each file in my current directory but I want a method to compare the two timestamps (compare both date and time).

void show_dir_content(char *path) {
    struct dirent *dir;
    struct stat statbuf;
    char datestring[256];
    struct tm *tm;
    DIR *d = opendir(path);
    if (d == NULL) {
        return;  
    }
    //
    while ((dir = readdir(d)) != NULL) {
        if (dir->d_type == DT_REG) {
            char f_path[500];
            char filename[256];
            sprintf(filename, "%s", dir->d_name);
            sprintf(f_path, "%s/%s", path, dir->d_name);
            printf("filename: %s", filename);
            printf("  filepath: %s\n", f_path);

            if (stat(f_path, &statbuf) == -1) {
                fprintf(stderr,"Error: %s\n", strerror(errno));
                continue;
            }
            tm = gmtime(&statbuf.st_mtime);
            time_t t1 = statbuf.st_mtime;
            strftime(datestring, sizeof(datestring), " %x-%X", tm);
            printf("datestring: %s\n", datestring);
        }

        if (dir->d_type == DT_DIR && strcmp(dir->d_name, ".") != 0 && strcmp(dir->d_name, "..") != 0) {
            printf("directory: %s ", dir->d_name);
            char d_path[500]; 
            sprintf(d_path, "%s/%s", path, dir->d_name);
            printf("  dirpath: %s\n", d_path);
            show_dir_content(d_path);
        }
    }
    closedir(d);
}
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

The timestamp for the last modification time is statbuf.st_mtime. The type is struct timespec, a structure with 2 members:

  • tv_sec, the number of seconds since Jan 1st, 1970 0:00:00 UTC,
  • tv_nsec, a number of nanoseconds.

You can compare 2 such timestamps this way:

// compare 2 timespecs:
// return -1 if t1 < t2, 0 if they are equal, 1 if t1 > t2

int compare_timespec(const struct timespec *t1, const struct timespec *t2) {
    if (t1->tv_sec == t2->tv_sec)
        return (t1->tv_nsec > t2->tv_nsec) - (t1->tv_nsec < t2->tv_nsec);
    else
        return (t1->tv_sec > t2->tv_sec) ? 1 : -1;
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!