#ifndef SYS_TIME_H
#define SYS_TIME_H

/*
**        $VER: sys/time.h 1.3 (18.09.98)
**            Includes Release 45.00
**
**     Copyright © 1996/99 by CyberdyneSystems
**
**            written by Matthias Henze
**               All Rights Reserved
*/

#ifndef STORMAMIGA_H
  #include <stormamiga.h>
#endif
#ifndef _INCLUDE_TIME_H
  #include <time.h>
#endif

#ifdef __cplusplus
  extern "C" {
#endif

#ifndef DEVICES_TIMER_H
  struct timeval
  {
    long    tv_sec;               /* seconds */
    long    tv_usec;              /* microseconds */
  };
#else
  #define tv_sec  tv_secs         /* seconds */
  #define tv_usec tv_micro        /* nanoseconds */
#endif

/* Structure defined by POSIX.4 to be like a timeval. */
struct timespec
{
  time_t  ts_sec;                 /* seconds */
  long    ts_nsec;                /* nanoseconds */
};

#define TIMEVAL_TO_TIMESPEC(tv, ts) { (ts)->ts_sec = (tv)->tv_sec;                \
				    (ts)->ts_nsec = (tv)->tv_usec * 1000; }
#define TIMESPEC_TO_TIMEVAL(tv, ts) { (tv)->tv_sec = (ts)->ts_sec;                \
				    (tv)->tv_usec = (ts)->ts_nsec / 1000; }

/* Operations on timevals. */
#define timerisset(tvp)         ((tvp)->tv_sec || (tvp)->tv_usec)
#define timerclear(tvp)         (tvp)->tv_sec = (tvp)->tv_usec = 0
#define timercmp(tvp, uvp, cmp) (((tvp)->tv_sec == (uvp)->tv_sec) ?               \
				((tvp)->tv_usec cmp (uvp)->tv_usec) :             \
				((tvp)->tv_sec cmp (uvp)->tv_sec))
#define timeradd(tvp, uvp, vvp) do {                                              \
				(vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec;    \
				(vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \
				if ((vvp)->tv_usec >= 1000000) {                  \
				  (vvp)->tv_sec++;                                \
				  (vvp)->tv_usec -= 1000000; }                    \
				} while (0)
#define timersub(tvp, uvp, vvp) do {                                              \
				(vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec;    \
				(vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
				if ((vvp)->tv_usec < 0) {                         \
				  (vvp)->tv_sec--;                                \
				  (vvp)->tv_usec += 1000000; }                    \
				} while (0)   

/* Names of the interval timers, and structure defining a timer setting. */
#define ITIMER_REAL     0
#define ITIMER_VIRTUAL  1
#define ITIMER_PROF     2

struct  itimerval
{
  struct  timeval it_interval;    /* timer interval */
  struct  timeval it_value;       /* current value */
};

/* Getkerninfo clock information structure */
struct clockinfo
{
  int     hz;                     /* clock frequency */
  int     tick;                   /* micro-seconds per hz tick */
  int     tickadj;                /* clock skew rate for adjtime() */
  int     stathz;                 /* statistics clock frequency */
  int     profhz;                 /* profiling clock frequency */
};

/*----- UNIX-functions -----*/

#ifndef _POSIX_SOURCE
  int utimes  (cchar *, const struct timeval *);
#endif

#ifdef __cplusplus
  }
#endif

#ifndef _POSIX_SOURCE
  #ifdef STORMAMIGA_UNIXPATH
    __inline int utimes_u (cchar *file, const struct timeval *times)
    {return utimes        (file, times); }

    #define utimes(file, times) utimes_u(UnixToAmigaPath(file), times)
  #endif
#endif

#endif /* SYS_TIME_H */
