Namespaces
Variants

difftime

From cppreference.net
Definido en el encabezado <time.h>
double difftime ( time_t time_end, time_t time_beg ) ;

Calcula la diferencia entre dos tiempos de calendario como objetos time_t ( time_end - time_beg ) en segundos. Si time_end se refiere a un punto temporal anterior a time_beg entonces el resultado es negativo.

Contenidos

Parámetros

time_beg, time_end - tiempos a comparar

Valor de retorno

Diferencia entre dos tiempos en segundos.

Notas

En sistemas POSIX, time_t se mide en segundos, y difftime es equivalente a la resta aritmética, pero C y C++ permiten unidades fraccionarias para time_t .

Ejemplo

El siguiente programa calcula el número de segundos que han transcurrido desde el comienzo del mes.

#include <stdio.h>
#include <time.h>
int main(void)
{
    time_t now = time(0);
    struct tm beg = *localtime(&now);
    // set beg to the beginning of the month
    beg.tm_hour = 0,
    beg.tm_min = 0,
    beg.tm_sec = 0,
    beg.tm_mday = 1;
    double seconds = difftime(now, mktime(&beg));
    printf("%.f seconds have passed since the beginning of the month.\n", seconds);
    return 0;
}

Salida:

1937968 seconds have passed since the beginning of the month.

Referencias

  • Estándar C17 (ISO/IEC 9899:2018):
  • 7.27.2.2 La función difftime (p: 285)
  • Estándar C11 (ISO/IEC 9899:2011):
  • 7.27.2.2 La función difftime (p: 390)
  • Estándar C99 (ISO/IEC 9899:1999):
  • 7.23.2.2 La función difftime (p: 338)
  • Estándar C89/C90 (ISO/IEC 9899:1990):
  • 7.12.2.2 La función difftime (p: 171)

Véase también