constant strings should be in codespace
[fw/sdcc] / device / lib / time.c
1 /*-------------------------------------------------------------------------
2   time.c - stdlib time conversion routines
3   
4    Written By - Johan Knol, johan.knol@iduna.nl
5     
6    This program is free software; you can redistribute it and/or modify it
7    under the terms of the GNU General Public License as published by the
8    Free Software Foundation; either version 2, or (at your option) any
9    later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19    
20    In other words, you are welcome to use, share and improve this program.
21    You are forbidden to forbid anyone else to use, share and improve
22    what you give them.   Help stamp out software-hoarding!  
23 -------------------------------------------------------------------------*/
24
25 #include <stdio.h>
26 #include <time.h>
27
28 #define FIXDS390BUG (long)
29
30 // please note that the tm structure has the years since 1900,
31 // but time returns the seconds since 1970
32
33 /* You need some kind of real time clock for the time() function.
34    Either a rtc-chip or some kind of DCF device will do. For TINI, the 
35    HAVE_RTC is defined in tinibios.h
36    If not, the conversion routines still work.
37 */
38
39 #ifndef HAVE_RTC
40 unsigned char RtcRead(struct tm *timeptr) {
41   // no real time hardware 
42   timeptr; // hush the compiler
43   return 0;
44 }
45 #endif
46
47 // return the calendar time, seconds since the Epoch (Jan 1 1970 00:00:00)
48 time_t time(time_t *timeptr) {
49   struct tm now;
50   time_t t=-1;
51
52   if (RtcRead(&now)) {
53     t=mktime(&now);
54   }
55   if (timeptr) {
56     *timeptr=t;
57   }
58   return t;
59 }
60
61 static _CODE char monthDays[]={31,28,31,30,31,30,31,31,30,31,30,31};
62
63 _CODE char * _CODE __month[]={"Jan","Feb","Mar","Apr","May","Jun",
64                               "Jul","Aug","Sep","Oct","Nov","Dec"};
65
66 _CODE char * _CODE __day[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
67
68 static char ascTimeBuffer[32];
69
70 // validate the tm structure
71 static void CheckTime(struct tm *timeptr) {
72     // we could do some normalization here, e.g.
73     // change 40 october to 9 november
74     if (timeptr->tm_sec<0) timeptr->tm_sec=0;
75     else if (timeptr->tm_sec>59) timeptr->tm_sec=59;
76     if (timeptr->tm_min<0) timeptr->tm_min=0;
77     else if (timeptr->tm_min>59) timeptr->tm_min=59;
78     if (timeptr->tm_hour<0) timeptr->tm_hour=0;
79     else if (timeptr->tm_hour>23) timeptr->tm_hour=23;
80     if (timeptr->tm_wday<0) timeptr->tm_wday=0;
81     else if (timeptr->tm_wday>6) timeptr->tm_wday=6;
82     if (timeptr->tm_mday<1) timeptr->tm_mday=1;
83     else if (timeptr->tm_mday>31) timeptr->tm_mday=31;
84     if (timeptr->tm_mon<0) timeptr->tm_mon=0;
85     else if (timeptr->tm_mon>11) timeptr->tm_mon=11;
86     if (timeptr->tm_year<0) timeptr->tm_year=0;
87 }
88
89 // format the time into "Sat Feb 17 17:45:23 2001\n"
90 char *asctime(struct tm *timeptr) {
91   CheckTime(timeptr);
92   sprintf (ascTimeBuffer, "%s %s %2d %02d:%02d:%02d %04d\n",
93            __day[timeptr->tm_wday], __month[timeptr->tm_mon], timeptr->tm_mday,
94            timeptr->tm_hour, timeptr->tm_min, timeptr->tm_sec, 
95            timeptr->tm_year+1900);
96   return ascTimeBuffer;
97 }
98
99 char *ctime(time_t *timep) {
100   return asctime(localtime(timep));
101 }
102
103 static struct tm lastTime;
104
105 /* convert calendar time (seconds since 1970) to broken-time
106    This only works for dates between 01-01-1970 00:00:00 and 
107    19-01-2038 03:14:07
108
109    A leap year is ((((year%4)==0) && ((year%100)!=0)) || ((year%400)==0)) 
110    but since we have no fancy years between 1970 and 2038 we can do:
111 */
112
113 #define LEAP_YEAR(year) ((year%4)==0)
114
115 // forget about timezones for now
116 struct tm *localtime(time_t *timep) {
117     return gmtime(timep);
118 }
119
120 struct tm *gmtime(time_t *timep) {
121   unsigned long epoch=*timep;
122   unsigned int year;
123   unsigned char month, monthLength;
124   unsigned long days;
125   
126   lastTime.tm_sec=epoch%60;
127   epoch/=60; // now it is minutes
128   lastTime.tm_min=epoch%60;
129   epoch/=60; // now it is hours
130   lastTime.tm_hour=epoch%24;
131   epoch/=24; // now it is days
132   lastTime.tm_wday=(epoch+4)%7;
133   
134   year=1970;
135   days=0;
136   while((days += (LEAP_YEAR(year) ? 366 : 365)) <= epoch) {
137     year++;
138   }
139   lastTime.tm_year=year-1900;
140   
141   days -= LEAP_YEAR(year) ? 366 : 365;
142   epoch -= days; // now it is days in this year, starting at 0
143   lastTime.tm_yday=epoch;
144   
145   days=0;
146   month=0;
147   monthLength=0;
148   for (month=0; month<12; month++) {
149     if (month==1) { // februari
150       if (LEAP_YEAR(year)) {
151         monthLength=29;
152       } else {
153         monthLength=28;
154       }
155     } else {
156       monthLength = monthDays[month];
157     }
158     
159     if (epoch>=monthLength) {
160       epoch-=monthLength;
161     } else {
162         break;
163     }
164   }
165   lastTime.tm_mon=month;
166   lastTime.tm_mday=epoch+1;
167   
168   lastTime.tm_isdst=0;
169   
170   return &lastTime;
171 }
172
173 // convert broken time to calendar time (seconds since 1970)
174 time_t mktime(struct tm *timeptr) {
175     int year=timeptr->tm_year+1900, month=timeptr->tm_mon, i;
176     long seconds;
177     
178     CheckTime(timeptr);
179
180     // seconds from 1970 till 1 jan 00:00:00 this year
181     seconds= FIXDS390BUG (year-1970)*60*60*24*365;
182
183     // add extra days for leap years
184     for (i=1970; i<year; i++) {
185         if (LEAP_YEAR(i)) {
186             seconds+= FIXDS390BUG 60*60*24;
187         }
188     }
189
190     // add days for this year
191     for (i=0; i<month; i++) {
192       if (i==1 && LEAP_YEAR(year)) { 
193         seconds+= FIXDS390BUG 60*60*24*29;
194       } else {
195         seconds+= FIXDS390BUG 60*60*24*monthDays[i];
196       }
197     }
198
199     seconds+= FIXDS390BUG (timeptr->tm_mday-1)*60*60*24;
200     seconds+= FIXDS390BUG timeptr->tm_hour*60*60;
201     seconds+= FIXDS390BUG timeptr->tm_min*60;
202     seconds+= FIXDS390BUG timeptr->tm_sec;
203     return seconds;
204 }
205