11/*
2- * Copyright (C) 2014-2023 Jean-Luc Barriere
2+ * Copyright (C) 2014-2024 Jean-Luc Barriere
33 *
44 * This Program is free software; you can redistribute it and/or modify
55 * it under the terms of the GNU General Public License as published by
@@ -215,6 +215,42 @@ int hex_to_num(const char *str, int *num)
215215 return 0 ;
216216}
217217
218+ unsigned uint_to_strdec (unsigned u , char * str , unsigned len , int pad )
219+ {
220+ static const char g [10 ] = {
221+ '0' , '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' ,
222+ };
223+ if (len )
224+ {
225+ char * ptr = str , * end = str + len ;
226+ unsigned n = u , n10 = u / 10 ;
227+ do
228+ {
229+ * ptr ++ = g [n - n10 * 10 ];
230+ n = n10 ;
231+ } while ((n10 = n / 10 ) > 0 && ptr < end );
232+ if (ptr < end )
233+ {
234+ /* push last digit */
235+ if (n > 0 )
236+ * ptr ++ = g [n ];
237+ /* padding */
238+ if (pad )
239+ while (ptr < end ) * ptr ++ = '0' ;
240+ }
241+ len = ptr - str ;
242+ /* reorder digits */
243+ while (-- ptr > str )
244+ {
245+ char c = * str ;
246+ * str = * ptr ;
247+ * ptr = c ;
248+ ++ str ;
249+ }
250+ }
251+ return len ;
252+ }
253+
218254time_t __timegm (struct tm * utctime_tm )
219255{
220256 time_t time ;
@@ -384,13 +420,20 @@ void time_to_iso8601utc(time_t time, BUILTIN_BUFFER *str)
384420 str -> data [0 ] = '\0' ;
385421 return ;
386422 }
387- snprintf (str -> data , sizeof (BUILTIN_BUFFER ), "%4.4d-%2.2d-%2.2dT%2.2d:%2.2d:%2.2dZ" ,
388- time_tm .tm_year + 1900 ,
389- time_tm .tm_mon + 1 ,
390- time_tm .tm_mday ,
391- time_tm .tm_hour ,
392- time_tm .tm_min ,
393- time_tm .tm_sec );
423+ /* yyyy-MM-ddTHH:mm:ssZ */
424+ uint_to_strdec (time_tm .tm_year + 1900 , str -> data , 4 , 1 );
425+ str -> data [4 ] = '-' ;
426+ uint_to_strdec (time_tm .tm_mon + 1 , str -> data + 5 , 2 , 1 );
427+ str -> data [7 ] = '-' ;
428+ uint_to_strdec (time_tm .tm_mday , str -> data + 8 , 2 , 1 );
429+ str -> data [10 ] = 'T' ;
430+ uint_to_strdec (time_tm .tm_hour , str -> data + 11 , 2 , 1 );
431+ str -> data [13 ] = ':' ;
432+ uint_to_strdec (time_tm .tm_min , str -> data + 14 , 2 , 1 );
433+ str -> data [16 ] = ':' ;
434+ uint_to_strdec (time_tm .tm_sec , str -> data + 17 , 2 , 1 );
435+ str -> data [19 ] = 'Z' ;
436+ str -> data [20 ] = '\0' ;
394437}
395438
396439void time_to_iso8601 (time_t time , BUILTIN_BUFFER * str )
@@ -402,13 +445,19 @@ void time_to_iso8601(time_t time, BUILTIN_BUFFER *str)
402445 str -> data [0 ] = '\0' ;
403446 return ;
404447 }
405- snprintf (str -> data , sizeof (BUILTIN_BUFFER ), "%4.4d-%2.2d-%2.2dT%2.2d:%2.2d:%2.2d" ,
406- time_tm .tm_year + 1900 ,
407- time_tm .tm_mon + 1 ,
408- time_tm .tm_mday ,
409- time_tm .tm_hour ,
410- time_tm .tm_min ,
411- time_tm .tm_sec );
448+ /* yyyy-MM-ddTHH:mm:ss */
449+ uint_to_strdec (time_tm .tm_year + 1900 , str -> data , 4 , 1 );
450+ str -> data [4 ] = '-' ;
451+ uint_to_strdec (time_tm .tm_mon + 1 , str -> data + 5 , 2 , 1 );
452+ str -> data [7 ] = '-' ;
453+ uint_to_strdec (time_tm .tm_mday , str -> data + 8 , 2 , 1 );
454+ str -> data [10 ] = 'T' ;
455+ uint_to_strdec (time_tm .tm_hour , str -> data + 11 , 2 , 1 );
456+ str -> data [13 ] = ':' ;
457+ uint_to_strdec (time_tm .tm_min , str -> data + 14 , 2 , 1 );
458+ str -> data [16 ] = ':' ;
459+ uint_to_strdec (time_tm .tm_sec , str -> data + 17 , 2 , 1 );
460+ str -> data [19 ] = '\0' ;
412461}
413462
414463void time_to_isodate (time_t time , BUILTIN_BUFFER * str )
@@ -420,10 +469,12 @@ void time_to_isodate(time_t time, BUILTIN_BUFFER *str)
420469 str -> data [0 ] = '\0' ;
421470 return ;
422471 }
423- snprintf (str -> data , sizeof (BUILTIN_BUFFER ), "%4.4d-%2.2d-%2.2d" ,
424- time_tm .tm_year + 1900 ,
425- time_tm .tm_mon + 1 ,
426- time_tm .tm_mday );
472+ uint_to_strdec (time_tm .tm_year + 1900 , str -> data , 4 , 1 );
473+ str -> data [4 ] = '-' ;
474+ uint_to_strdec (time_tm .tm_mon + 1 , str -> data + 5 , 2 , 1 );
475+ str -> data [7 ] = '-' ;
476+ uint_to_strdec (time_tm .tm_mday , str -> data + 8 , 2 , 1 );
477+ str -> data [10 ] = '\0' ;
427478}
428479
429480tz_t * time_tz (time_t time , tz_t * tz ) {
0 commit comments