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

247
Views
Comparar la fecha y la hora de dos archivos en C

Estoy escribiendo un programa en un sistema Linux donde tengo que encontrar el archivo modificado más reciente (es decir, el archivo más reciente que ha sido modificado) según la marca de tiempo en mi directorio actual.

En el ejemplo a continuación, tengo que revisar todos los archivos en el directorio y encontrar el que tiene la marca de tiempo más reciente (es decir , 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

Puedo obtener la marca de tiempo de cada archivo en mi directorio actual, pero quiero un método para comparar las dos marcas de tiempo (comparar la fecha y la hora).

 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

La marca de tiempo de la hora de la última modificación es statbuf.st_mtime . El tipo es struct timespec , una estructura con 2 miembros:

  • tv_sec , el número de segundos desde el 1 de enero de 1970 0:00:00 UTC,
  • tv_nsec , un número de nanosegundos.

Puede comparar 2 de estas marcas de tiempo de esta manera:

 // 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!