]>
cloudbase.mooo.com Git - z180-stamp.git/blob - time/strftime.c
2 * (c)2012 Michael Duane Rice All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer. Redistributions in binary
10 * form must reproduce the above copyright notice, this list of conditions
11 * and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution. Neither the name of the copyright holders
13 * nor the names of contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
29 /* $Id: strftime.c 2391 2013-05-03 20:53:06Z swfltek $ */
32 Standard strftime(). This is a memory hungry monster.
39 extern long __utc_offset
;
43 const __memx
char strfwkdays
[] = "Sunday Monday Tuesday Wednesday Thursday Friday Saturday ";
44 const __memx
char strfmonths
[] = "January February March April May June July August September October November December ";
48 const char strfwkdays
[] = "Sunday Monday Tuesday Wednesday Thursday Friday Saturday ";
49 const char strfmonths
[] = "January February March April May June July August September October November December ";
56 pgm_copystring(const char __memx
* p
, unsigned char i
, char *b
, unsigned char l
)
62 pgm_copystring(const char *p
, unsigned char i
, char *b
, unsigned char l
)
77 while (c
!= ' ' && l
--) {
87 strftime(char *buffer
, size_t limit
, const char *pattern
, const struct tm
* timeptr
)
89 unsigned int count
, length
;
96 while (count
< limit
) {
100 if (c
== 'E' || c
== 'O')
109 length
= pgm_copystring(strfwkdays
, timeptr
->tm_wday
, _store
, 3);
113 length
= pgm_copystring(strfwkdays
, timeptr
->tm_wday
, _store
, 255);
118 length
= pgm_copystring(strfmonths
, timeptr
->tm_mon
, _store
, 3);
122 length
= pgm_copystring(strfmonths
, timeptr
->tm_mon
, _store
, 255);
126 asctime_r(timeptr
, _store
);
128 while (_store
[length
])
133 d
= timeptr
->tm_year
+ 1900;
135 length
= sprintf(_store
, "%.2d", d
);
139 length
= sprintf(_store
, "%.2u", timeptr
->tm_mday
);
143 length
= sprintf(_store
, "%.2u/%.2u/%.2u", \
144 timeptr
->tm_mon
+ 1, \
146 timeptr
->tm_year
% 100 \
151 length
= sprintf(_store
, "%2d", timeptr
->tm_mday
);
155 length
= sprintf(_store
, "%d-%.2d-%.2d", \
156 timeptr
->tm_year
+ 1900, \
157 timeptr
->tm_mon
+ 1, \
164 iso_week_date_r(timeptr
->tm_year
+ 1900, timeptr
->tm_yday
, &wd
);
166 length
= sprintf(_store
, "%.2d", wd
.year
% 100);
168 length
= sprintf(_store
, "%.4d", wd
.year
);
174 length
= sprintf(_store
, "%.2u", timeptr
->tm_hour
);
178 d
= timeptr
->tm_hour
% 12;
181 length
= sprintf(_store
, "%.2u", d
);
185 length
= sprintf(_store
, "%.3u", timeptr
->tm_yday
+ 1);
189 length
= sprintf(_store
, "%.2u", timeptr
->tm_mon
+ 1);
193 length
= sprintf(_store
, "%.2u", timeptr
->tm_min
);
204 if (timeptr
->tm_hour
> 11)
211 d
= timeptr
->tm_hour
% 12;
214 length
= sprintf(_store
, "%2d:%.2d:%.2d AM", \
219 if (timeptr
->tm_hour
> 11)
224 length
= sprintf(_store
, "%.2d:%.2d", timeptr
->tm_hour
, timeptr
->tm_min
);
228 length
= sprintf(_store
, "%.2u", timeptr
->tm_sec
);
232 length
= sprintf(_store
, "\t");
236 length
= sprintf(_store
, "%.2d:%.2d:%.2d", \
244 w
= timeptr
->tm_wday
;
247 length
= sprintf(_store
, "%d", w
);
251 length
= sprintf(_store
, "%.2u", week_of_year(timeptr
, 0));
255 iso_week_date_r(timeptr
->tm_year
+ 1900, timeptr
->tm_yday
, &wd
);
256 length
= sprintf(_store
, "%.2u", wd
.week
);
260 length
= sprintf(_store
, "%u", timeptr
->tm_wday
);
264 w
= week_of_year(timeptr
, 1);
265 length
= sprintf(_store
, "%.2u", w
);
269 length
= sprintf(_store
, "%.2u/%.2u/%.2u", timeptr
->tm_mon
+ 1, timeptr
->tm_mday
, timeptr
->tm_year
% 100);
273 length
= sprintf(_store
, "%.2u:%.2u:%.2u", timeptr
->tm_hour
, timeptr
->tm_min
, timeptr
->tm_sec
);
277 length
= sprintf(_store
, "%.2u", timeptr
->tm_year
% 100);
281 length
= sprintf(_store
, "%u", timeptr
->tm_year
+ 1900);
285 d
= __utc_offset
/ 60;
286 w
= timeptr
->tm_isdst
/ 60;
291 length
= sprintf(_store
, "%+.2d%.2d", d
, w
);
301 if ((length
+ count
) < limit
) {
303 for (d
= 0; d
< (int) length
; d
++) {
304 *buffer
++ = _store
[d
];
311 } else { /* copy a literal */