cdf_time.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 "file.h"
  27. #ifndef lint
  28. FILE_RCSID("@(#)$File: cdf_time.c,v 1.8 2009/06/20 20:47:30 christos Exp $")
  29. #endif
  30. #include <time.h>
  31. #ifdef TEST
  32. #include <err.h>
  33. #endif
  34. #include <string.h>
  35. #include "cdf.h"
  36. #define isleap(y) ((((y) % 4) == 0) && \
  37. ((((y) % 100) != 0) || (((y) % 400) == 0)))
  38. static const int mdays[] = {
  39. 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
  40. };
  41. #ifdef __DJGPP__
  42. #define timespec timeval
  43. #define tv_nsec tv_usec
  44. #endif
  45. /*
  46. * Return the number of days between jan 01 1601 and jan 01 of year.
  47. */
  48. static int
  49. cdf_getdays(int year)
  50. {
  51. int days = 0;
  52. int y;
  53. for (y = CDF_BASE_YEAR; y < year; y++)
  54. days += isleap(y) + 365;
  55. return days;
  56. }
  57. /*
  58. * Return the day within the month
  59. */
  60. static int
  61. cdf_getday(int year, int days)
  62. {
  63. size_t m;
  64. for (m = 0; m < sizeof(mdays) / sizeof(mdays[0]); m++) {
  65. int sub = mdays[m] + (m == 1 && isleap(year));
  66. if (days < sub)
  67. return days;
  68. days -= sub;
  69. }
  70. return days;
  71. }
  72. /*
  73. * Return the 0...11 month number.
  74. */
  75. static int
  76. cdf_getmonth(int year, int days)
  77. {
  78. size_t m;
  79. for (m = 0; m < sizeof(mdays) / sizeof(mdays[0]); m++) {
  80. days -= mdays[m];
  81. if (m == 1 && isleap(year))
  82. days--;
  83. if (days <= 0)
  84. return (int)m;
  85. }
  86. return (int)m;
  87. }
  88. int
  89. cdf_timestamp_to_timespec(struct timespec *ts, cdf_timestamp_t t)
  90. {
  91. struct tm tm;
  92. #ifdef HAVE_STRUCT_TM_TM_ZONE
  93. static char UTC[] = "UTC";
  94. #endif
  95. int rdays;
  96. /* Unit is 100's of nanoseconds */
  97. ts->tv_nsec = (t % CDF_TIME_PREC) * 100;
  98. t /= CDF_TIME_PREC;
  99. tm.tm_sec = (int)(t % 60);
  100. t /= 60;
  101. tm.tm_min = (int)(t % 60);
  102. t /= 60;
  103. tm.tm_hour = (int)(t % 24);
  104. t /= 24;
  105. // XXX: Approx
  106. tm.tm_year = (int)(CDF_BASE_YEAR + (t / 365));
  107. rdays = cdf_getdays(tm.tm_year);
  108. t -= rdays;
  109. tm.tm_mday = cdf_getday(tm.tm_year, (int)t);
  110. tm.tm_mon = cdf_getmonth(tm.tm_year, (int)t);
  111. tm.tm_wday = 0;
  112. tm.tm_yday = 0;
  113. tm.tm_isdst = 0;
  114. #ifdef HAVE_STRUCT_TM_TM_GMTOFF
  115. tm.tm_gmtoff = 0;
  116. #endif
  117. #ifdef HAVE_STRUCT_TM_TM_ZONE
  118. tm.tm_zone = UTC;
  119. #endif
  120. tm.tm_year -= 1900;
  121. ts->tv_sec = mktime(&tm);
  122. if (ts->tv_sec == -1) {
  123. errno = EINVAL;
  124. return -1;
  125. }
  126. return 0;
  127. }
  128. int
  129. /*ARGSUSED*/
  130. cdf_timespec_to_timestamp(cdf_timestamp_t *t, const struct timespec *ts)
  131. {
  132. #ifndef __lint__
  133. (void)&t;
  134. (void)&ts;
  135. #endif
  136. #ifdef notyet
  137. struct tm tm;
  138. if (gmtime_r(&ts->ts_sec, &tm) == NULL) {
  139. errno = EINVAL;
  140. return -1;
  141. }
  142. *t = (ts->ts_nsec / 100) * CDF_TIME_PREC;
  143. *t = tm.tm_sec;
  144. *t += tm.tm_min * 60;
  145. *t += tm.tm_hour * 60 * 60;
  146. *t += tm.tm_mday * 60 * 60 * 24;
  147. #endif
  148. return 0;
  149. }
  150. #ifdef TEST
  151. int
  152. main(int argc, char *argv[])
  153. {
  154. struct timespec ts;
  155. static const cdf_timestamp_t tst = 0x01A5E403C2D59C00ULL;
  156. static const char *ref = "Sat Apr 23 01:30:00 1977";
  157. char *p, *q;
  158. cdf_timestamp_to_timespec(&ts, tst);
  159. p = ctime(&ts.tv_sec);
  160. if ((q = strchr(p, '\n')) != NULL)
  161. *q = '\0';
  162. if (strcmp(ref, p) != 0)
  163. errx(1, "Error date %s != %s\n", ref, p);
  164. return 0;
  165. }
  166. #endif