home *** CD-ROM | disk | FTP | other *** search
/ Amiga Elysian Archive / AmigaElysianArchive.iso / comm / xprzedza.lzh / GetSysTime.c < prev    next >
C/C++ Source or Header  |  1991-11-25  |  3KB  |  88 lines

  1. /**********************************************************************
  2.  * Amiga-type replacement for standard Unix time() function.  Can't use
  3.  * Lattice's time() function from within a library because it's not
  4.  * reentrant (has some hidden static vars it sets) and because it wants
  5.  * you to have opened dos.library for some reason, which is supposedly
  6.  * a bad idea inside a library.  Oh, well... this is quite a bit
  7.  * smaller & faster anyway.  B-)
  8.  **********************************************************************/
  9.  
  10. #include <proto/all.h>
  11. #include <exec/types.h>
  12. #include <exec/exec.h>
  13. #include <devices/timer.h>
  14. #include <string.h>
  15.  
  16. /*
  17.  * # seconds between 1-1-70 (Unix time base) and 1-1-78 (Amiga time base).
  18.  * Add this value to the returned seconds count to convert Amiga system time
  19.  * to normal Unix system time.
  20.  */
  21.  
  22. ULONG UnixTimeOffset = 252482400;
  23.  
  24. /**********************************************************
  25.  *    ULONG getsystime(struct timeval *tv)
  26.  *
  27.  *      This function was rewritten using DateStamp() to
  28.  * eliminate the opening and closing of the timer.device
  29.  * that was occurring everytime this function was called.
  30.  * An attempt to save some processing time.   -WMP-
  31.  **********************************************************/
  32. ULONG getsystime(struct timeval *tv)
  33. {
  34.    struct DateStamp ds;
  35.    ULONG  secs;
  36.  
  37.    DateStamp(&ds);
  38.  
  39.    secs = (ds.ds_Days * 86400) + (ds.ds_Minute * 60)
  40.       + (ds.ds_Tick / TICKS_PER_SECOND);
  41.    if (tv)
  42.    {
  43.       tv->tv_secs = secs;
  44.       tv->tv_micro = 0;        /* Not Used. */
  45.       }
  46.  
  47.    return secs;
  48.    }    /* End of ULONG getsystime() */
  49.  
  50. /**********************************************************
  51.  *    ULONG getsystime(struct timeval *tv)
  52.  *
  53.  * This is the old function and is commented out.
  54.  **********************************************************
  55.  *
  56.  * Returns current system time in standard Amiga-style timeval
  57.  * structure, if you pass a pointer to one.  Also returns the
  58.  * seconds part as the return value.  This lets you get just
  59.  * the seconds by calling getsystime(NULL) if that's all you
  60.  * want, or the full timeval struct if you need that.  This
  61.  * is very similar to how the standard time() function is used.
  62.  *
  63.  **********************************************************
  64. ULONG getsystime(struct timeval *tv)
  65. {
  66.    struct timerequest tr;
  67.  
  68.    /*
  69.     * timer.device must be working or the system would've died, so let's
  70.     * not bother with error checking.
  71.     *
  72.    memset(&tr, 0, sizeof(tr));
  73.    OpenDevice(TIMERNAME, UNIT_VBLANK, (struct IORequest *) &tr, 0L);
  74.  
  75.    tr.tr_node.io_Message.mn_Node.ln_Type = NT_MESSAGE;
  76.    tr.tr_node.io_Command = TR_GETSYSTIME;
  77.  
  78.    DoIO((struct IORequest *) &tr);
  79.  
  80.    if (tv)
  81.       *tv = tr.tr_time;
  82.  
  83.    CloseDevice((struct IORequest *) &tr);
  84.  
  85.    return tr.tr_time.tv_secs;
  86.    }    /* End of ULONG getsystime() */
  87. /* End of getsystime.c source */
  88.