浏览代码

Feat: timestampFromDateDouble

viest 5 年之前
父节点
当前提交
53b570392a
共有 8 个文件被更改,包括 116 次插入42 次删除
  1. 1 0
      config.m4
  2. 1 0
      config.w32
  3. 18 0
      include/help.h
  4. 1 0
      include/xlswriter.h
  5. 21 2
      kernel/excel.c
  6. 59 0
      kernel/help.c
  7. 1 40
      kernel/read.c
  8. 14 0
      tests/timestamp_from_date_double.phpt

+ 1 - 0
config.m4

@@ -20,6 +20,7 @@ if test "$PHP_XLSWRITER" != "no"; then
     kernel/write.c \
     kernel/format.c \
     kernel/chart.c \
+    kernel/help.c \
     "
 
     xls_read_sources="

+ 1 - 0
config.w32

@@ -27,6 +27,7 @@ if (PHP_XLSWRITER != "no") {
                 chart.c \
                 read.c \
                 csv.c \
+                help.c \
                 ", "xlswriter");
 
         ADD_SOURCES(configure_module_dirname + "\\library\\libxlsxwriter\\third_party\\minizip", "\

+ 18 - 0
include/help.h

@@ -0,0 +1,18 @@
+/*
+  +----------------------------------------------------------------------+
+  | XlsWriter Extension                                                  |
+  +----------------------------------------------------------------------+
+  | Copyright (c) 2017-2018 The Viest                                    |
+  +----------------------------------------------------------------------+
+  | http://www.viest.me                                                  |
+  +----------------------------------------------------------------------+
+  | Author: viest <[email protected]>                                 |
+  +----------------------------------------------------------------------+
+*/
+
+#ifndef PHP_EXT_XLS_EXPORT_HELP_H
+#define PHP_EXT_XLS_EXPORT_HELP_H
+
+zend_long date_double_to_timestamp(double value);
+
+#endif //PHP_EXT_XLS_EXPORT_HELP_H

+ 1 - 0
include/xlswriter.h

@@ -34,6 +34,7 @@
 #include "exception.h"
 #include "format.h"
 #include "chart.h"
+#include "help.h"
 
 #ifdef ENABLE_READER
 #include "xlsxio_read.h"

+ 21 - 2
kernel/excel.c

@@ -948,6 +948,24 @@ PHP_METHOD(vtiful_xls, stringFromColumnIndex)
 }
 /* }}} */
 
+/** {{{ \Vtiful\Kernel\Excel::timestampFromDateDouble(string $date)
+ */
+PHP_METHOD(vtiful_xls, timestampFromDateDouble)
+{
+    double date = 0;
+
+    ZEND_PARSE_PARAMETERS_START(1, 1)
+            Z_PARAM_DOUBLE(date)
+    ZEND_PARSE_PARAMETERS_END();
+
+    if (date <= 0) {
+        RETURN_LONG(0);
+    }
+
+    RETURN_LONG(date_double_to_timestamp(date));
+}
+/* }}} */
+
 /** {{{ \Vtiful\Kernel\Excel::gridline(int $option = \Vtiful\Kernel\Excel::GRIDLINES_SHOW_ALL)
  */
 PHP_METHOD(vtiful_xls, gridline)
@@ -1278,8 +1296,9 @@ zend_function_entry xls_methods[] = {
         PHP_ME(vtiful_xls, zoom,          xls_sheet_zoom_arginfo,     ZEND_ACC_PUBLIC)
         PHP_ME(vtiful_xls, gridline,      xls_sheet_gridline_arginfo, ZEND_ACC_PUBLIC)
 
-        PHP_ME(vtiful_xls, columnIndexFromString,  xls_index_to_string, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
-        PHP_ME(vtiful_xls, stringFromColumnIndex,  xls_string_to_index, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
+        PHP_ME(vtiful_xls, columnIndexFromString,   xls_index_to_string, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
+        PHP_ME(vtiful_xls, stringFromColumnIndex,   xls_string_to_index, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
+        PHP_ME(vtiful_xls, timestampFromDateDouble, xls_string_to_index, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
 
 #ifdef ENABLE_READER
         PHP_ME(vtiful_xls, openFile,         xls_open_file_arginfo,          ZEND_ACC_PUBLIC)

+ 59 - 0
kernel/help.c

@@ -0,0 +1,59 @@
+/*
+  +----------------------------------------------------------------------+
+  | XlsWriter Extension                                                  |
+  +----------------------------------------------------------------------+
+  | Copyright (c) 2017-2018 The Viest                                    |
+  +----------------------------------------------------------------------+
+  | http://www.viest.me                                                  |
+  +----------------------------------------------------------------------+
+  | Author: viest <[email protected]>                                 |
+  +----------------------------------------------------------------------+
+*/
+
+#include "xlswriter.h"
+#include "ext/date/php_date.h"
+#include "ext/standard/php_math.h"
+
+zend_long date_double_to_timestamp(double value) {
+    double days, partDay, hours, minutes, seconds;
+
+    days    = floor(value);
+    partDay = value - days;
+    hours   = floor(partDay * 24);
+    partDay = partDay * 24 - hours;
+    minutes = floor(partDay * 60);
+    partDay = partDay * 60 - minutes;
+    seconds = _php_math_round(partDay * 60, 0, PHP_ROUND_HALF_UP);
+
+    zval datetime;
+    php_date_instantiate(php_date_get_date_ce(), &datetime);
+    php_date_initialize(Z_PHPDATE_P(&datetime), ZEND_STRL("1899-12-30"), NULL, NULL, 1);
+
+    zval _modify_args[1], _modify_result;
+    smart_str _modify_arg_string = {0};
+    if (days >= 0) {
+        smart_str_appendl(&_modify_arg_string, "+", 1);
+    }
+    smart_str_append_long(&_modify_arg_string, days);
+    smart_str_appendl(&_modify_arg_string, " days", 5);
+    ZVAL_STR(&_modify_args[0], _modify_arg_string.s);
+    call_object_method(&datetime, "modify", 1, _modify_args, &_modify_result);
+    zval_ptr_dtor(&datetime);
+
+    zval _set_time_args[3], _set_time_result;
+    ZVAL_LONG(&_set_time_args[0], (zend_long)hours);
+    ZVAL_LONG(&_set_time_args[1], (zend_long)minutes);
+    ZVAL_LONG(&_set_time_args[2], (zend_long)seconds);
+    call_object_method(&_modify_result, "setTime", 3, _set_time_args, &_set_time_result);
+    zval_ptr_dtor(&_modify_result);
+
+    zval _format_args[1], _format_result;
+    ZVAL_STRING(&_format_args[0], "U");
+    call_object_method(&_set_time_result, "format", 1, _format_args, &_format_result);
+    zval_ptr_dtor(&_set_time_result);
+
+    zend_long timestamp = ZEND_STRTOL(Z_STRVAL(_format_result), NULL ,10);
+    zval_ptr_dtor(&_format_result);
+
+    return timestamp;
+}

+ 1 - 40
kernel/read.c

@@ -107,46 +107,7 @@ void data_to_custom_type(const char *string_value, const size_t string_value_len
             return;
         }
 
-        double value = zend_strtod(string_value, NULL);
-        double days, partDay, hours, minutes, seconds;
-
-        days    = floor(value);
-        partDay = value - days;
-        hours   = floor(partDay * 24);
-        partDay = partDay * 24 - hours;
-        minutes = floor(partDay * 60);
-        partDay = partDay * 60 - minutes;
-        seconds = _php_math_round(partDay * 60, 0, PHP_ROUND_HALF_UP);
-
-        zval datetime;
-        php_date_instantiate(php_date_get_date_ce(), &datetime);
-        php_date_initialize(Z_PHPDATE_P(&datetime), ZEND_STRL("1899-12-30"), NULL, NULL, 1);
-
-        zval _modify_args[1], _modify_result;
-        smart_str _modify_arg_string = {0};
-        if (days >= 0) {
-            smart_str_appendl(&_modify_arg_string, "+", 1);
-        }
-        smart_str_append_long(&_modify_arg_string, days);
-        smart_str_appendl(&_modify_arg_string, " days", 5);
-        ZVAL_STR(&_modify_args[0], _modify_arg_string.s);
-        call_object_method(&datetime, "modify", 1, _modify_args, &_modify_result);
-        zval_ptr_dtor(&datetime);
-
-        zval _set_time_args[3], _set_time_result;
-        ZVAL_LONG(&_set_time_args[0], (zend_long)hours);
-        ZVAL_LONG(&_set_time_args[1], (zend_long)minutes);
-        ZVAL_LONG(&_set_time_args[2], (zend_long)seconds);
-        call_object_method(&_modify_result, "setTime", 3, _set_time_args, &_set_time_result);
-        zval_ptr_dtor(&_modify_result);
-
-        zval _format_args[1], _format_result;
-        ZVAL_STRING(&_format_args[0], "U");
-        call_object_method(&_set_time_result, "format", 1, _format_args, &_format_result);
-        zval_ptr_dtor(&_set_time_result);
-
-        zend_long timestamp = ZEND_STRTOL(Z_STRVAL(_format_result), NULL ,10);
-        zval_ptr_dtor(&_format_result);
+        zend_long timestamp = date_double_to_timestamp(zend_strtod(string_value, NULL));
 
         // GMT
         // if (value != 0) {

+ 14 - 0
tests/timestamp_from_date_double.phpt

@@ -0,0 +1,14 @@
+--TEST--
+Check for vtiful presence
+--SKIPIF--
+<?php if (!extension_loaded("xlswriter")) print "skip"; ?>
+--FILE--
+<?php
+var_dump(\Vtiful\Kernel\Excel::timestampFromDateDouble(43727.306782407));
+var_dump(\Vtiful\Kernel\Excel::timestampFromDateDouble(NULL));
+var_dump(\Vtiful\Kernel\Excel::timestampFromDateDouble(4321432143));
+?>
+--EXPECT--
+int(1568877706)
+int(0)
+int(373369527993600)