dow.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * The MIT License (MIT)
  3. *
  4. * Copyright (c) 2015 Derick Rethans
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #include "timelib.h"
  25. static int m_table_common[13] = { -1, 0, 3, 3, 6, 1, 4, 6, 2, 5, 0, 3, 5 }; /* 1 = jan */
  26. static int m_table_leap[13] = { -1, 6, 2, 3, 6, 1, 4, 6, 2, 5, 0, 3, 5 }; /* 1 = jan */
  27. static timelib_sll positive_mod(timelib_sll x, timelib_sll y)
  28. {
  29. timelib_sll tmp;
  30. tmp = x % y;
  31. if (tmp < 0) {
  32. tmp += y;
  33. }
  34. return tmp;
  35. }
  36. static timelib_sll century_value(timelib_sll j)
  37. {
  38. return 6 - positive_mod(j, 4) * 2;
  39. }
  40. static timelib_sll timelib_day_of_week_ex(timelib_sll y, timelib_sll m, timelib_sll d, int iso)
  41. {
  42. timelib_sll c1, y1, m1, dow;
  43. /* Only valid for Gregorian calendar, commented out as we don't handle
  44. * Julian calendar. We just return the 'wrong' day of week to be
  45. * consistent. */
  46. c1 = century_value(y / 100);
  47. y1 = positive_mod(y, 100);
  48. m1 = timelib_is_leap(y) ? m_table_leap[m] : m_table_common[m];
  49. dow = positive_mod((c1 + y1 + m1 + (y1 / 4) + d), 7);
  50. if (iso) {
  51. if (dow == 0) {
  52. dow = 7;
  53. }
  54. }
  55. return dow;
  56. }
  57. timelib_sll timelib_day_of_week(timelib_sll y, timelib_sll m, timelib_sll d)
  58. {
  59. return timelib_day_of_week_ex(y, m, d, 0);
  60. }
  61. timelib_sll timelib_iso_day_of_week(timelib_sll y, timelib_sll m, timelib_sll d)
  62. {
  63. return timelib_day_of_week_ex(y, m, d, 1);
  64. }
  65. /* jan feb mar apr may jun jul aug sep oct nov dec */
  66. static int d_table_common[13] = { 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
  67. static int d_table_leap[13] = { 0, 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335 };
  68. static int ml_table_common[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  69. static int ml_table_leap[13] = { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  70. timelib_sll timelib_day_of_year(timelib_sll y, timelib_sll m, timelib_sll d)
  71. {
  72. return (timelib_is_leap(y) ? d_table_leap[m] : d_table_common[m]) + d - 1;
  73. }
  74. timelib_sll timelib_days_in_month(timelib_sll y, timelib_sll m)
  75. {
  76. return timelib_is_leap(y) ? ml_table_leap[m] : ml_table_common[m];
  77. }
  78. void timelib_isoweek_from_date(timelib_sll y, timelib_sll m, timelib_sll d, timelib_sll *iw, timelib_sll *iy)
  79. {
  80. int y_leap, prev_y_leap, doy, jan1weekday, weekday;
  81. y_leap = timelib_is_leap(y);
  82. prev_y_leap = timelib_is_leap(y-1);
  83. doy = timelib_day_of_year(y, m, d) + 1;
  84. if (y_leap && m > 2) {
  85. doy++;
  86. }
  87. jan1weekday = timelib_day_of_week(y, 1, 1);
  88. weekday = timelib_day_of_week(y, m, d);
  89. if (weekday == 0) weekday = 7;
  90. if (jan1weekday == 0) jan1weekday = 7;
  91. /* Find if Y M D falls in YearNumber Y-1, WeekNumber 52 or 53 */
  92. if (doy <= (8 - jan1weekday) && jan1weekday > 4) {
  93. *iy = y - 1;
  94. if (jan1weekday == 5 || (jan1weekday == 6 && prev_y_leap)) {
  95. *iw = 53;
  96. } else {
  97. *iw = 52;
  98. }
  99. } else {
  100. *iy = y;
  101. }
  102. /* 8. Find if Y M D falls in YearNumber Y+1, WeekNumber 1 */
  103. if (*iy == y) {
  104. int i;
  105. i = y_leap ? 366 : 365;
  106. if ((i - (doy - y_leap)) < (4 - weekday)) {
  107. *iy = y + 1;
  108. *iw = 1;
  109. return;
  110. }
  111. }
  112. /* 9. Find if Y M D falls in YearNumber Y, WeekNumber 1 through 53 */
  113. if (*iy == y) {
  114. int j;
  115. j = doy + (7 - weekday) + (jan1weekday - 1);
  116. *iw = j / 7;
  117. if (jan1weekday > 4) {
  118. *iw -= 1;
  119. }
  120. }
  121. }
  122. timelib_sll timelib_daynr_from_weeknr(timelib_sll y, timelib_sll w, timelib_sll d)
  123. {
  124. timelib_sll dow, day;
  125. /* Figure out the dayofweek for y-1-1 */
  126. dow = timelib_day_of_week(y, 1, 1);
  127. /* then use that to figure out the offset for day 1 of week 1 */
  128. day = 0 - (dow > 4 ? dow - 7 : dow);
  129. /* Add weeks and days */
  130. return day + ((w - 1) * 7) + d;
  131. }
  132. int timelib_valid_time(timelib_sll h, timelib_sll i, timelib_sll s)
  133. {
  134. if (h < 0 || h > 23 || i < 0 || i > 59 || s < 0 || s > 59) {
  135. return 0;
  136. }
  137. return 1;
  138. }
  139. int timelib_valid_date(timelib_sll y, timelib_sll m, timelib_sll d)
  140. {
  141. if (m < 1 || m > 12 || d < 1 || d > timelib_days_in_month(y, m)) {
  142. return 0;
  143. }
  144. return 1;
  145. }
  146. #if 0
  147. int main(void)
  148. {
  149. printf("dow = %d\n", timelib_day_of_week(1978, 12, 22)); /* 5 */
  150. printf("dow = %d\n", timelib_day_of_week(2005, 2, 19)); /* 6 */
  151. }
  152. #endif