ctime.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /* $OpenBSD: ctime.c,v 1.5 2003/08/11 06:23:09 deraadt Exp $ */
  2. /*
  3. * Copyright (c) 1998 Michael Shalayeff
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  16. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  17. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  18. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  19. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  20. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  21. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  22. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  24. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <sys/time.h>
  27. #include "stand.h"
  28. #define isleap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0)
  29. char *
  30. ctime(const time_t *clock)
  31. {
  32. static const char wdays[][4] = {
  33. "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
  34. };
  35. static const char months[][4] = {
  36. "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  37. "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  38. };
  39. static const u_int monthcnt[] = {
  40. 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
  41. };
  42. static char buf[64];
  43. int ss, mm, hh, wday, month, year;
  44. time_t tt = *clock;
  45. ss = tt % 60;
  46. tt /= 60; /* minutes */
  47. mm = tt % 60;
  48. tt /= 60; /* hours */
  49. hh = tt % 24;
  50. tt /= 24; /* days */
  51. wday = (4 + tt) % 7; /* weekday, 'twas thursday when time started */
  52. for (year = 1970; tt >= 365; year++)
  53. tt -= isleap(year)? 366: 365;
  54. tt++; /* days are 1-based */
  55. for (month = 0; tt > monthcnt[month]; month++)
  56. tt -= monthcnt[month];
  57. if (month > 2 && isleap(year))
  58. tt--;
  59. /* no field widths in printf() */
  60. snprintf(buf, sizeof buf, "%s %s %d %d:%d:%d %d\n",
  61. ((wday < 0 || wday >= 7)? "???": wdays[wday]),
  62. ((month < 0 || month >= 12)? "???": months[month]),
  63. (int)tt, hh, mm, ss, year);
  64. return buf;
  65. }