cdf_time.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*-
  2. * Copyright (c) 2008 Christos Zoulas
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
  15. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  16. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  17. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
  18. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  19. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  20. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  21. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  22. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  23. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  24. * POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "php.h"
  27. #include "file.h"
  28. #ifndef lint
  29. FILE_RCSID("@(#)$File: cdf_time.c,v 1.19 2019/03/12 20:43:05 christos Exp $")
  30. #endif
  31. #include <time.h>
  32. #ifdef TEST
  33. #include <err.h>
  34. #endif
  35. #include <string.h>
  36. #include "cdf.h"
  37. #define isleap(y) ((((y) % 4) == 0) && \
  38. ((((y) % 100) != 0) || (((y) % 400) == 0)))
  39. static const int mdays[] = {
  40. 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
  41. };
  42. /*
  43. * Return the number of days between jan 01 1601 and jan 01 of year.
  44. */
  45. static int
  46. cdf_getdays(int year)
  47. {
  48. int days = 0;
  49. int y;
  50. for (y = CDF_BASE_YEAR; y < year; y++)
  51. days += isleap(y) + 365;
  52. return days;
  53. }
  54. /*
  55. * Return the day within the month
  56. */
  57. static int
  58. cdf_getday(int year, int days)
  59. {
  60. size_t m;
  61. for (m = 0; m < __arraycount(mdays); m++) {
  62. int sub = mdays[m] + (m == 1 && isleap(year));
  63. if (days < sub)
  64. return days;
  65. days -= sub;
  66. }
  67. return days;
  68. }
  69. /*
  70. * Return the 0...11 month number.
  71. */
  72. static int
  73. cdf_getmonth(int year, int days)
  74. {
  75. size_t m;
  76. for (m = 0; m < __arraycount(mdays); m++) {
  77. days -= mdays[m];
  78. if (m == 1 && isleap(year))
  79. days--;
  80. if (days <= 0)
  81. return CAST(int, m);
  82. }
  83. return CAST(int, m);
  84. }
  85. int
  86. cdf_timestamp_to_timespec(struct timespec *ts, cdf_timestamp_t t)
  87. {
  88. struct tm tm;
  89. #ifdef HAVE_STRUCT_TM_TM_ZONE
  90. static char UTC[] = "UTC";
  91. #endif
  92. int rdays;
  93. /* Unit is 100's of nanoseconds */
  94. ts->tv_nsec = (t % CDF_TIME_PREC) * 100;
  95. t /= CDF_TIME_PREC;
  96. tm.tm_sec = CAST(int, t % 60);
  97. t /= 60;
  98. tm.tm_min = CAST(int, t % 60);
  99. t /= 60;
  100. tm.tm_hour = CAST(int, t % 24);
  101. t /= 24;
  102. /* XXX: Approx */
  103. tm.tm_year = CAST(int, CDF_BASE_YEAR + (t / 365));
  104. rdays = cdf_getdays(tm.tm_year);
  105. t -= rdays - 1;
  106. tm.tm_mday = cdf_getday(tm.tm_year, CAST(int, t));
  107. tm.tm_mon = cdf_getmonth(tm.tm_year, CAST(int, t));
  108. tm.tm_wday = 0;
  109. tm.tm_yday = 0;
  110. tm.tm_isdst = 0;
  111. #ifdef HAVE_STRUCT_TM_TM_GMTOFF
  112. tm.tm_gmtoff = 0;
  113. #endif
  114. #ifdef HAVE_STRUCT_TM_TM_ZONE
  115. tm.tm_zone = UTC;
  116. #endif
  117. tm.tm_year -= 1900;
  118. ts->tv_sec = mktime(&tm);
  119. if (ts->tv_sec == -1) {
  120. errno = EINVAL;
  121. return -1;
  122. }
  123. return 0;
  124. }
  125. int
  126. /*ARGSUSED*/
  127. cdf_timespec_to_timestamp(cdf_timestamp_t *t, const struct timespec *ts)
  128. {
  129. #ifndef __lint__
  130. (void)&t;
  131. (void)&ts;
  132. #endif
  133. #ifdef notyet
  134. struct tm tm;
  135. if (php_gmtime_r(&ts->ts_sec, &tm) == NULL) {
  136. errno = EINVAL;
  137. return -1;
  138. }
  139. *t = (ts->ts_nsec / 100) * CDF_TIME_PREC;
  140. *t = tm.tm_sec;
  141. *t += tm.tm_min * 60;
  142. *t += tm.tm_hour * 60 * 60;
  143. *t += tm.tm_mday * 60 * 60 * 24;
  144. #endif
  145. return 0;
  146. }
  147. char *
  148. cdf_ctime(const time_t *sec, char *buf)
  149. {
  150. char *ptr = php_ctime_r(sec, buf);
  151. if (ptr != NULL)
  152. return buf;
  153. (void)snprintf(buf, 26, "*Bad* %#16.16" INT64_T_FORMAT "x\n",
  154. CAST(long long, *sec));
  155. return buf;
  156. }
  157. #ifdef TEST_TIME
  158. int
  159. main(int argc, char *argv[])
  160. {
  161. struct timespec ts;
  162. char buf[25];
  163. static const cdf_timestamp_t tst = 0x01A5E403C2D59C00ULL;
  164. static const char *ref = "Sat Apr 23 01:30:00 1977";
  165. char *p, *q;
  166. cdf_timestamp_to_timespec(&ts, tst);
  167. p = cdf_ctime(&ts.tv_sec, buf);
  168. if ((q = strchr(p, '\n')) != NULL)
  169. *q = '\0';
  170. if (strcmp(ref, p) != 0)
  171. errx(1, "Error date %s != %s\n", ref, p);
  172. return 0;
  173. }
  174. #endif