x11gfx.hidd: support 32 bit modes
[AROS.git] / compiler / stdc / gmtime_r.c
blob60a7930a6be1df160e116c30517a78e6fbd6e77c
1 /*
2 Copyright © 1995-2012, The AROS Development Team. All rights reserved.
3 $Id$
5 Convert a time into UTC, reentrant.
6 */
8 /* At the moment no daylight saving time information
9 * Implementation has to be changed when DST is implemented in AROS
11 static 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_r (
26 /* SYNOPSIS */
27 const time_t * tt,
28 struct tm * tm)
30 /* FUNCTION
31 The gmtime_r() function converts the calendar time tt to
32 broken-down time representation, expressed in Coordinated Universal
33 Time (UTC).
36 INPUTS
37 tt - The time to convert
38 tm - A struct tm to store the result in
40 RESULT
41 The pointer passed in tm, containing the broken down time in
42 Coordinated Universal Time (UTC).
44 NOTES
46 EXAMPLE
47 time_t tt;
48 struct tm tm;
50 // Get the time
51 time (&tt);
53 // and convert it
54 gmtime (&tt, &tm);
56 BUGS
58 SEE ALSO
59 time(), ctime_r(), asctime_r(), localtime_r()
61 INTERNALS
62 Rules for leap-years:
64 1. every 4th year is a leap year
66 2. every 100th year is none
68 3. every 400th is one
70 4. 1900 was none, 2000 is one
72 ******************************************************************************/
74 signed long tim;
75 int leapday = 0,
76 leapyear = 0,
79 tim = *tt;
81 tm->tm_sec = tim % 60;
82 tim /= 60;
84 tm->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 tm->tm_hour = tim % 24;
93 tim = tim / 24 + 719162;
95 tm->tm_wday = (tim + 1) % 7;
97 /* 146097 number of days from 1.1.1 to 1.1.401 */
98 tm->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 tm->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 tm->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 tm->tm_year += tim / 365;
141 tim = tim % 365 + leapday;
143 tm->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 tm->tm_mon = i;
158 tm->tm_mday = tim + 1;
160 tm->tm_isdst = __dstflag;
162 return tm;
163 } /* gmtime */