/contrib/ntp/libntp/humandate.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 62 lines · 43 code · 14 blank · 5 comment · 2 complexity · 39da5502032d839719ff61a95aad50fe MD5 · raw file

  1. /*
  2. * humandate - convert an NTP (or the current) time to something readable
  3. */
  4. #include <stdio.h>
  5. #include "ntp_fp.h"
  6. #include "ntp_unixtime.h" /* includes <sys/time.h> and <time.h> */
  7. #include "lib_strbuf.h"
  8. #include "ntp_stdlib.h"
  9. static const char *months[] = {
  10. "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  11. "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  12. };
  13. static const char *days[] = {
  14. "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
  15. };
  16. char *
  17. humandate(
  18. u_long ntptime
  19. )
  20. {
  21. char *bp;
  22. struct tm *tm;
  23. tm = ntp2unix_tm(ntptime, 1);
  24. if (!tm)
  25. return "--- --- -- ---- --:--:--";
  26. LIB_GETBUF(bp);
  27. (void) sprintf(bp, "%s, %s %2d %4d %2d:%02d:%02d",
  28. days[tm->tm_wday], months[tm->tm_mon], tm->tm_mday,
  29. 1900+tm->tm_year, tm->tm_hour, tm->tm_min, tm->tm_sec);
  30. return bp;
  31. }
  32. /* This is used in msyslog.c; we don't want to clutter up the log with
  33. the year and day of the week, etc.; just the minimal date and time. */
  34. char *
  35. humanlogtime(void)
  36. {
  37. char *bp;
  38. time_t cursec = time((time_t *) 0);
  39. struct tm *tm;
  40. tm = localtime(&cursec);
  41. if (!tm)
  42. return "-- --- --:--:--";
  43. LIB_GETBUF(bp);
  44. (void) sprintf(bp, "%2d %s %02d:%02d:%02d",
  45. tm->tm_mday, months[tm->tm_mon],
  46. tm->tm_hour, tm->tm_min, tm->tm_sec);
  47. return bp;
  48. }