asctime.c 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /*
  2. * asctime - print a date
  3. */
  4. /* $Id$ */
  5. #include <string.h>
  6. #include <time.h>
  7. #include "loc_time.h"
  8. #define DATE_STR "??? ??? ?? ??:??:?? ????\n"
  9. static char *
  10. two_digits(register char *pb, int i, int nospace)
  11. {
  12. *pb = (i / 10) % 10 + '0';
  13. if (!nospace && *pb == '0') *pb = ' ';
  14. pb++;
  15. *pb++ = (i % 10) + '0';
  16. return ++pb;
  17. }
  18. static char *
  19. four_digits(register char *pb, int i)
  20. {
  21. i %= 10000;
  22. *pb++ = (i / 1000) + '0';
  23. i %= 1000;
  24. *pb++ = (i / 100) + '0';
  25. i %= 100;
  26. *pb++ = (i / 10) + '0';
  27. *pb++ = (i % 10) + '0';
  28. return ++pb;
  29. }
  30. char *asctime(const struct tm *timeptr)
  31. {
  32. static char buf[26];
  33. register char *pb = buf;
  34. register const char *ps;
  35. register int n;
  36. strcpy(pb, DATE_STR);
  37. ps = _days[timeptr->tm_wday];
  38. n = ABB_LEN;
  39. while(--n >= 0) *pb++ = *ps++;
  40. pb++;
  41. ps = _months[timeptr->tm_mon];
  42. n = ABB_LEN;
  43. while(--n >= 0) *pb++ = *ps++;
  44. pb++;
  45. pb = two_digits(
  46. two_digits(
  47. two_digits(two_digits(pb, timeptr->tm_mday, 0)
  48. , timeptr->tm_hour, 1)
  49. , timeptr->tm_min, 1)
  50. , timeptr->tm_sec, 1);
  51. four_digits(pb, timeptr->tm_year + 1900);
  52. return buf;
  53. }