/*
 * util.c:
 *
 * Copyright (c) 2000 James McKenzie <james@fishsoup.dhs.org>,
 * All rights reserved.
 *
 */

static char rcsid[] = "$Id: util.c,v 1.3 2000/09/24 01:46:06 root Exp $";

/*
 * $Log: util.c,v $
 * Revision 1.3  2000/09/24 01:46:06  root
 * #
 *
 * Revision 1.2  2000/09/24 01:12:49  root
 * #
 *
 * Revision 1.1  2000/09/03 03:21:53  root
 * #
 *
 * Revision 1.1  2000/09/03 02:03:28  root
 * #
 *
 */


#include "project.h"

double
timevaltod (struct timeval tv)
{
  double ret;

  ret = (double) (0.000001 * tv.tv_usec);
  ret += (double) tv.tv_sec;

  return ret;
}

struct timeval
dtotimeval (double d)
{
  struct timeval ret;
  double e;

  e = floor (d);
  d -= e;

  ret.tv_sec = (int) e;
  ret.tv_usec = (int) (d * 1000000.0);

  return ret;
}

char *
timeval_to_asc (struct timeval tv)
{
  static char ret[1024];
  struct tm tm;
  time_t t = tv.tv_sec;

#ifdef LONGNAMES
  char *dname[] = { "Sunday", "Monday", "Tuesday",
    "Wednesday", "Thursday", "Friday", "Saturday"
  };

  char *mname[] = { "January", "Feburary", "March", "April", "May",
    "June", "July", "August", "September", "October", "November",
    "December"
  };
#else
  char *dname[] = { "Sun", "Mon", "Tue",
    "Wed", "Thr", "Fri", "Sat"
  };

  char *mname[] = { "Jan", "Feb", "Mar", "Apr", "May",
    "Jun", "Jul", "Aug", "Sep", "Oct", "Nov",
    "Dec"
  };

#endif


  localtime_r (&t, &tm);

  sprintf (ret, "%s %s %2d %02d:%02d:%02d.%06d %04d",
           dname[tm.tm_wday],
           mname[tm.tm_mon],
           tm.tm_mday,
           tm.tm_hour,
           tm.tm_min, tm.tm_sec, (int) tv.tv_usec, tm.tm_year + 1900);

  return ret;
}
