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

132
Views
Compara dos tiempos en C

¿Cómo comparo el tiempo en C? Mi programa obtiene la hora de última modificación de 2 archivos, luego compara esa hora para ver cuál es la última. ¿Hay alguna función que compare el tiempo para ti, o tienes que crear una tú mismo? Esta es mi función de obtención de tiempo:

 #include <stdio.h> #include <stdlib.h> #include <time.h> #include <sys/stat.h> #include <sys/types.h> void getFileCreationTime(char *path) { struct stat attr; stat(path, &attr); printf("Last modified time: %s", ctime(&attr.st_mtime)); }
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Usa difftime(time1, time0) from time.h para obtener la diferencia entre dos tiempos. Esto calcula time1 - time0 y devuelve un double que representa la diferencia en segundos. Si es positivo, entonces time1 es posterior a time0 ; si es negativo, time0 es posterior; si es 0, son iguales.

over 4 years ago · Santiago Trujillo Report

0

Puede comparar dos valores de time_t para encontrar cuál es más nuevo:

 #include <stdio.h> #include <stdlib.h> #include <time.h> #include <sys/stat.h> static time_t getFileModifiedTime(const char *path) { struct stat attr; if (stat(path, &attr) == 0) { printf("%s: last modified time: %s", path, ctime(&attr.st_mtime)); return attr.st_mtime; } return 0; } int main(int argc, char **argv) { if (argc != 3) { fprintf(stderr, "Usage: %s file1 file2\n", argv[0]); return 1; } time_t t1 = getFileModifiedTime(argv[1]); time_t t2 = getFileModifiedTime(argv[2]); if (t1 < t2) printf("%s is older than %s\n", argv[1], argv[2]); else if (t1 > t2) printf("%s is newer than %s\n", argv[1], argv[2]); else printf("%s is the same age as %s\n", argv[1], argv[2]); return 0; }

Si desea conocer la diferencia entre los valores en segundos, debe usar difftime() oficialmente, pero en la práctica simplemente puede restar los dos valores de time_t .

over 4 years ago · Santiago Trujillo Report

0

Puedes usar el siguiente método

 double difftime (time_t end, time_t beginning);

Devuelve la diferencia horaria en segundos. Puede encontrar un ejemplo aquí.

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!