asc_conv.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #include <features.h>
  2. #include <ctype.h>
  3. #include <langinfo.h>
  4. #include <string.h>
  5. #include <time.h>
  6. /*
  7. * Internal ascii conversion routine, avoid use of printf, it's a bit big!
  8. */
  9. /*
  10. * Modified Manuel Novoa III Jan 2001
  11. *
  12. * Removed static function "hit" and did time-field fills inline and
  13. * put day, hour, min, and sec conversions in a loop using a small
  14. * table to reduce code size.
  15. *
  16. * Made daysp[] and mons[] const to move them from bss to text.
  17. *
  18. * Also fixed day conversion ... ANSI says no leading 0.
  19. *
  20. * Modified Erik Andersen May 2002
  21. * Changed to optionally support real locales.
  22. *
  23. */
  24. #ifdef __UCLIBC_HAS_LOCALE__
  25. /* This is defined in locale/C-time.c in the GNU libc. */
  26. extern const struct locale_data _nl_C_LC_TIME;
  27. #define __ab_weekday_name(DAY) (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (ABDAY_1)+(DAY)].string)
  28. #define __ab_month_name(MON) (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (ABMON_1)+(MON)].string)
  29. #else
  30. extern char const __ab_weekday_name[][4];
  31. extern char const __ab_month_name[][4];
  32. #define __ab_weekday_name(DAY) (__ab_weekday_name[DAY])
  33. #define __ab_month_name(MON) (__ab_month_name[MON])
  34. #endif
  35. void __asctime(register char *buffer, struct tm *ptm)
  36. {
  37. char *p;
  38. int tmp, i, tm_field[4];
  39. /* 012345678901234567890123456 */
  40. static const char template[] = "??? ??? 00 00:00:00 0000\n";
  41. /* Since we need memcpy below, use it here instead of strcpy. */
  42. memcpy(buffer, template, sizeof(template));
  43. if ((ptm->tm_wday >= 0) && (ptm->tm_wday <= 6)) {
  44. memcpy(buffer, __ab_weekday_name(ptm->tm_wday), 3);
  45. }
  46. if ((ptm->tm_mon >= 0) && (ptm->tm_mon <= 11)) {
  47. memcpy(buffer + 4, __ab_month_name(ptm->tm_mon), 3);
  48. }
  49. tm_field[0] = ptm->tm_mday;
  50. tm_field[1] = ptm->tm_hour;
  51. tm_field[2] = ptm->tm_min;
  52. tm_field[3] = ptm->tm_sec;
  53. p = buffer + 9;
  54. for (i=0 ; i<4 ; i++) {
  55. tmp = tm_field[i];
  56. *p-- += tmp % 10;
  57. *p += (tmp/10) % 10;
  58. p += 4 ; /* skip to end of next field */
  59. }
  60. tmp = ptm->tm_year + 1900;
  61. p = buffer + 23;
  62. for (i=0 ; i<4 ; i++) {
  63. *p-- += tmp % 10;
  64. tmp /= 10;
  65. }
  66. if (buffer[8] == '0') {
  67. buffer[8] = ' ';
  68. }
  69. }