Advertisement
Guest User

Untitled

a guest
Apr 17th, 2014
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.00 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdint.h>
  3. #include "dt_dow.h"
  4. #include "dt_accessor.h"
  5.  
  6. const struct test {
  7. int year;
  8. int month; /* Month of the year [1=Jan, 12=Dec] */
  9. int nth; /* Occurrence within month */
  10. dt_dow_t dow; /* Day of the week [1=Mon, 7=Sun] */
  11. int dom; /* Expected day of the month [1, 31] */
  12. } tests[] = {
  13. { 2014, 3, 1, DT_SUNDAY, 2 },
  14. { 2014, 4, -1, DT_TUESDAY, 29 },
  15. { 2014, 4, -2, DT_MONDAY, 21 },
  16. { 2014, 4, -5, DT_TUESDAY, 1 },
  17. { 2014, 4, 1, DT_TUESDAY, 1 },
  18. { 2014, 12, 4, DT_WEDNESDAY, 24 },
  19. };
  20.  
  21. int
  22. main() {
  23. int i, ntests;
  24.  
  25. ntests = sizeof(tests) / sizeof(*tests);
  26. for (i = 0; i < ntests; i++) {
  27. const struct test t = tests[i];
  28.  
  29. {
  30. int dom = dt_dom(dt_from_nth_dow_in_month(t.year, t.month, t.nth, t.dow));
  31.  
  32. if (t.dom != dom) {
  33. printf("dt_dom(dt_from_nth_dow_in_month(%d, %d, %d, %d))n",
  34. t.year, t.month, t.nth, t.dow);
  35. printf(" got: %dn", dom);
  36. printf(" exp: %dn", t.dom);
  37. }
  38. }
  39. }
  40. return 0;
  41. }
  42.  
  43. #include <stdio.h>
  44. #include <assert.h>
  45. #include <stdint.h>
  46. #include <stdbool.h>
  47.  
  48. bool
  49. leap_year(int y) {
  50. return ((y % 4) == 0 && (y % 100 != 0 || y % 400 == 0));
  51. }
  52.  
  53. int
  54. days_in_month(int y, int m) {
  55. static const int T[2][13] = {
  56. { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
  57. { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
  58. };
  59. assert(m >= 1);
  60. assert(m <= 12);
  61. return T[leap_year(y)][m];
  62. }
  63.  
  64. /* Computes the day of the week [1=Mon, 7=Sun] from the given year, month, day. */
  65. int
  66. ymd_to_dow(int y, int m, int d) {
  67. static const int T[13] = { 0, 6, 2, 1, 4, 6, 2, 4, 0, 3, 5, 1, 3 };
  68.  
  69. assert(y >= 1);
  70. assert(m >= 1);
  71. assert(m <= 12);
  72. assert(d >= 1);
  73.  
  74. y -= m < 3;
  75. return 1 + (y + y/4 - y/100 + y/400 + T[m] + d) % 7;
  76. }
  77.  
  78. int
  79. dom_from_nth_dow_in_month(int y, int m, int nth, int dow) {
  80. int dim, dom;
  81.  
  82. assert(y >= 1);
  83. assert(m >= 1);
  84. assert(m <= 12);
  85. assert(dow >= 1);
  86. assert(dow <= 7);
  87.  
  88. dim = days_in_month(y, m);
  89. if (nth > 0) {
  90. dom = 1;
  91. dom += (dow - ymd_to_dow(y, m, dom) + 7) % 7;
  92. dom += --nth * 7;
  93. if (dom <= dim)
  94. return dom;
  95. }
  96. else if (nth < 0) {
  97. dom = dim;
  98. dom -= (ymd_to_dow(y, m, dom) - dow + 7) % 7;
  99. dom -= ++nth * -7;
  100. if (dom >= 1)
  101. return dom;
  102. }
  103. return -1;
  104. }
  105.  
  106. const struct test {
  107. int year;
  108. int month; /* Month of the year [1=Jan, 12=Dec] */
  109. int nth; /* Occurrence within month */
  110. int dow; /* Day of the week [1=Mon, 7=Sun] */
  111. int dom; /* Expected day of the month [1, 31] */
  112. } tests[] = {
  113. { 2014, 3, 1, 7, 2 },
  114. { 2014, 4, -1, 2, 29 },
  115. { 2014, 4, -2, 1, 21 },
  116. { 2014, 4, -5, 2, 1 },
  117. { 2014, 4, 1, 2, 1 },
  118. { 2014, 12, 4, 3, 24 },
  119. };
  120.  
  121. int
  122. main() {
  123. int i, ntests;
  124.  
  125. ntests = sizeof(tests) / sizeof(*tests);
  126. for (i = 0; i < ntests; i++) {
  127. const struct test t = tests[i];
  128.  
  129. {
  130. int dom = dom_from_nth_dow_in_month(t.year, t.month, t.nth, t.dow);
  131.  
  132. if (t.dom != dom) {
  133. printf("dom_from_nth_dow_in_month(%d, %d, %d, %d))n",
  134. t.year, t.month, t.nth, t.dow);
  135. printf(" got: %dn", dom);
  136. printf(" exp: %dn", t.dom);
  137. }
  138. }
  139. }
  140. return 0;
  141. }
  142.  
  143. // DateFromWeekNumAndDay
  144. // year - Year YYYY.
  145. // weekNum - From 0 to 4.
  146. // weekDay - Week day starts from Sunday - 0 to Saturday - 6.
  147. boost::gregorian::date DateFromWeekNumAndDay(boost::gregorian::date::year_type year, unsigned short weekNum, boost::date_time::weekdays weekDay)
  148. {
  149. boost::gregorian::date date(year, boost::gregorian::Apr, 1);
  150. boost::gregorian::date::day_of_week_type d = date.day_of_week();
  151.  
  152. date += boost::gregorian::date_duration(weekNum * 7) + boost::gregorian::date_duration(weekDay - d);
  153.  
  154. return date;
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement