help.c 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. #include "ext/standard/php_filestat.h"
  16. /* {{{ */
  17. zend_long date_double_to_timestamp(double value) {
  18. double days, partDay, hours, minutes, seconds;
  19. days = floor(value);
  20. partDay = value - days;
  21. hours = floor(partDay * 24);
  22. partDay = partDay * 24 - hours;
  23. minutes = floor(partDay * 60);
  24. partDay = partDay * 60 - minutes;
  25. seconds = _php_math_round(partDay * 60, 0, PHP_ROUND_HALF_UP);
  26. zval datetime;
  27. php_date_instantiate(php_date_get_date_ce(), &datetime);
  28. php_date_initialize(Z_PHPDATE_P(&datetime), ZEND_STRL("1899-12-30"), NULL, NULL, 1);
  29. zval _modify_args[1], _modify_result;
  30. smart_str _modify_arg_string = {0};
  31. if (days >= 0) {
  32. smart_str_appendl(&_modify_arg_string, "+", 1);
  33. }
  34. smart_str_append_long(&_modify_arg_string, days);
  35. smart_str_appendl(&_modify_arg_string, " days", 5);
  36. ZVAL_STR(&_modify_args[0], _modify_arg_string.s);
  37. call_object_method(&datetime, "modify", 1, _modify_args, &_modify_result);
  38. zval_ptr_dtor(&datetime);
  39. zval _set_time_args[3], _set_time_result;
  40. ZVAL_LONG(&_set_time_args[0], (zend_long)hours);
  41. ZVAL_LONG(&_set_time_args[1], (zend_long)minutes);
  42. ZVAL_LONG(&_set_time_args[2], (zend_long)seconds);
  43. call_object_method(&_modify_result, "setTime", 3, _set_time_args, &_set_time_result);
  44. zval_ptr_dtor(&_modify_result);
  45. zval _format_args[1], _format_result;
  46. ZVAL_STRING(&_format_args[0], "U");
  47. call_object_method(&_set_time_result, "format", 1, _format_args, &_format_result);
  48. zval_ptr_dtor(&_set_time_result);
  49. zend_long timestamp = ZEND_STRTOL(Z_STRVAL(_format_result), NULL ,10);
  50. zval_ptr_dtor(&_format_result);
  51. return timestamp;
  52. }
  53. /* }}} */
  54. /* {{{ */
  55. unsigned int directory_exists(const char *path) {
  56. zval dir_exists;
  57. php_stat(path, strlen(path), FS_IS_DIR, &dir_exists);
  58. if (Z_TYPE(dir_exists) == IS_FALSE) {
  59. zval_ptr_dtor(&dir_exists);
  60. return XLSWRITER_FALSE;
  61. }
  62. zval_ptr_dtor(&dir_exists);
  63. return XLSWRITER_TRUE;
  64. }
  65. /* }}} */
  66. /* {{{ */
  67. unsigned int file_exists(const char *path) {
  68. zval file_exists;
  69. php_stat(path, strlen(path), FS_IS_FILE, &file_exists);
  70. if (Z_TYPE(file_exists) == IS_FALSE) {
  71. zval_ptr_dtor(&file_exists);
  72. return XLSWRITER_FALSE;
  73. }
  74. zval_ptr_dtor(&file_exists);
  75. return XLSWRITER_TRUE;
  76. }
  77. /* }}} */