home *** CD-ROM | disk | FTP | other *** search
/ MACD 4 / MACD4.iso / Emulatory / AROS / utility / amiga2date.c < prev    next >
Encoding:
C/C++ Source or Header  |  1978-03-06  |  3.4 KB  |  125 lines

  1. /*
  2.     $Id: amiga2date.c,v 1.3 1996/10/24 22:51:46 aros Exp $
  3.     $Log: amiga2date.c,v $
  4.     Revision 1.3  1996/10/24 22:51:46  aros
  5.     Use proper Amiga datatypes (eg: ULONG not unsigned long)
  6.  
  7.     Revision 1.2  1996/10/24 15:51:34  aros
  8.     Use the official AROS macros over the __AROS versions.
  9.  
  10.     Revision 1.1  1996/08/31 12:58:11  aros
  11.     Merged in/modified for FreeBSD.
  12.  
  13.     Desc:
  14.     Lang: english
  15. */
  16. #include "utility_intern.h"
  17.  
  18. /*****************************************************************************
  19.  
  20.     NAME */
  21.         #include <clib/utility_protos.h>
  22.  
  23.         AROS_LH2(void, Amiga2Date,
  24.  
  25. /*  SYNOPSIS */
  26.         AROS_LHA(ULONG             , seconds, D0),
  27.         AROS_LHA(struct ClockData *, result, A0),
  28.  
  29. /*  LOCATION */
  30.         struct Library *, UtilityBase, 20, Utility)
  31.  
  32. /*  FUNCTION
  33.         Convert the time value given as the number of seconds since the
  34.         1st of January 1978 (00:00:00 1.1.78), to a more useful values,
  35.         which is easier for most people to understand. These values will
  36.         be stored in the ClockData structure whose address is passed as
  37.         an argument.
  38.  
  39.     INPUTS
  40.         seconds     -   Number of seconds since 1.1.78 00:00:00
  41.         result      -   The ClockData structure to store the information
  42.                         in.
  43.  
  44.     RESULT
  45.         The ClockData structure will contain the converted time values.
  46.  
  47.     NOTES
  48.  
  49.     EXAMPLE
  50.  
  51.     BUGS
  52.  
  53.     SEE ALSO
  54.  
  55.     INTERNALS
  56.         Some information about some constants I use:
  57.  
  58.              731 =  365 + 366, the number of days between 1.1.1978 and
  59.                     1.1.1976. Using 1976 makes working out leap years
  60.                     simpler.
  61.             1461 =  The number of days in four years including 1 leap year.
  62.                     (eg 365*3 + 366)
  63.            86400 =  The number of seconds in one day.
  64.  
  65.         I used these as constants so that they don't have to be computed
  66.         on the fly, or read from variables.
  67.  
  68.     HISTORY
  69.         29-10-95    digulla automatically created from
  70.                             utility_lib.fd and clib/utility_protos.h
  71.         19-05-96    iaint   Wrote, with a little help from a Perl package.
  72.         11-08-96    iaint   Updated for the new AROS format.
  73.         17-08-96    iaint   Removed calls to unimplemented UDivMod32/UMult32
  74.  
  75. *****************************************************************************/
  76. {
  77.     AROS_LIBFUNC_INIT
  78.  
  79.     static const ULONG dim[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  80.     ULONG days, temp, leap;
  81.  
  82.     days = seconds / 86400;
  83.     result->wday = days % 7;
  84.  
  85.     /*
  86.         using the number of days since 1.1.76 makes leap year calculations
  87.         simpler.
  88.     */
  89.  
  90.     days += 731;
  91.  
  92.     result->sec = seconds % 60;
  93.     seconds /= 60;
  94.     result->min = seconds % 60;
  95.     seconds /= 60;
  96.     result->hour= seconds % 24;
  97.  
  98.     /* Number of sets of four years since 1976 */
  99.     temp = days / 1461;
  100.  
  101.     /* days since the beginning of the last leap year  */
  102.     days %= 1461;
  103.  
  104.     temp = 1976 + (temp << 2);
  105.  
  106.     leap = (days <= 365);
  107.     if(!leap) /* not a leap year */
  108.     {
  109.         temp++;
  110.         days -= 366;
  111.         result->year = temp + (days / 365);
  112.         days %= 365;
  113.     }
  114.  
  115.     /* days now contains the days since the beginning of the current year */
  116.     for(temp = 0; (temp == 1) ? (days >= 28 + leap) : (days >= dim[temp]); temp++)
  117.         days -= (temp == 1) ? (28 + leap) : dim[temp];
  118.  
  119.     result->month = temp;
  120.     result->mday = days + 1;
  121.  
  122.     AROS_LIBFUNC_EXIT
  123.  
  124. } /* Amiga2Date */
  125.