easter.c 4.3 KB

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