easter.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Copyright (c) The PHP Group |
  4. +----------------------------------------------------------------------+
  5. | This source file is subject to version 3.01 of the PHP license, |
  6. | that is bundled with this package in the file LICENSE, and is |
  7. | available through the world-wide-web at the following url: |
  8. | https://www.php.net/license/3_01.txt |
  9. | If you did not receive a copy of the PHP license and are unable to |
  10. | obtain it through the world-wide-web, please send a note to |
  11. | license@php.net so we can mail you a copy immediately. |
  12. +----------------------------------------------------------------------+
  13. | Authors: Shane Caraveo <shane@caraveo.com> |
  14. | Colin Viebrock <colin@easydns.com> |
  15. | Hartmut Holzgraefe <hholzgra@php.net> |
  16. +----------------------------------------------------------------------+
  17. */
  18. #include "php.h"
  19. #include "php_calendar.h"
  20. #include "sdncal.h"
  21. #include <time.h>
  22. static void _cal_easter(INTERNAL_FUNCTION_PARAMETERS, zend_long gm)
  23. {
  24. /* based on code by Simon Kershaw, <webmaster@ely.anglican.org> */
  25. struct tm te;
  26. zend_long year, golden, solar, lunar, pfm, dom, tmp, easter, result;
  27. zend_long method = CAL_EASTER_DEFAULT;
  28. bool year_is_null = 1;
  29. if (zend_parse_parameters(ZEND_NUM_ARGS(),
  30. "|l!l", &year, &year_is_null, &method) == FAILURE) {
  31. RETURN_THROWS();
  32. }
  33. /* Default to the current year if year parameter is not given */
  34. if (year_is_null) {
  35. time_t a;
  36. struct tm b, *res;
  37. time(&a);
  38. res = php_localtime_r(&a, &b);
  39. if (!res) {
  40. year = 1900;
  41. } else {
  42. year = 1900 + b.tm_year;
  43. }
  44. }
  45. if (gm && (year<1970 || year>2037)) { /* out of range for timestamps */
  46. zend_argument_value_error(1, "must be between 1970 and 2037 (inclusive)");
  47. RETURN_THROWS();
  48. }
  49. golden = (year % 19) + 1; /* the Golden number */
  50. if ((year <= 1582 && method != CAL_EASTER_ALWAYS_GREGORIAN) ||
  51. (year >= 1583 && year <= 1752 && method != CAL_EASTER_ROMAN && method != CAL_EASTER_ALWAYS_GREGORIAN) ||
  52. method == CAL_EASTER_ALWAYS_JULIAN) { /* JULIAN CALENDAR */
  53. dom = (year + (year/4) + 5) % 7; /* the "Dominical number" - finding a Sunday */
  54. if (dom < 0) {
  55. dom += 7;
  56. }
  57. pfm = (3 - (11*golden) - 7) % 30; /* uncorrected date of the Paschal full moon */
  58. if (pfm < 0) {
  59. pfm += 30;
  60. }
  61. } else { /* GREGORIAN CALENDAR */
  62. dom = (year + (year/4) - (year/100) + (year/400)) % 7; /* the "Domincal number" */
  63. if (dom < 0) {
  64. dom += 7;
  65. }
  66. solar = (year-1600)/100 - (year-1600)/400; /* the solar and lunar corrections */
  67. lunar = (((year-1400) / 100) * 8) / 25;
  68. pfm = (3 - (11*golden) + solar - lunar) % 30; /* uncorrected date of the Paschal full moon */
  69. if (pfm < 0) {
  70. pfm += 30;
  71. }
  72. }
  73. if ((pfm == 29) || (pfm == 28 && golden > 11)) { /* corrected date of the Paschal full moon */
  74. pfm--; /* - days after 21st March */
  75. }
  76. tmp = (4-pfm-dom) % 7;
  77. if (tmp < 0) {
  78. tmp += 7;
  79. }
  80. easter = pfm + tmp + 1; /* Easter as the number of days after 21st March */
  81. if (gm) { /* return a timestamp */
  82. te.tm_isdst = -1;
  83. te.tm_year = year-1900;
  84. te.tm_sec = 0;
  85. te.tm_min = 0;
  86. te.tm_hour = 0;
  87. if (easter < 11) {
  88. te.tm_mon = 2; /* March */
  89. te.tm_mday = easter+21;
  90. } else {
  91. te.tm_mon = 3; /* April */
  92. te.tm_mday = easter-10;
  93. }
  94. result = mktime(&te);
  95. } else { /* return the days after March 21 */
  96. result = easter;
  97. }
  98. ZVAL_LONG(return_value, result);
  99. }
  100. /* {{{ Return the timestamp of midnight on Easter of a given year (defaults to current year) */
  101. PHP_FUNCTION(easter_date)
  102. {
  103. _cal_easter(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
  104. }
  105. /* }}} */
  106. /* {{{ Return the number of days after March 21 that Easter falls on for a given year (defaults to current year) */
  107. PHP_FUNCTION(easter_days)
  108. {
  109. _cal_easter(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
  110. }
  111. /* }}} */