Hackfix and re-enable strtoull and wcstoull, see bug #3798.
[sdcc.git] / sdcc / device / lib / time.c
blob4e6e3d1f2178d760af2ca1b3e6f9c31bf3cfbf74
1 /*-------------------------------------------------------------------------
2 time.c - stdlib time conversion routines
4 Copyright (C) 2001, Johan Knol <johan.knol AT iduna.nl>
6 This library 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.
11 This library 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.
16 You should have received a copy of the GNU General Public License
17 along with this library; see the file COPYING. If not, write to the
18 Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
19 MA 02110-1301, USA.
21 As a special exception, if you link this library with other files,
22 some of which are compiled with SDCC, to produce an executable,
23 this library does not by itself cause the resulting executable to
24 be covered by the GNU General Public License. This exception does
25 not however invalidate any other reasons why the executable file
26 might be covered by the GNU General Public License.
27 -------------------------------------------------------------------------*/
29 #include <stdio.h>
30 #include <time.h>
32 // please note that the tm structure has the years since 1900,
33 // but time returns the seconds since 1970
35 /* You need some kind of real time clock for the time() function.
36 Either a rtc-chip or some kind of DCF device will do. For TINI, the
37 HAVE_RTC is defined in tinibios.h
38 If not, the conversion routines still work.
41 #ifndef HAVE_RTC
42 unsigned char RtcRead(struct tm *timeptr) {
43 // no real time hardware
44 timeptr; // hush the compiler
45 return 0;
47 #endif
49 // return the calendar time, seconds since the Epoch (Jan 1 1970 00:00:00)
50 time_t time(time_t *timeptr) {
51 struct tm now;
52 time_t t=(time_t) -1;
54 if (RtcRead(&now)) {
55 t=mktime(&now);
57 if (timeptr) {
58 *timeptr=t;
60 return t;
63 static const char monthDays[]={31,28,31,30,31,30,31,31,30,31,30,31};
65 const const char *const __month[]={"Jan","Feb","Mar","Apr","May","Jun",
66 "Jul","Aug","Sep","Oct","Nov","Dec"};
68 const const char *const __day[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
70 static char ascTimeBuffer[32];
72 // validate the tm structure
73 static void CheckTime(struct tm *timeptr) {
74 // we could do some normalization here, e.g.
75 // change 40 october to 9 november
76 #if !__TIME_UNSIGNED
77 if (timeptr->tm_sec<0) timeptr->tm_sec=0;
78 if (timeptr->tm_min<0) timeptr->tm_min=0;
79 if (timeptr->tm_hour<0) timeptr->tm_hour=0;
80 if (timeptr->tm_wday<0) timeptr->tm_wday=0;
81 if (timeptr->tm_mon<0) timeptr->tm_mon=0;
82 #endif
84 if (timeptr->tm_sec>59) timeptr->tm_sec=59;
85 if (timeptr->tm_min>59) timeptr->tm_min=59;
86 if (timeptr->tm_hour>23) timeptr->tm_hour=23;
87 if (timeptr->tm_wday>6) timeptr->tm_wday=6;
88 if (timeptr->tm_mday<1) timeptr->tm_mday=1;
89 else if (timeptr->tm_mday>31) timeptr->tm_mday=31;
90 if (timeptr->tm_mon>11) timeptr->tm_mon=11;
91 if (timeptr->tm_year<0) timeptr->tm_year=0;
94 // format the time into "Sat Feb 17 17:45:23 2001\n"
95 char *asctime(struct tm *timeptr) {
96 CheckTime(timeptr);
97 sprintf (ascTimeBuffer, "%s %s %2d %02d:%02d:%02d %04d\n",
98 __day[timeptr->tm_wday], __month[timeptr->tm_mon], timeptr->tm_mday,
99 timeptr->tm_hour, timeptr->tm_min, timeptr->tm_sec,
100 timeptr->tm_year+1900);
101 return ascTimeBuffer;
104 char *ctime(time_t *timep) {
105 return asctime(localtime(timep));
108 static struct tm lastTime;
110 /* convert calendar time (seconds since 1970) to broken-time
111 This only works for dates between 01-01-1970 00:00:00 and
112 19-01-2038 03:14:07
114 A leap year is ((((year%4)==0) && ((year%100)!=0)) || ((year%400)==0))
115 but since we have no fancy years between 1970 and 2038 we can do:
118 #define LEAP_YEAR(year) ((year%4)==0)
120 // forget about timezones for now
121 struct tm *localtime(time_t *timep) {
122 return gmtime(timep);
125 struct tm *gmtime(time_t *timep) {
126 unsigned long epoch=*timep;
127 unsigned int year;
128 unsigned char month, monthLength;
129 unsigned long days;
131 lastTime.tm_sec=epoch%60;
132 epoch/=60; // now it is minutes
133 lastTime.tm_min=epoch%60;
134 epoch/=60; // now it is hours
135 lastTime.tm_hour=epoch%24;
136 epoch/=24; // now it is days
137 lastTime.tm_wday=(epoch+4)%7;
139 year=1970;
140 days=0;
141 while((days += (LEAP_YEAR(year) ? 366 : 365)) <= epoch) {
142 year++;
144 lastTime.tm_year=year-1900;
146 days -= LEAP_YEAR(year) ? 366 : 365;
147 epoch -= days; // now it is days in this year, starting at 0
148 lastTime.tm_yday=epoch;
150 days=0;
151 month=0;
152 monthLength=0;
153 for (month=0; month<12; month++) {
154 if (month==1) { // februari
155 if (LEAP_YEAR(year)) {
156 monthLength=29;
157 } else {
158 monthLength=28;
160 } else {
161 monthLength = monthDays[month];
164 if (epoch>=monthLength) {
165 epoch-=monthLength;
166 } else {
167 break;
170 lastTime.tm_mon=month;
171 lastTime.tm_mday=epoch+1;
173 lastTime.tm_isdst=0;
175 return &lastTime;
178 // convert broken time to calendar time (seconds since 1970)
179 time_t mktime(struct tm *timeptr) {
180 int year=timeptr->tm_year+1900, month=timeptr->tm_mon, i;
181 long seconds;
183 CheckTime(timeptr);
185 // seconds from 1970 till 1 jan 00:00:00 this year
186 seconds= (year-1970)*(60*60*24L*365);
188 // add extra days for leap years
189 for (i=1970; i<year; i++) {
190 if (LEAP_YEAR(i)) {
191 seconds+= 60*60*24L;
195 // add days for this year
196 for (i=0; i<month; i++) {
197 if (i==1 && LEAP_YEAR(year)) {
198 seconds+= 60*60*24L*29;
199 } else {
200 seconds+= 60*60*24L*monthDays[i];
204 seconds+= (timeptr->tm_mday-1)*60*60*24L;
205 seconds+= timeptr->tm_hour*60*60L;
206 seconds+= timeptr->tm_min*60;
207 seconds+= timeptr->tm_sec;
208 return seconds;