cal_unix.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. #define SECS_PER_DAY (24 * 3600)
  25. /* {{{ proto int unixtojd([int timestamp])
  26. Convert UNIX timestamp to Julian Day */
  27. PHP_FUNCTION(unixtojd)
  28. {
  29. time_t ts = 0;
  30. zend_long tl = 0;
  31. struct tm *ta, tmbuf;
  32. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|l", &tl) == FAILURE) {
  33. return;
  34. }
  35. if (!tl) {
  36. ts = time(NULL);
  37. } else if (tl >= 0) {
  38. ts = (time_t) tl;
  39. } else {
  40. RETURN_FALSE;
  41. }
  42. if (!(ta = php_localtime_r(&ts, &tmbuf))) {
  43. RETURN_FALSE;
  44. }
  45. RETURN_LONG(GregorianToSdn(ta->tm_year+1900, ta->tm_mon+1, ta->tm_mday));
  46. }
  47. /* }}} */
  48. /* {{{ proto int jdtounix(int jday)
  49. Convert Julian Day to UNIX timestamp */
  50. PHP_FUNCTION(jdtounix)
  51. {
  52. zend_long uday;
  53. if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &uday) == FAILURE) {
  54. return;
  55. }
  56. uday -= 2440588 /* J.D. of 1.1.1970 */;
  57. if (uday < 0 || uday > ZEND_LONG_MAX / SECS_PER_DAY) { /* before beginning of unix epoch or greater than representable */
  58. RETURN_FALSE;
  59. }
  60. RETURN_LONG(uday * SECS_PER_DAY);
  61. }
  62. /* }}} */
  63. /*
  64. * Local variables:
  65. * tab-width: 4
  66. * c-basic-offset: 4
  67. * End:
  68. * vim600: sw=4 ts=4 fdm=marker
  69. * vim<600: sw=4 ts=4
  70. */