help.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. #if PHP_VERSION_ID >= 80100
  58. zend_string *zs_path = zend_string_init(path, strlen(path), 0);
  59. php_stat(zs_path, FS_IS_DIR, &dir_exists);
  60. zend_string_release(zs_path);
  61. #else
  62. php_stat(path, strlen(path), FS_IS_DIR, &dir_exists);
  63. #endif
  64. if (Z_TYPE(dir_exists) == IS_FALSE) {
  65. zval_ptr_dtor(&dir_exists);
  66. return XLSWRITER_FALSE;
  67. }
  68. zval_ptr_dtor(&dir_exists);
  69. return XLSWRITER_TRUE;
  70. }
  71. /* }}} */
  72. /* {{{ */
  73. unsigned int file_exists(const char *path) {
  74. zval file_exists;
  75. #if PHP_VERSION_ID >= 80100
  76. zend_string *zs_path = zend_string_init(path, strlen(path), 0);
  77. php_stat(zs_path, FS_IS_FILE, &file_exists);
  78. zend_string_release(zs_path);
  79. #else
  80. php_stat(path, strlen(path), FS_IS_FILE, &file_exists);
  81. #endif
  82. if (Z_TYPE(file_exists) == IS_FALSE) {
  83. zval_ptr_dtor(&file_exists);
  84. return XLSWRITER_FALSE;
  85. }
  86. zval_ptr_dtor(&file_exists);
  87. return XLSWRITER_TRUE;
  88. }
  89. /* }}} */