Don't call ReadArgs() if started from WB.
[tangerine.git] / compiler / clib / gmtime.c
blob6ca67788f73c7ad9d70438a74df0658ed6c09486
1 /*
2 Copyright © 1995-2003, The AROS Development Team. All rights reserved.
3 $Id$
5 Convert a time into UTC.
6 */
8 /* At the moment no daylight saving time information
9 * Implementation has to be changed when DST is implemented in AROS
11 int __dstflag = -1;
13 static char monthtable[] =
15 /* JanFebMarAprMayJunJulAugSepOktNov */
16 31,29,31,30,31,30,31,31,30,31,30
19 /*****************************************************************************
21 NAME */
22 #include <time.h>
24 struct tm * gmtime (
26 /* SYNOPSIS */
27 const time_t * tt)
29 /* FUNCTION
30 The gmtime() function converts the calendar time tt to
31 broken-down time representation, expressed in Coordinated Universal
32 Time (UTC).
35 INPUTS
36 tt - The time to convert
38 RESULT
39 The broken down time in Coordinated Universal Time (UTC).
41 NOTES
42 This function must not be used in a shared library or
43 in a threaded application.
45 EXAMPLE
46 time_t tt;
47 struct tm * tm;
49 // Get the time
50 time (&tt);
52 // and convert it
53 tm = gmtime (&tt);
55 BUGS
57 SEE ALSO
58 time(), ctime(), asctime(), localtime()
60 INTERNALS
61 Rules for leap-years:
63 1. every 4th year is a leap year
65 2. every 100th year is none
67 3. every 400th is one
69 4. 1900 was none, 2000 is one
71 ******************************************************************************/
73 static struct tm utim;
74 signed long tim;
75 int leapday = 0,
76 leapyear = 0,
79 tim = *tt;
81 utim.tm_sec = tim % 60;
82 tim /= 60;
84 utim.tm_min = tim % 60;
85 tim /= 60;
88 719162 number of days between 1.1.1 and 1.1.1970 if the calendar
89 would go so far which it doesn't :-) this is true for all of the
90 following.
92 utim.tm_hour = tim % 24;
93 tim = tim / 24 + 719162;
95 utim.tm_wday = (tim + 1) % 7;
97 /* 146097 number of days from 1.1.1 to 1.1.401 */
98 utim.tm_year = tim / 146097 * 400 - 1899;
99 tim %= 146097;
101 /* 145731 number of days from 1.1.1 to 1.1.400 */
102 if (tim >= 145731)
104 leapyear ++; /* The day is in one of the 400th */
106 /* Be careful: The last of the 4 centuries is 1 day longer */
107 if (tim == 146096)
109 tim --;
110 leapday ++;
114 /* 36524 number of days from 1.1.1 to 1.1.101 */
115 utim.tm_year += tim / 36524 * 100;
116 tim %= 36524;
118 /* 36159 number of days from 1.1.1 to 1.1.100 */
119 if (tim >= 36159)
120 leapyear --; /* The day is in one of the 100th */
122 /* 1461 number of days from 1.1.1 to 1.1.5 */
123 utim.tm_year += tim / 1461 * 4;
124 tim %= 1461;
126 /* 1095 number of days from 1.1.1 to 1.1.4 */
127 if (tim >= 1095)
129 leapyear ++; /* The day is in one of the 4th */
131 /* Be careful: The 4th year is 1 day longer */
132 if (tim == 1460)
134 tim --;
135 leapday ++;
139 /* 365 days in a normal year */
140 utim.tm_year += tim / 365;
141 tim = tim % 365 + leapday;
143 utim.tm_yday = tim;
145 if (!leapyear && tim >= 31+28)
146 tim ++; /* add 1 for 29-Feb if no leap year */
148 /* Find the month */
149 for (i=0; i<11; i++)
151 if (tim < monthtable[i])
152 break;
154 tim-=monthtable[i];
157 utim.tm_mon = i;
158 utim.tm_mday = tim + 1;
160 utim.tm_isdst = __dstflag;
162 return &utim;
163 } /* gmtime */