home *** CD-ROM | disk | FTP | other *** search
/ Aminet 18 / aminetcdnumber181997.iso / Aminet / misc / emu / AROSdev.lha / AROS / compiler / clib / gmtime.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-01-29  |  3.0 KB  |  163 lines

  1. /*
  2.     (C) 1995-96 AROS - The Amiga Replacement OS
  3.     $Id: gmtime.c,v 1.1 1997/01/29 16:51:34 digulla Exp $
  4.  
  5.     Desc: Convert a time into UTC.
  6.     Lang: english
  7. */
  8.  
  9. int __dstflag;
  10.  
  11. static char monthtable[] =
  12. {
  13.  /* JanFebMarAprMayJunJulAugSepOktNov */
  14.     31,29,31,30,31,30,31,31,30,31,30
  15. };
  16.  
  17. /*****************************************************************************
  18.  
  19.     NAME */
  20. #include <time.h>
  21.  
  22.     struct tm * gmtime (
  23.  
  24. /*  SYNOPSIS */
  25.     const time_t * tt)
  26.  
  27. /*  FUNCTION
  28.     The gmtime() function converts the calendar time tt to
  29.     broken-down time representation, expressed in Coordinated Universal
  30.     Time (UTC).
  31.  
  32.  
  33.     INPUTS
  34.     tt - The time to convert
  35.  
  36.     RESULT
  37.     The broken down time in Coordinated Universal Time (UTC).
  38.  
  39.     NOTES
  40.  
  41.     EXAMPLE
  42.     time_t        tt;
  43.     struct tm * tm;
  44.  
  45.     // Get the time
  46.     time (&tt);
  47.  
  48.     // and convert it
  49.     tm = gmtime (&tt);
  50.  
  51.     BUGS
  52.  
  53.     SEE ALSO
  54.     time(), ctime(), asctime(), localtime()
  55.  
  56.     INTERNALS
  57.     Rules for leap-years:
  58.  
  59.     1. every 4th year is a leap year
  60.  
  61.     2. every 100th year is none
  62.  
  63.     3. every 400th is one
  64.  
  65.     4. 1900 was none, 2000 is one
  66.  
  67.     HISTORY
  68.     29.01.1997 digulla created
  69.  
  70. ******************************************************************************/
  71. {
  72.     static struct tm utim;
  73.     signed long      tim;
  74.     int          leapday  = 0,
  75.              leapyear = 0,
  76.              i;
  77.  
  78.     tim = *tt;
  79.  
  80.     utim.tm_sec = tim % 60;
  81.     tim /= 60;
  82.  
  83.     utim.tm_min = tim % 60;
  84.     tim /= 60;
  85.  
  86.     /*
  87.     719162 number of days between 1.1.1 and 1.1.1970 if the calendar
  88.     would go so far which it doesn't :-) this is true for all of the
  89.     following.
  90.     */
  91.     utim.tm_hour = tim % 24;
  92.     tim = tim / 24 + 719162;
  93.  
  94.     utim.tm_wday = (tim + 1) % 7;
  95.  
  96.     /* 146097 number of days from 1.1.1 to 1.1.401 */
  97.     utim.tm_year = tim / 146097 * 400 - 1899;
  98.     tim %= 146097;
  99.  
  100.     /* 145731 number of days from 1.1.1 to 1.1.400 */
  101.     if (tim >= 145731)
  102.     {
  103.     leapyear ++; /* The day is in one of the 400th */
  104.  
  105.     /* Be careful: The last of the 4 centuries is 1 day longer */
  106.     if (tim == 146096)
  107.     {
  108.         tim --;
  109.         leapday ++;
  110.     }
  111.     }
  112.  
  113.     /* 36524 number of days from 1.1.1 to 1.1.101 */
  114.     utim.tm_year += tim / 36524 * 100;
  115.     tim %= 36524;
  116.  
  117.     /* 36159 number of days from 1.1.1 to 1.1.100 */
  118.     if (tim >= 36159)
  119.     leapyear --; /* The day is in one of the 100th */
  120.  
  121.     /* 1461 number of days from 1.1.1 to 1.1.5 */
  122.     utim.tm_year += tim / 1461 * 4;
  123.     tim %= 1461;
  124.  
  125.     /* 1095 number of days from 1.1.1 to 1.1.4 */
  126.     if (tim >= 1095)
  127.     {
  128.     leapyear ++; /* The day is in one of the 4th */
  129.  
  130.     /* Be careful: The 4th year is 1 day longer */
  131.     if (tim == 1460)
  132.     {
  133.         tim --;
  134.         leapday ++;
  135.     }
  136.     }
  137.  
  138.     /* 365 days in a normal year */
  139.     utim.tm_year += tim / 365;
  140.     tim = tim % 365 + leapday;
  141.  
  142.     utim.tm_yday = tim;
  143.  
  144.     if (!leapyear && tim >= 31+28)
  145.     tim ++; /* add 1 for 29-Feb if no leap year */
  146.  
  147.     /* Find the month */
  148.     for (i=0; i<11; i++)
  149.     {
  150.     if (tim < monthtable[i])
  151.         break;
  152.  
  153.     tim-=monthtable[i];
  154.     }
  155.  
  156.     utim.tm_mon = i;
  157.     utim.tm_mday = tim + 1;
  158.  
  159.     utim.tm_isdst = __dstflag;
  160.  
  161.     return &utim;
  162. } /* gmtime */
  163.