asc_conv.c 2.0 KB

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