3 Copyright (C) 2007-2009 Jörg Pfähler
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 #include <libOS/LIBC_glue.h>
27 * NOTE: Helper functions
30 size_t _LIBC_month
[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
32 static bool _LIBC_is_leap_year(size_t year
)
34 if ((year
% 400) == 0)return true;
35 if ((year
% 100) == 0)return false;
36 if ((year
% 4) == 0)return true;
40 size_t _LIBC_calc_day_of_year(size_t year
, size_t month
)
43 for (size_t i
= 0;i
< month
;i
++)
44 yday
+= _LIBC_month
[i
];
45 if (_LIBC_is_leap_year(year
) == true && month
> 1)++yday
;
49 static void _LIBC_calc_month(size_t *days
,
53 while (*days
>= _LIBC_month
[*month
- 1])
55 *days
-= _LIBC_month
[*month
- 1];
70 double difftime(time_t time1
, time_t time0
)
72 return (double)(time1
- time0
);
75 time_t mktime(struct tm
*timeptr
)
77 // NOTE: See http://www.opengroup.org/onlinepubs/000095399/basedefs/xbd_chap04.html#tag_04_14
78 return timeptr
->tm_sec
+ timeptr
->tm_min
* 60 + timeptr
->tm_hour
* 3600 + timeptr
->tm_yday
* 86400 +
79 (timeptr
->tm_year
- 70) * 31536000 + ((timeptr
->tm_year
- 69) / 4) * 86400 -
80 ((timeptr
->tm_year
- 1) / 100) * 86400 + ((timeptr
->tm_year
+ 299) / 400) * 86400;
83 time_t time(time_t *tloc
)
86 _LIBOS_get_time(&time
);
87 time_t result
= mktime(&time
);
93 char *asctime(const struct tm
*timeptr
)
95 static const char wday_name
[7][3] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
96 static const char mon_name
[12][3] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
97 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
98 static char result
[26];
100 "%.3s %.3s%3d %.2d:%.2d:%.2d %d\n",
101 wday_name
[timeptr
->tm_wday
],
102 mon_name
[timeptr
->tm_mon
],
103 timeptr
->tm_mday
, timeptr
->tm_hour
,
104 timeptr
->tm_min
, timeptr
->tm_sec
,
109 char *ctime(const time_t *timer
)
111 return asctime(localtime(timer
));
114 struct tm
*gmtime(const time_t *timer
)
117 static struct tm result
;
123 result
.tm_sec
= t
% 60;
125 result
.tm_min
= t
% 60;
127 result
.tm_hour
= t
% 24;
130 result
.tm_year
= 1970;
133 bool leapYear
= _LIBC_is_leap_year(result
.tm_year
);
134 if (leapYear
== true && t
>= 366)
139 else if (leapYear
== false && t
>= 365)
151 _LIBC_calc_month(&t
, &mon
);
156 // TODO int tm_isdst;
157 _LIBOS_WARNING("libc", "incomplete implementation");
161 struct tm
*localtime(const time_t *timer
)
163 // TODO: probably not quite right, should consult the locale?
164 _LIBOS_WARNING("libc", "incomplete implementation");
165 return gmtime(timer
);
168 size_t strftime(char * restrict s
,
170 const char * restrict format
,
171 const struct tm
* restrict timeptr
)
174 while (*format
!= '\0')
186 bool e_mod
= false, o_mod
= false;
192 else if (*format
== 'O')
198 // TODO: Use the E and O modifiers
199 if (e_mod
!= 0 || o_mod
!= 0)
200 _LIBOS_WARNING("libc", "O and E modifiers not implemented");
208 *s
++ = timeptr
->tm_hour
/ 10 + '0';
209 *s
++ = timeptr
->tm_hour
% 10 + '0';
216 *s
++ = timeptr
->tm_min
/ 10 + '0';
217 *s
++ = timeptr
->tm_min
% 10 + '0';
224 *s
++ = timeptr
->tm_sec
/ 10 + '0';
225 *s
++ = timeptr
->tm_sec
% 10 + '0';
232 *s
++ = (timeptr
->tm_year
/ 10) % 10 + '0';
233 *s
++ = timeptr
->tm_year
% 10 + '0';
239 *s
++ = timeptr
->tm_year
/ 1000 + '0';
240 *s
++ = (timeptr
->tm_year
/ 100) % 10 + '0';
241 *s
++ = (timeptr
->tm_year
/ 10) % 10 + '0';
242 *s
++ = timeptr
->tm_year
% 10 + '0';
249 *s
++ = timeptr
->tm_year
/ 1000 + '0';
250 *s
++ = (timeptr
->tm_year
/ 100) % 10 + '0';
257 *s
++ = timeptr
->tm_mon
/ 10 + '0';
258 *s
++ = timeptr
->tm_mon
% 10 + '0';
265 *s
++ = timeptr
->tm_mday
/ 10 + '0';
266 *s
++ = timeptr
->tm_mday
% 10 + '0';
270 _LIBOS_WARNING("libc", "unimplemented conversion specifier");