#ifndef AUXTIME_H
#define AUXTIME_H

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 * $Id: auxtime.c,v 2.1 2010/07/25 05:40:26 swift Exp $
 *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
/* Copyright University of Washington.   Written by Dana Swift.
 *
 * This software was developed at the University of Washington using funds
 * generously provided by the US Office of Naval Research, the National
 * Science Foundation, and NOAA.
 *  
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or (at
 * your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */
/**
   This translation unit is a drop-in replacement for the time-related
   services offered in the ANSI/ISO Standard C Library.  It is compliant
   with the ANSI/ISO standard except that multibyte characters are not
   supported and the "C" locale is the only locale implemented.  It has been
   tested against the output of the GNU Standard C Library over the full
   range of inputs for time.

   Many details about how these functions ought to be implemented were
   gleaned from the book, "The Standard C Library" by P.J. Plauger published
   by Prentice Hall (ISBN# 0-13-131509-9).

   written by Dana Swift
 *
 * RCS log of revisions to the C source code.
 *
 * \begin{verbatim}
 * $Log: auxtime.c,v $
 * Revision 2.1  2010/07/25 05:40:26  swift
 * Library module designed to compute time differences with few-millisecond resolution.
 *
 * \end{verbatim}
 *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
#define AuxTimeChangeLog "$RCSfile: auxtime.c,v $  $Revision: 2.1 $   $Date: 2010/07/25 05:40:26 $"

#include <time.h>

/*========================================================================*/
/* definition of structure 'timeval' to represent high-resolution time    */
/*========================================================================*/
/**
   This structure defines a representation of time with microsecond resolution.
   The members of the timeval structure are listed below.

     \begin{verbatim}
     tv_sec.....The number of seconds since UNIX epoch (ie., 00:00:00GMT 
                on Jan 1, 1970).
     tv_usec....The number of microseconds since tv_sec.
     \end{verbatim}
*/
struct timeval {
      time_t  tv_sec;     /* seconds */
      time_t  tv_usec;    /* microseconds */
};

/*========================================================================*/
/* definition of structure 'timezone' to represent time zones              */
/*========================================================================*/
/**
   This structure defines a representation of time with microsecond resolution.
   The members of the timeval structure are listed below.

     \begin{verbatim}
     tz_minuteswest.....The number of minutes west of Greenwich.
     tz_dsttime.........Type of DST correction.
     \end{verbatim}
*/
struct timezone
{
      int tz_minuteswest;     /* minutes west of Greenwich */
      int tz_dsttime;         /* type of DST correction */
};

/* declare prototypes for functions with external linkage */
int gettimeofday(struct timeval *tv, struct timezone *tz);
double difftime_tv(struct timeval *time1, struct timeval *time0);

#endif /* AUXTIME_H */

#include <nan.h>

/*------------------------------------------------------------------------*/
/* function to compute the time difference between two timeval's          */
/*------------------------------------------------------------------------*/
double difftime_tv(struct timeval *time1, struct timeval *time0)
{
   double dT=NaN();

   /* validate the function arguements */
   if (time1 && time0)
   {
      /* compute the difference between epochs (in seconds) */
      dT  = difftime(time1->tv_sec,time0->tv_sec);

      /* add the difference between fractions of seconds */
      dT += (double)(time1->tv_usec - time0->tv_usec)/1e6;
   }

   return dT;
}
