File tree Expand file tree Collapse file tree 5 files changed +75
-15
lines changed Expand file tree Collapse file tree 5 files changed +75
-15
lines changed Original file line number Diff line number Diff line change 2
2
3
3
namespace PhpOffice \PhpSpreadsheet \Cell ;
4
4
5
- use DateTime ;
6
5
use PhpOffice \PhpSpreadsheet \Calculation \Calculation ;
7
6
use PhpOffice \PhpSpreadsheet \Calculation \Information \ExcelError ;
8
7
use PhpOffice \PhpSpreadsheet \Collection \Cells ;
@@ -240,18 +239,7 @@ public function setValueExplicit($value, $dataType)
240
239
241
240
break ;
242
241
case DataType::TYPE_ISO_DATE :
243
- if (!is_string ($ value )) {
244
- throw new Exception ('Non-string supplied for datatype Date ' );
245
- }
246
- $ date = new DateTime ($ value );
247
- $ newValue = SharedDate::PHPToExcel ($ date );
248
- if ($ newValue === false ) {
249
- throw new Exception ("Invalid string $ value supplied for datatype Date " );
250
- }
251
- if (preg_match ('/^ \\d \\d: \\d \\d: \\d \\d/ ' , $ value ) == 1 ) {
252
- $ newValue = fmod ($ newValue , 1.0 );
253
- }
254
- $ this ->value = $ newValue ;
242
+ $ this ->value = SharedDate::convertIsoDate ($ value );
255
243
$ dataType = DataType::TYPE_NUMERIC ;
256
244
257
245
break ;
Original file line number Diff line number Diff line change 9
9
use PhpOffice \PhpSpreadsheet \Calculation \Functions ;
10
10
use PhpOffice \PhpSpreadsheet \Calculation \Information \ExcelError ;
11
11
use PhpOffice \PhpSpreadsheet \Cell \Cell ;
12
+ use PhpOffice \PhpSpreadsheet \Exception ;
12
13
use PhpOffice \PhpSpreadsheet \Exception as PhpSpreadsheetException ;
14
+ use PhpOffice \PhpSpreadsheet \Shared \Date as SharedDate ;
13
15
use PhpOffice \PhpSpreadsheet \Style \NumberFormat ;
14
16
15
17
class Date
@@ -158,6 +160,36 @@ private static function validateTimeZone($timeZone)
158
160
throw new PhpSpreadsheetException ('Invalid timezone ' );
159
161
}
160
162
163
+ /**
164
+ * @param mixed $value
165
+ *
166
+ * @return float|int
167
+ */
168
+ public static function convertIsoDate ($ value )
169
+ {
170
+ if (!is_string ($ value )) {
171
+ throw new Exception ('Non-string value supplied for Iso Date conversion ' );
172
+ }
173
+
174
+ $ date = new DateTime ($ value );
175
+ $ dateErrors = DateTime::getLastErrors ();
176
+
177
+ if (is_array ($ dateErrors ) && ($ dateErrors ['warning_count ' ] > 0 || $ dateErrors ['error_count ' ] > 0 )) {
178
+ throw new Exception ("Invalid string $ value supplied for datatype Date " );
179
+ }
180
+
181
+ $ newValue = SharedDate::PHPToExcel ($ date );
182
+ if ($ newValue === false ) {
183
+ throw new Exception ("Invalid string $ value supplied for datatype Date " );
184
+ }
185
+
186
+ if (preg_match ('/^ \\d \\d: \\d \\d: \\d \\d/ ' , $ value ) == 1 ) {
187
+ $ newValue = fmod ($ newValue , 1.0 );
188
+ }
189
+
190
+ return $ newValue ;
191
+ }
192
+
161
193
/**
162
194
* Convert a MS serialized datetime value from Excel to a PHP Date/Time object.
163
195
*
Original file line number Diff line number Diff line change 2
2
3
3
namespace PhpOffice \PhpSpreadsheetTests \Cell ;
4
4
5
+ use PhpOffice \PhpSpreadsheet \Cell \DataType ;
5
6
use PhpOffice \PhpSpreadsheet \Exception ;
6
7
use PhpOffice \PhpSpreadsheet \Spreadsheet ;
7
8
use PhpOffice \PhpSpreadsheet \Style \Color ;
@@ -33,6 +34,19 @@ public function providerSetValueExplicit(): array
33
34
return require 'tests/data/Cell/SetValueExplicit.php ' ;
34
35
}
35
36
37
+ public function testInvalidIsoDateSetValueExplicit (): void
38
+ {
39
+ $ spreadsheet = new Spreadsheet ();
40
+ $ cell = $ spreadsheet ->getActiveSheet ()->getCell ('A1 ' );
41
+
42
+ $ dateValue = '2022-02-29 ' ; // Invalid leap year
43
+ $ this ->expectException (Exception::class);
44
+ $ this ->expectExceptionMessage ("Invalid string {$ dateValue } supplied for datatype Date " );
45
+ $ cell ->setValueExplicit ($ dateValue , DataType::TYPE_ISO_DATE );
46
+
47
+ $ spreadsheet ->disconnectWorksheets ();
48
+ }
49
+
36
50
/**
37
51
* @dataProvider providerSetValueExplicitException
38
52
*
Original file line number Diff line number Diff line change 3
3
namespace PhpOffice \PhpSpreadsheetTests \Shared ;
4
4
5
5
use DateTimeZone ;
6
+ use PhpOffice \PhpSpreadsheet \Exception ;
6
7
use PhpOffice \PhpSpreadsheet \Shared \Date ;
7
8
use PhpOffice \PhpSpreadsheet \Style \NumberFormat ;
8
9
use PHPUnit \Framework \TestCase ;
@@ -209,6 +210,15 @@ public function providerDateTimeExcelToTimestamp1900Timezone(): array
209
210
return require 'tests/data/Shared/Date/ExcelToTimestamp1900Timezone.php ' ;
210
211
}
211
212
213
+ public function testConvertIsoDateError (): void
214
+ {
215
+ Date::setExcelCalendar (Date::CALENDAR_WINDOWS_1900 );
216
+
217
+ $ this ->expectException (Exception::class);
218
+ $ this ->expectExceptionMessage ('Non-string value supplied for Iso Date conversion ' );
219
+ Date::convertIsoDate (false );
220
+ }
221
+
212
222
public function testVarious (): void
213
223
{
214
224
Date::setDefaultTimeZone ('UTC ' );
Original file line number Diff line number Diff line change 1
1
<?php
2
2
3
+ use PhpOffice \PhpSpreadsheet \Calculation \Information \ExcelError ;
3
4
use PhpOffice \PhpSpreadsheet \Cell \DataType ;
4
5
5
6
return [
39
40
DataType::TYPE_NUMERIC ,
40
41
],
41
42
[
42
- '#DIV/0! ' ,
43
+ 44613.43090277778 ,
44
+ '2022-02-21 10:20:30 ' ,
45
+ DataType::TYPE_ISO_DATE ,
46
+ ],
47
+ [
48
+ 44613.0 ,
49
+ '2022-02-21 ' ,
50
+ DataType::TYPE_ISO_DATE ,
51
+ ],
52
+ [
53
+ -30879.0 ,
54
+ '1815-06-15 ' , // Dates outside the Excel Range should fail really
55
+ DataType::TYPE_ISO_DATE ,
56
+ ],
57
+ [
58
+ ExcelError::DIV0 (),
43
59
'#DIV/0! ' ,
44
60
DataType::TYPE_ERROR ,
45
61
],
46
62
[
47
- ' #NULL! ' ,
63
+ ExcelError:: null () ,
48
64
'NOT A VALID ERROR TYPE VALUE ' ,
49
65
DataType::TYPE_ERROR ,
50
66
],
You can’t perform that action at this time.
0 commit comments