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

  1. /*
  2.     $Id: date2amiga.c,v 1.2 1996/10/24 15:51:35 aros Exp $
  3.     $Log: date2amiga.c,v $
  4.     Revision 1.2  1996/10/24 15:51:35  aros
  5.     Use the official AROS macros over the __AROS versions.
  6.  
  7.     Revision 1.1  1996/08/31 12:58:12  aros
  8.     Merged in/modified for FreeBSD.
  9.  
  10.     Desc:
  11.     Lang: english
  12. */
  13. #include "utility_intern.h"
  14.  
  15. /*****************************************************************************
  16.  
  17.     NAME */
  18.         #include <clib/utility_protos.h>
  19.  
  20.         AROS_LH1(ULONG, Date2Amiga,
  21.  
  22. /*  SYNOPSIS */
  23.         AROS_LHA(struct ClockData *, date, A0),
  24.  
  25. /*  LOCATION */
  26.         struct UtilityBase *, UtilityBase, 21, Utility)
  27.  
  28. /*  FUNCTION
  29.         Converts the information given in the struct ClockData *date, into
  30.         the number of seconds that have past since the 1st of January 1978.
  31.  
  32.     INPUTS
  33.         date    -   Contains the information about the time.
  34.  
  35.     RESULT
  36.         The number of seconds since 1.1.1978
  37.  
  38.     NOTES
  39.  
  40.     EXAMPLE
  41.  
  42.     BUGS
  43.  
  44.     SEE ALSO
  45.         utility/Amiga2Date(), utility/CheckData()
  46.  
  47.     INTERNALS
  48.         Bit of a hack in the leap year handling.
  49.  
  50.     HISTORY
  51.         29-10-95    digulla automatically created from
  52.                             utility_lib.fd and clib/utility_protos.h
  53.  
  54. *****************************************************************************/
  55. {
  56.     AROS_LIBFUNC_INIT
  57.  
  58.     /* This array contains the number of days that have been in the year
  59.        up to the start of the month. Does not take into account leap years.
  60.     */
  61.     static const UWORD dayspermonth[12] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
  62.  
  63.     ULONG time;
  64.     UWORD year;
  65.  
  66.     time = date->sec + (date->min * 60) + (date->hour * 3600);
  67.     time += (date->mday - 1)* 86400;
  68.     time += dayspermonth[date->month-1] * 86400;
  69.     time += (date->year - 1978) * 86400 * 365;
  70.  
  71.     /* Now comes the hard bit, how do we work out the extra day for a leap
  72.         year. I do it by subtracting lots of four to start with, then
  73.         by considering the remaining few years.
  74.  
  75.         For every group of four years which we have to subtract, we can
  76.         add one leap year. eg 1982 - 1978= 4, so we add one leap year (1980)
  77.  
  78.         1989 - 1978 = 11 (two lots of four subtracted), (1980, 1984, 1988).
  79.         However in this case, the 1988 is handled by the code after the
  80.         while loop.
  81.  
  82.         Is there an easier way perhaps?
  83.     */
  84.     year = date->year - 1978;
  85.     while( year > 3 )
  86.     {
  87.         if(year > 4 )   time += 86400;
  88.         year -= 4;
  89.     }
  90.  
  91.  
  92.     if( (year > 2) || ((year == 2) && (date->month > 2)))
  93.         time += 86400;
  94.  
  95.     return time;
  96.  
  97.     AROS_LIBFUNC_EXIT
  98. } /* Date2Amiga */
  99.