time.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #ifndef _LINUX_TIME_H
  2. #define _LINUX_TIME_H
  3. #include <rtc.h>
  4. #include <linux/types.h>
  5. #define _DEFUN(a,b,c) a(c)
  6. #define _CONST const
  7. #define _AND ,
  8. #define _REENT_ONLY
  9. #define SECSPERMIN 60L
  10. #define MINSPERHOUR 60L
  11. #define HOURSPERDAY 24L
  12. #define SECSPERHOUR (SECSPERMIN * MINSPERHOUR)
  13. #define SECSPERDAY (SECSPERHOUR * HOURSPERDAY)
  14. #define DAYSPERWEEK 7
  15. #define MONSPERYEAR 12
  16. #define YEAR_BASE 1900
  17. #define EPOCH_YEAR 1970
  18. #define EPOCH_WDAY 4
  19. #define isleap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0)
  20. /* Used by other time functions. */
  21. struct tm {
  22. int tm_sec; /* Seconds. [0-60] (1 leap second) */
  23. int tm_min; /* Minutes. [0-59] */
  24. int tm_hour; /* Hours. [0-23] */
  25. int tm_mday; /* Day. [1-31] */
  26. int tm_mon; /* Month. [0-11] */
  27. int tm_year; /* Year - 1900. */
  28. int tm_wday; /* Day of week. [0-6] */
  29. int tm_yday; /* Days in year.[0-365] */
  30. int tm_isdst; /* DST. [-1/0/1]*/
  31. # ifdef __USE_BSD
  32. long int tm_gmtoff; /* Seconds east of UTC. */
  33. __const char *tm_zone; /* Timezone abbreviation. */
  34. # else
  35. long int __tm_gmtoff; /* Seconds east of UTC. */
  36. __const char *__tm_zone; /* Timezone abbreviation. */
  37. # endif
  38. };
  39. static inline char *
  40. _DEFUN (asctime_r, (tim_p, result),
  41. _CONST struct tm *tim_p _AND
  42. char *result)
  43. {
  44. static _CONST char day_name[7][3] = {
  45. "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
  46. };
  47. static _CONST char mon_name[12][3] = {
  48. "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  49. "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  50. };
  51. sprintf (result, "%.3s %.3s %.2d %.2d:%.2d:%.2d %d\n",
  52. day_name[tim_p->tm_wday],
  53. mon_name[tim_p->tm_mon],
  54. tim_p->tm_mday, tim_p->tm_hour, tim_p->tm_min,
  55. tim_p->tm_sec, 1900 + tim_p->tm_year);
  56. return result;
  57. }
  58. static inline struct tm *
  59. _DEFUN (localtime_r, (tim_p, res),
  60. _CONST time_t * tim_p _AND
  61. struct tm *res)
  62. {
  63. static _CONST int mon_lengths[2][MONSPERYEAR] = {
  64. {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
  65. {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
  66. } ;
  67. static _CONST int year_lengths[2] = {
  68. 365,
  69. 366
  70. } ;
  71. long days, rem;
  72. int y;
  73. int yleap;
  74. _CONST int *ip;
  75. days = ((long) *tim_p) / SECSPERDAY;
  76. rem = ((long) *tim_p) % SECSPERDAY;
  77. while (rem < 0)
  78. {
  79. rem += SECSPERDAY;
  80. --days;
  81. }
  82. /* compute hour, min, and sec */
  83. res->tm_hour = (int) (rem / SECSPERHOUR);
  84. rem %= SECSPERHOUR;
  85. res->tm_min = (int) (rem / SECSPERMIN);
  86. res->tm_sec = (int) (rem % SECSPERMIN);
  87. /* compute day of week */
  88. if ((res->tm_wday = ((EPOCH_WDAY + days) % DAYSPERWEEK)) < 0)
  89. res->tm_wday += DAYSPERWEEK;
  90. /* compute year & day of year */
  91. y = EPOCH_YEAR;
  92. if (days >= 0)
  93. {
  94. for (;;)
  95. {
  96. yleap = isleap(y);
  97. if (days < year_lengths[yleap])
  98. break;
  99. y++;
  100. days -= year_lengths[yleap];
  101. }
  102. }
  103. else
  104. {
  105. do
  106. {
  107. --y;
  108. yleap = isleap(y);
  109. days += year_lengths[yleap];
  110. } while (days < 0);
  111. }
  112. res->tm_year = y - YEAR_BASE;
  113. res->tm_yday = days;
  114. ip = mon_lengths[yleap];
  115. for (res->tm_mon = 0; days >= ip[res->tm_mon]; ++res->tm_mon)
  116. days -= ip[res->tm_mon];
  117. res->tm_mday = days + 1;
  118. /* set daylight saving time flag */
  119. res->tm_isdst = -1;
  120. return (res);
  121. }
  122. static inline char *
  123. _DEFUN (ctime_r, (tim_p, result),
  124. _CONST time_t * tim_p _AND
  125. char * result)
  126. {
  127. struct tm tm;
  128. return asctime_r (localtime_r (tim_p, &tm), result);
  129. }
  130. /* for compatibility with linux code */
  131. typedef __s64 time64_t;
  132. #ifdef CONFIG_LIB_DATE
  133. time64_t mktime64(const unsigned int year, const unsigned int mon,
  134. const unsigned int day, const unsigned int hour,
  135. const unsigned int min, const unsigned int sec);
  136. #endif
  137. #endif