Skip to content

Commit 554ddc0

Browse files
authored
Merge pull request #263 from viest/dev
Feat: timestampFromDateDouble
2 parents b09213d + b4f16a2 commit 554ddc0

File tree

8 files changed

+116
-42
lines changed

8 files changed

+116
-42
lines changed

config.m4

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ if test "$PHP_XLSWRITER" != "no"; then
2020
kernel/write.c \
2121
kernel/format.c \
2222
kernel/chart.c \
23+
kernel/help.c \
2324
"
2425

2526
xls_read_sources="

config.w32

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ if (PHP_XLSWRITER != "no") {
2727
chart.c \
2828
read.c \
2929
csv.c \
30+
help.c \
3031
", "xlswriter");
3132

3233
ADD_SOURCES(configure_module_dirname + "\\library\\libxlsxwriter\\third_party\\minizip", "\

include/help.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
13+
#ifndef PHP_EXT_XLS_EXPORT_HELP_H
14+
#define PHP_EXT_XLS_EXPORT_HELP_H
15+
16+
zend_long date_double_to_timestamp(double value);
17+
18+
#endif //PHP_EXT_XLS_EXPORT_HELP_H

include/xlswriter.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "exception.h"
3535
#include "format.h"
3636
#include "chart.h"
37+
#include "help.h"
3738

3839
#ifdef ENABLE_READER
3940
#include "xlsxio_read.h"

kernel/excel.c

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -948,6 +948,24 @@ PHP_METHOD(vtiful_xls, stringFromColumnIndex)
948948
}
949949
/* }}} */
950950

951+
/** {{{ \Vtiful\Kernel\Excel::timestampFromDateDouble(string $date)
952+
*/
953+
PHP_METHOD(vtiful_xls, timestampFromDateDouble)
954+
{
955+
double date = 0;
956+
957+
ZEND_PARSE_PARAMETERS_START(1, 1)
958+
Z_PARAM_DOUBLE(date)
959+
ZEND_PARSE_PARAMETERS_END();
960+
961+
if (date <= 0) {
962+
RETURN_LONG(0);
963+
}
964+
965+
RETURN_LONG(date_double_to_timestamp(date));
966+
}
967+
/* }}} */
968+
951969
/** {{{ \Vtiful\Kernel\Excel::gridline(int $option = \Vtiful\Kernel\Excel::GRIDLINES_SHOW_ALL)
952970
*/
953971
PHP_METHOD(vtiful_xls, gridline)
@@ -1278,8 +1296,9 @@ zend_function_entry xls_methods[] = {
12781296
PHP_ME(vtiful_xls, zoom, xls_sheet_zoom_arginfo, ZEND_ACC_PUBLIC)
12791297
PHP_ME(vtiful_xls, gridline, xls_sheet_gridline_arginfo, ZEND_ACC_PUBLIC)
12801298

1281-
PHP_ME(vtiful_xls, columnIndexFromString, xls_index_to_string, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
1282-
PHP_ME(vtiful_xls, stringFromColumnIndex, xls_string_to_index, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
1299+
PHP_ME(vtiful_xls, columnIndexFromString, xls_index_to_string, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
1300+
PHP_ME(vtiful_xls, stringFromColumnIndex, xls_string_to_index, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
1301+
PHP_ME(vtiful_xls, timestampFromDateDouble, xls_string_to_index, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
12831302

12841303
#ifdef ENABLE_READER
12851304
PHP_ME(vtiful_xls, openFile, xls_open_file_arginfo, ZEND_ACC_PUBLIC)

kernel/help.c

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
13+
#include "xlswriter.h"
14+
#include "ext/date/php_date.h"
15+
#include "ext/standard/php_math.h"
16+
17+
zend_long date_double_to_timestamp(double value) {
18+
double days, partDay, hours, minutes, seconds;
19+
20+
days = floor(value);
21+
partDay = value - days;
22+
hours = floor(partDay * 24);
23+
partDay = partDay * 24 - hours;
24+
minutes = floor(partDay * 60);
25+
partDay = partDay * 60 - minutes;
26+
seconds = _php_math_round(partDay * 60, 0, PHP_ROUND_HALF_UP);
27+
28+
zval datetime;
29+
php_date_instantiate(php_date_get_date_ce(), &datetime);
30+
php_date_initialize(Z_PHPDATE_P(&datetime), ZEND_STRL("1899-12-30"), NULL, NULL, 1);
31+
32+
zval _modify_args[1], _modify_result;
33+
smart_str _modify_arg_string = {0};
34+
if (days >= 0) {
35+
smart_str_appendl(&_modify_arg_string, "+", 1);
36+
}
37+
smart_str_append_long(&_modify_arg_string, days);
38+
smart_str_appendl(&_modify_arg_string, " days", 5);
39+
ZVAL_STR(&_modify_args[0], _modify_arg_string.s);
40+
call_object_method(&datetime, "modify", 1, _modify_args, &_modify_result);
41+
zval_ptr_dtor(&datetime);
42+
43+
zval _set_time_args[3], _set_time_result;
44+
ZVAL_LONG(&_set_time_args[0], (zend_long)hours);
45+
ZVAL_LONG(&_set_time_args[1], (zend_long)minutes);
46+
ZVAL_LONG(&_set_time_args[2], (zend_long)seconds);
47+
call_object_method(&_modify_result, "setTime", 3, _set_time_args, &_set_time_result);
48+
zval_ptr_dtor(&_modify_result);
49+
50+
zval _format_args[1], _format_result;
51+
ZVAL_STRING(&_format_args[0], "U");
52+
call_object_method(&_set_time_result, "format", 1, _format_args, &_format_result);
53+
zval_ptr_dtor(&_set_time_result);
54+
55+
zend_long timestamp = ZEND_STRTOL(Z_STRVAL(_format_result), NULL ,10);
56+
zval_ptr_dtor(&_format_result);
57+
58+
return timestamp;
59+
}

kernel/read.c

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -107,46 +107,7 @@ void data_to_custom_type(const char *string_value, const size_t string_value_len
107107
return;
108108
}
109109

110-
double value = zend_strtod(string_value, NULL);
111-
double days, partDay, hours, minutes, seconds;
112-
113-
days = floor(value);
114-
partDay = value - days;
115-
hours = floor(partDay * 24);
116-
partDay = partDay * 24 - hours;
117-
minutes = floor(partDay * 60);
118-
partDay = partDay * 60 - minutes;
119-
seconds = _php_math_round(partDay * 60, 0, PHP_ROUND_HALF_UP);
120-
121-
zval datetime;
122-
php_date_instantiate(php_date_get_date_ce(), &datetime);
123-
php_date_initialize(Z_PHPDATE_P(&datetime), ZEND_STRL("1899-12-30"), NULL, NULL, 1);
124-
125-
zval _modify_args[1], _modify_result;
126-
smart_str _modify_arg_string = {0};
127-
if (days >= 0) {
128-
smart_str_appendl(&_modify_arg_string, "+", 1);
129-
}
130-
smart_str_append_long(&_modify_arg_string, days);
131-
smart_str_appendl(&_modify_arg_string, " days", 5);
132-
ZVAL_STR(&_modify_args[0], _modify_arg_string.s);
133-
call_object_method(&datetime, "modify", 1, _modify_args, &_modify_result);
134-
zval_ptr_dtor(&datetime);
135-
136-
zval _set_time_args[3], _set_time_result;
137-
ZVAL_LONG(&_set_time_args[0], (zend_long)hours);
138-
ZVAL_LONG(&_set_time_args[1], (zend_long)minutes);
139-
ZVAL_LONG(&_set_time_args[2], (zend_long)seconds);
140-
call_object_method(&_modify_result, "setTime", 3, _set_time_args, &_set_time_result);
141-
zval_ptr_dtor(&_modify_result);
142-
143-
zval _format_args[1], _format_result;
144-
ZVAL_STRING(&_format_args[0], "U");
145-
call_object_method(&_set_time_result, "format", 1, _format_args, &_format_result);
146-
zval_ptr_dtor(&_set_time_result);
147-
148-
zend_long timestamp = ZEND_STRTOL(Z_STRVAL(_format_result), NULL ,10);
149-
zval_ptr_dtor(&_format_result);
110+
zend_long timestamp = date_double_to_timestamp(zend_strtod(string_value, NULL));
150111

151112
// GMT
152113
// if (value != 0) {
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
--TEST--
2+
Check for vtiful presence
3+
--SKIPIF--
4+
<?php if (!extension_loaded("xlswriter")) print "skip"; ?>
5+
--FILE--
6+
<?php
7+
var_dump(\Vtiful\Kernel\Excel::timestampFromDateDouble(43727.306782407));
8+
var_dump(\Vtiful\Kernel\Excel::timestampFromDateDouble(NULL));
9+
var_dump(\Vtiful\Kernel\Excel::timestampFromDateDouble(43727));
10+
?>
11+
--EXPECT--
12+
int(1568877706)
13+
int(0)
14+
int(1568851200)

0 commit comments

Comments
 (0)