home *** CD-ROM | disk | FTP | other *** search
/ Aminet 18 / aminetcdnumber181997.iso / Aminet / misc / emu / AROSdev.lha / AROS / rom / utility / amiga2date.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-02-03  |  4.3 KB  |  160 lines

  1. /*
  2.     Copyright (C) 1995-1997 AROS - The Amiga Replacement OS
  3.     $Id: amiga2date.c,v 1.7 1997/02/03 02:58:30 ldp Exp $
  4.  
  5.     Desc: Convert the date from machine to human form.
  6.     Lang: english
  7. */
  8. #include "utility_intern.h"
  9.  
  10. /*****************************************************************************
  11.  
  12.     NAME */
  13. #include <utility/date.h>
  14. #include <proto/utility.h>
  15.  
  16.         AROS_LH2(void, Amiga2Date,
  17.  
  18. /*  SYNOPSIS */
  19.         AROS_LHA(ULONG             , seconds, D0),
  20.         AROS_LHA(struct ClockData *, result, A0),
  21.  
  22. /*  LOCATION */
  23.         struct Library *, UtilityBase, 20, Utility)
  24.  
  25. /*  FUNCTION
  26.         Convert the time value given as the number of seconds since the
  27.         1st of January 1978 (00:00:00 1.1.78), to a more useful values,
  28.         which is easier for most people to understand. These values will
  29.         be stored in the ClockData structure whose address is passed as
  30.         an argument.
  31.  
  32.     INPUTS
  33.         seconds     -   Number of seconds since 1.1.78 00:00:00
  34.         result      -   The ClockData structure to store the information
  35.                         in.
  36.  
  37.     RESULT
  38.         The ClockData structure will contain the converted time values.
  39.  
  40.     NOTES
  41.  
  42.     EXAMPLE
  43.  
  44.     BUGS
  45.  
  46.     SEE ALSO
  47.  
  48.     INTERNALS
  49.         Some information about some constants I use:
  50.  
  51.              731 =  365 + 366, the number of days between 1.1.1978 and
  52.                     1.1.1976. Using 1976 makes working out leap years
  53.                     simpler.
  54.             1461 =  The number of days in four years including 1 leap year.
  55.                     (eg 365*3 + 366)
  56.            86400 =  The number of seconds in one day.
  57.  
  58.         I used these as constants so that they don't have to be computed
  59.         on the fly, or read from variables.
  60.  
  61.     HISTORY
  62.         29-10-95    digulla automatically created from
  63.                             utility_lib.fd and clib/utility_protos.h
  64.         19-05-96    iaint   Wrote, with a little help from a Perl package.
  65.         11-08-96    iaint   Updated for the new AROS format.
  66.         17-08-96    iaint   Removed calls to unimplemented UDivMod32/UMult32
  67.         24-02-97    iaint   Reimplemented, actually works now :)
  68.  
  69. *****************************************************************************/
  70. {
  71.     AROS_LIBFUNC_INIT
  72.  
  73.     static const ULONG dim[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  74.     ULONG days;
  75.     UWORD leap, temp, year;
  76.  
  77.     days = seconds / 86400;
  78.     result->wday = days % 7;
  79.  
  80.     result->sec = seconds % 60;
  81.     seconds /= 60;
  82.     result->min = seconds % 60;
  83.     seconds /= 60;
  84.     result->hour = seconds % 24;
  85.  
  86.     /*  Calculate the current year.
  87.  
  88.         Firstly, if the year is less than 1980, then the leap year
  89.         handling is not required...
  90.  
  91.     */
  92.     if(days < 1096)
  93.     {
  94.         result->year = 1978;
  95.  
  96.         if(days > 729)
  97.             leap = TRUE;
  98.         else
  99.             leap = FALSE;
  100.  
  101.         year = (days / 365);
  102.         days = days - (year * 365);
  103.     }
  104.     else
  105.     {
  106.         /*
  107.             We need to get into a year that follows a leap year, there
  108.             are two cases, >2100 and <=2100
  109.  
  110.             If the year is after 2100, which is not a leap year, then
  111.             start point is 2101.
  112.  
  113.             The first day in year 2101 is ...
  114.         */
  115.         if(days > 44925)
  116.         {
  117.             days -= 44926;
  118.             result->year = 2101;
  119.         }
  120.         /*
  121.             Otherwise, we just set everything up so that we are relative
  122.             to 1981.
  123.         */
  124.         else
  125.         {
  126.             result->year = 1981;
  127.             days -= 1096;
  128.         }
  129.  
  130.         /*
  131.             From here, we know that every remaining set of 4 years
  132.             has 1 leap year...
  133.         */
  134.         year = days / 1461;
  135.         days -= year * 1461;
  136.         result->year += year * 4;
  137.  
  138.         if(days > 1095)
  139.             leap = TRUE;
  140.         else
  141.             leap = FALSE;
  142.  
  143.         year = days / 365;
  144.         days -= year * 365;
  145.  
  146.         /* Now days is the number of days in the current year... */
  147.     } /* (not less than 1981) */
  148.  
  149.     /* days now contains the days since the beginning of the current year */
  150.     for(temp = 0; (temp == 1) ? (days >= 28 + leap) : (days >= dim[temp]); temp++)
  151.         days -= (temp == 1) ? (28 + leap) : dim[temp];
  152.  
  153.     result->month = temp + 1;
  154.     result->mday = days + 1;
  155.     result->year += year;
  156.  
  157.     AROS_LIBFUNC_EXIT
  158.  
  159. } /* Amiga2Date */
  160.