    /*********************************************************************

                   Copyright (c) 2007 SKY Computers, Inc.

    Including this file, and any software and its file formats and  visual
    displays described herein, all  rights  reserved,  may  only  be  used
    pursuant  to  the  applicable  software  license  agreement;  contains
    confidential and proprietary information  of SKY  and/or third parties
    which is protected by copyright trade secret and trademark law and may
    not  be  provided or otherwise made available  without  prior  written
    authorization.

                       Restricted Rights Legend

    Use,  duplication,  or disclosure  by  the  Government  is  subject to
    restrictions as set  forth in subdivision  (c)(1)(ii) of the Rights in
    Technical Data and Computer  Software  clauses at  DFARS  252.227-7013
    (October 1988) and FAR 52.227-19(c) (June 1987).

    *********************************************************************/

/*
 *--------------------------------------------------------------------------
 *
 >++
 *
 *   File: ex_misc.c
 *
 *   Function: To provide several misc functions to support the TimeTrac
 *       examples.
 *
 *   Calling Sequence: int ex_spin_some (int),
 *
 *   Implementation Details:
 *       1. See below.
 *
 *   Restrictions:
 *       1. None.
 *
 >**
 *   Revision Information:
 *   Date        By   Rev      Changes Made....
 *   10/19/07    bwj  0.1      Ported from other code.
 >--
 *--------------------------------------------------------------------------
 */

/* System includes */
#include "sky_ex_inc.h"

/* ---------------------------------------------------------------------- */ 

/*
 >++
 *
 *  float ex_spin_some (int count):
 *       int count: used to set the delay time.
 *
 *       Provides a spin loop type of delay proportional to count.
 *
 *       Returns: a floating point value of no use.
 >--
 */

float ex_spin_some (int count)
{
  int i;
  float a, b, c;
  float sum;
  
  b = 3.14159;
  sum = 0.0;
  
  for (i = 0; i < count; i++) 
    {
      a = (float) i;
      sum = sum + a/ b;
    }
  
  return (sum);
}
 
/* ---------------------------------------------------------------------- */ 

/*
 >++
 *
 *   void ex_msecsleep (int delay); 
 *       int delay: delay in milliseconds.
 *
 *       This calls nanosleep for the requested  number of milliseconds 
         (can be used to delay many seconds if need be). 
 *   
 *       Returns: Nothing.
 >--  
 */   
 
void an_msecsleep (int delay)
{
  int ret;
  struct timespec tv_req, tv_rem;
  
  tv_req.tv_sec = delay / 1000;
  tv_req.tv_nsec =  (delay % 1000) * 1000000;
  
  ret = nanosleep (&tv_req, &tv_rem);
  
  /* Assume an early return is from an interrupt. */
  while (ret == -1)
    {
      if (errno == EINTR)
	ret = nanosleep (&tv_rem, &tv_rem);
      else
	return;
    }
  
  return;
}

/* ---------------------------------------------------------------------- */

/*
 >++
 *
 *   int ex_str_time (char *buff, int max_chars):
 *       char *buff: Buffer to return the character string.
 *       int max_chars: Maximum number of character to be retiurned.
 *
 *       Return to the user a string containing the current date and time in 
 *       "mm/dd/yy hh:mm:ss" format.
 *
 *       Returns: 0 on success.
 >--
 */

int ex_str_time (char *buff, int max_chars)
{
  int ret;
  struct timeval tNow;
  struct tm Lt;
  
  ret = gettimeofday (&tNow, NULL);
  if (ret == 0)
    {
      localtime_r (&tNow.tv_sec, &Lt);
      
      snprintf (buff, max_chars,
		"%02d/%02d/%02d %02d:%02d:%02d",
		Lt.tm_mon + 1, Lt.tm_mday, Lt.tm_year - 100, Lt.tm_hour, Lt.tm_min, Lt.tm_sec);
    }
  
  return (ret);
}
    
/* --------------------------- End of Module ---------------------------- */

