cal_unix.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. #define SECS_PER_DAY (24 * 3600)
  23. /* {{{ Convert UNIX timestamp to Julian Day */
  24. PHP_FUNCTION(unixtojd)
  25. {
  26. time_t ts;
  27. zend_long tl = 0;
  28. bool tl_is_null = 1;
  29. struct tm *ta, tmbuf;
  30. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|l!", &tl, &tl_is_null) == FAILURE) {
  31. RETURN_THROWS();
  32. }
  33. if (tl_is_null) {
  34. ts = time(NULL);
  35. } else if (tl >= 0) {
  36. ts = (time_t) tl;
  37. } else {
  38. zend_argument_value_error(1, "must be greater than or equal to 0");
  39. RETURN_THROWS();
  40. }
  41. if (!(ta = php_localtime_r(&ts, &tmbuf))) {
  42. RETURN_FALSE;
  43. }
  44. RETURN_LONG(GregorianToSdn(ta->tm_year+1900, ta->tm_mon+1, ta->tm_mday));
  45. }
  46. /* }}} */
  47. /* {{{ Convert Julian Day to UNIX timestamp */
  48. PHP_FUNCTION(jdtounix)
  49. {
  50. zend_long uday;
  51. if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &uday) == FAILURE) {
  52. RETURN_THROWS();
  53. }
  54. uday -= 2440588 /* J.D. of 1.1.1970 */;
  55. if (uday < 0 || uday > ZEND_LONG_MAX / SECS_PER_DAY) { /* before beginning of unix epoch or greater than representable */
  56. zend_value_error("jday must be between 2440588 and " ZEND_LONG_FMT, ZEND_LONG_MAX / SECS_PER_DAY + 2440588);
  57. RETURN_THROWS();
  58. }
  59. RETURN_LONG(uday * SECS_PER_DAY);
  60. }
  61. /* }}} */