time.h 3.9 KB

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