udftime.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /* Copyright (C) 1993, 1994, 1995, 1996, 1997 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Paul Eggert (eggert@twinsun.com).
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public License as
  6. published by the Free Software Foundation; either version 2 of the
  7. License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Library General Public License for more details.
  12. You should have received a copy of the GNU Library General Public
  13. License along with the GNU C Library; see the file COPYING.LIB. If not,
  14. write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA. */
  16. /*
  17. * dgb 10/02/98: ripped this from glibc source to help convert timestamps to unix time
  18. * 10/04/98: added new table-based lookup after seeing how ugly the gnu code is
  19. * blf 09/27/99: ripped out all the old code and inserted new table from
  20. * John Brockmeyer (without leap second corrections)
  21. * rewrote udf_stamp_to_time and fixed timezone accounting in
  22. udf_time_to_stamp.
  23. */
  24. /*
  25. * We don't take into account leap seconds. This may be correct or incorrect.
  26. * For more NIST information (especially dealing with leap seconds), see:
  27. * http://www.boulder.nist.gov/timefreq/pubs/bulletin/leapsecond.htm
  28. */
  29. #include <linux/types.h>
  30. #include <linux/kernel.h>
  31. #include "udfdecl.h"
  32. #define EPOCH_YEAR 1970
  33. #ifndef __isleap
  34. /* Nonzero if YEAR is a leap year (every 4 years,
  35. except every 100th isn't, and every 400th is). */
  36. #define __isleap(year) \
  37. ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
  38. #endif
  39. /* How many days come before each month (0-12). */
  40. static const unsigned short int __mon_yday[2][13] =
  41. {
  42. /* Normal years. */
  43. { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
  44. /* Leap years. */
  45. { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
  46. };
  47. #define MAX_YEAR_SECONDS 69
  48. #define SPD 0x15180 /*3600*24*/
  49. #define SPY(y,l,s) (SPD * (365*y+l)+s)
  50. static time_t year_seconds[MAX_YEAR_SECONDS]= {
  51. /*1970*/ SPY( 0, 0,0), SPY( 1, 0,0), SPY( 2, 0,0), SPY( 3, 1,0),
  52. /*1974*/ SPY( 4, 1,0), SPY( 5, 1,0), SPY( 6, 1,0), SPY( 7, 2,0),
  53. /*1978*/ SPY( 8, 2,0), SPY( 9, 2,0), SPY(10, 2,0), SPY(11, 3,0),
  54. /*1982*/ SPY(12, 3,0), SPY(13, 3,0), SPY(14, 3,0), SPY(15, 4,0),
  55. /*1986*/ SPY(16, 4,0), SPY(17, 4,0), SPY(18, 4,0), SPY(19, 5,0),
  56. /*1990*/ SPY(20, 5,0), SPY(21, 5,0), SPY(22, 5,0), SPY(23, 6,0),
  57. /*1994*/ SPY(24, 6,0), SPY(25, 6,0), SPY(26, 6,0), SPY(27, 7,0),
  58. /*1998*/ SPY(28, 7,0), SPY(29, 7,0), SPY(30, 7,0), SPY(31, 8,0),
  59. /*2002*/ SPY(32, 8,0), SPY(33, 8,0), SPY(34, 8,0), SPY(35, 9,0),
  60. /*2006*/ SPY(36, 9,0), SPY(37, 9,0), SPY(38, 9,0), SPY(39,10,0),
  61. /*2010*/ SPY(40,10,0), SPY(41,10,0), SPY(42,10,0), SPY(43,11,0),
  62. /*2014*/ SPY(44,11,0), SPY(45,11,0), SPY(46,11,0), SPY(47,12,0),
  63. /*2018*/ SPY(48,12,0), SPY(49,12,0), SPY(50,12,0), SPY(51,13,0),
  64. /*2022*/ SPY(52,13,0), SPY(53,13,0), SPY(54,13,0), SPY(55,14,0),
  65. /*2026*/ SPY(56,14,0), SPY(57,14,0), SPY(58,14,0), SPY(59,15,0),
  66. /*2030*/ SPY(60,15,0), SPY(61,15,0), SPY(62,15,0), SPY(63,16,0),
  67. /*2034*/ SPY(64,16,0), SPY(65,16,0), SPY(66,16,0), SPY(67,17,0),
  68. /*2038*/ SPY(68,17,0)
  69. };
  70. extern struct timezone sys_tz;
  71. #define SECS_PER_HOUR (60 * 60)
  72. #define SECS_PER_DAY (SECS_PER_HOUR * 24)
  73. time_t *
  74. udf_stamp_to_time(time_t *dest, long *dest_usec, kernel_timestamp src)
  75. {
  76. int yday;
  77. uint8_t type = src.typeAndTimezone >> 12;
  78. int16_t offset;
  79. if (type == 1)
  80. {
  81. offset = src.typeAndTimezone << 4;
  82. /* sign extent offset */
  83. offset = (offset >> 4);
  84. if (offset == -2047) /* unspecified offset */
  85. offset = 0;
  86. }
  87. else
  88. offset = 0;
  89. if ((src.year < EPOCH_YEAR) ||
  90. (src.year >= EPOCH_YEAR+MAX_YEAR_SECONDS))
  91. {
  92. *dest = -1;
  93. *dest_usec = -1;
  94. return NULL;
  95. }
  96. *dest = year_seconds[src.year - EPOCH_YEAR];
  97. *dest -= offset * 60;
  98. yday = ((__mon_yday[__isleap (src.year)]
  99. [src.month-1]) + (src.day-1));
  100. *dest += ( ( (yday* 24) + src.hour ) * 60 + src.minute ) * 60 + src.second;
  101. *dest_usec = src.centiseconds * 10000 + src.hundredsOfMicroseconds * 100 + src.microseconds;
  102. return dest;
  103. }
  104. kernel_timestamp *
  105. udf_time_to_stamp(kernel_timestamp *dest, struct timespec ts)
  106. {
  107. long int days, rem, y;
  108. const unsigned short int *ip;
  109. int16_t offset;
  110. offset = -sys_tz.tz_minuteswest;
  111. if (!dest)
  112. return NULL;
  113. dest->typeAndTimezone = 0x1000 | (offset & 0x0FFF);
  114. ts.tv_sec += offset * 60;
  115. days = ts.tv_sec / SECS_PER_DAY;
  116. rem = ts.tv_sec % SECS_PER_DAY;
  117. dest->hour = rem / SECS_PER_HOUR;
  118. rem %= SECS_PER_HOUR;
  119. dest->minute = rem / 60;
  120. dest->second = rem % 60;
  121. y = 1970;
  122. #define DIV(a,b) ((a) / (b) - ((a) % (b) < 0))
  123. #define LEAPS_THRU_END_OF(y) (DIV (y, 4) - DIV (y, 100) + DIV (y, 400))
  124. while (days < 0 || days >= (__isleap(y) ? 366 : 365))
  125. {
  126. long int yg = y + days / 365 - (days % 365 < 0);
  127. /* Adjust DAYS and Y to match the guessed year. */
  128. days -= ((yg - y) * 365
  129. + LEAPS_THRU_END_OF (yg - 1)
  130. - LEAPS_THRU_END_OF (y - 1));
  131. y = yg;
  132. }
  133. dest->year = y;
  134. ip = __mon_yday[__isleap(y)];
  135. for (y = 11; days < (long int) ip[y]; --y)
  136. continue;
  137. days -= ip[y];
  138. dest->month = y + 1;
  139. dest->day = days + 1;
  140. dest->centiseconds = ts.tv_nsec / 10000000;
  141. dest->hundredsOfMicroseconds = (ts.tv_nsec / 1000 - dest->centiseconds * 10000) / 100;
  142. dest->microseconds = (ts.tv_nsec / 1000 - dest->centiseconds * 10000 -
  143. dest->hundredsOfMicroseconds * 100);
  144. return dest;
  145. }
  146. /* EOF */