help.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. ZSTR_VAL(_modify_arg_string.s)[ZSTR_LEN(_modify_arg_string.s)] = '\0';
  37. ZVAL_STR(&_modify_args[0], _modify_arg_string.s);
  38. call_object_method(&datetime, "modify", 1, _modify_args, &_modify_result);
  39. zval_ptr_dtor(&datetime);
  40. zval _set_time_args[3], _set_time_result;
  41. ZVAL_LONG(&_set_time_args[0], (zend_long)hours);
  42. ZVAL_LONG(&_set_time_args[1], (zend_long)minutes);
  43. ZVAL_LONG(&_set_time_args[2], (zend_long)seconds);
  44. call_object_method(&_modify_result, "setTime", 3, _set_time_args, &_set_time_result);
  45. zval_ptr_dtor(&_modify_result);
  46. zval _format_args[1], _format_result;
  47. ZVAL_STRING(&_format_args[0], "U");
  48. call_object_method(&_set_time_result, "format", 1, _format_args, &_format_result);
  49. zval_ptr_dtor(&_set_time_result);
  50. zend_long timestamp = ZEND_STRTOL(Z_STRVAL(_format_result), NULL ,10);
  51. zval_ptr_dtor(&_format_result);
  52. return timestamp;
  53. }
  54. /* }}} */
  55. /* {{{ */
  56. unsigned int directory_exists(const char *path) {
  57. zval dir_exists;
  58. #if PHP_VERSION_ID >= 80100
  59. zend_string *zs_path = zend_string_init(path, strlen(path), 0);
  60. php_stat(zs_path, FS_IS_DIR, &dir_exists);
  61. zend_string_release(zs_path);
  62. #else
  63. php_stat(path, strlen(path), FS_IS_DIR, &dir_exists);
  64. #endif
  65. if (Z_TYPE(dir_exists) == IS_FALSE) {
  66. zval_ptr_dtor(&dir_exists);
  67. return XLSWRITER_FALSE;
  68. }
  69. zval_ptr_dtor(&dir_exists);
  70. return XLSWRITER_TRUE;
  71. }
  72. /* }}} */
  73. /* {{{ */
  74. unsigned int file_exists(const char *path) {
  75. zval file_exists;
  76. #if PHP_VERSION_ID >= 80100
  77. zend_string *zs_path = zend_string_init(path, strlen(path), 0);
  78. php_stat(zs_path, FS_IS_FILE, &file_exists);
  79. zend_string_release(zs_path);
  80. #else
  81. php_stat(path, strlen(path), FS_IS_FILE, &file_exists);
  82. #endif
  83. if (Z_TYPE(file_exists) == IS_FALSE) {
  84. zval_ptr_dtor(&file_exists);
  85. return XLSWRITER_FALSE;
  86. }
  87. zval_ptr_dtor(&file_exists);
  88. return XLSWRITER_TRUE;
  89. }
  90. /* }}} */