help.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*
  2. +----------------------------------------------------------------------+
  3. | XlsWriter Extension |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 2017-2018 The Viest |
  6. +----------------------------------------------------------------------+
  7. | http://www.viest.me |
  8. +----------------------------------------------------------------------+
  9. | Author: viest <[email protected]> |
  10. +----------------------------------------------------------------------+
  11. */
  12. #include "xlswriter.h"
  13. #include "ext/date/php_date.h"
  14. #include "ext/standard/php_math.h"
  15. zend_long date_double_to_timestamp(double value) {
  16. double days, partDay, hours, minutes, seconds;
  17. days = floor(value);
  18. partDay = value - days;
  19. hours = floor(partDay * 24);
  20. partDay = partDay * 24 - hours;
  21. minutes = floor(partDay * 60);
  22. partDay = partDay * 60 - minutes;
  23. seconds = _php_math_round(partDay * 60, 0, PHP_ROUND_HALF_UP);
  24. zval datetime;
  25. php_date_instantiate(php_date_get_date_ce(), &datetime);
  26. php_date_initialize(Z_PHPDATE_P(&datetime), ZEND_STRL("1899-12-30"), NULL, NULL, 1);
  27. zval _modify_args[1], _modify_result;
  28. smart_str _modify_arg_string = {0};
  29. if (days >= 0) {
  30. smart_str_appendl(&_modify_arg_string, "+", 1);
  31. }
  32. smart_str_append_long(&_modify_arg_string, days);
  33. smart_str_appendl(&_modify_arg_string, " days", 5);
  34. ZVAL_STR(&_modify_args[0], _modify_arg_string.s);
  35. call_object_method(&datetime, "modify", 1, _modify_args, &_modify_result);
  36. zval_ptr_dtor(&datetime);
  37. zval _set_time_args[3], _set_time_result;
  38. ZVAL_LONG(&_set_time_args[0], (zend_long)hours);
  39. ZVAL_LONG(&_set_time_args[1], (zend_long)minutes);
  40. ZVAL_LONG(&_set_time_args[2], (zend_long)seconds);
  41. call_object_method(&_modify_result, "setTime", 3, _set_time_args, &_set_time_result);
  42. zval_ptr_dtor(&_modify_result);
  43. zval _format_args[1], _format_result;
  44. ZVAL_STRING(&_format_args[0], "U");
  45. call_object_method(&_set_time_result, "format", 1, _format_args, &_format_result);
  46. zval_ptr_dtor(&_set_time_result);
  47. zend_long timestamp = ZEND_STRTOL(Z_STRVAL(_format_result), NULL ,10);
  48. zval_ptr_dtor(&_format_result);
  49. return timestamp;
  50. }