asctime.c 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /* $Id$ */
  2. #include <time.h>
  3. #define DATE_STR "??? ??? ?? ??:??:?? ????\n"
  4. static char *days[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
  5. static char *months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  6. "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
  7. static char *two_digits();
  8. static char *four_digits();
  9. char *
  10. asctime(tm)
  11. register struct tm *tm;
  12. {
  13. static char buf[32];
  14. register char *pb = buf, *ps;
  15. strcpy(pb, DATE_STR);
  16. ps = days[tm->tm_wday];
  17. while (*ps) *pb++ = *ps++;
  18. pb++;
  19. ps = months[tm->tm_mon];
  20. while (*ps) *pb++ = *ps++;
  21. pb++;
  22. pb = two_digits(
  23. two_digits(
  24. two_digits(
  25. two_digits(pb, tm->tm_mday, 0),
  26. tm->tm_hour, 1),
  27. tm->tm_min, 1),
  28. tm->tm_sec, 1);
  29. four_digits(pb, tm->tm_year+1900);
  30. return(buf);
  31. }
  32. static char *
  33. two_digits(pb, i, nospace)
  34. register char *pb;
  35. {
  36. *pb = (i / 10) % 10 + '0';
  37. if (!nospace && *pb == '0') *pb = ' ';
  38. pb++;
  39. *pb++ = (i % 10) + '0';
  40. return ++pb;
  41. }
  42. static char *
  43. four_digits(pb, i)
  44. register char *pb;
  45. {
  46. i %= 10000;
  47. *pb++ = (i / 1000) + '0';
  48. i %= 1000;
  49. *pb++ = (i / 100) + '0';
  50. i %= 100;
  51. *pb++ = (i / 10) + '0';
  52. *pb++ = (i % 10) + '0';
  53. return ++pb;
  54. }