Skip to content

Commit

Permalink
Update Doc Blocks to Discourage Use of Unix Timestamps (#2350)
Browse files Browse the repository at this point in the history
* Update Doc Blocks to Discourage Use of Unix Timestamps

This was suggested by issue #2347. Unix timestamps have clear disadvantages compared with the alternate methods of supplying date and time to PhpSpreadsheet - DateTime objects, Excel date time fields, and strings. In particular, Unix timestamp is not Y2038-safe on a 32-bit system, and it reflects the time in UTC, which may come as a surprise to the end-user (as it did in the cited issue). The alternate methods do not come with such baggage. This change updates some doc blocks to note that Unix timestamps are discoburage (N.B. - not deprecated). No executable code is changed.

* Document in Code As Well as Commmit Message

Per suggestion from @PowerKiKi.

* Missed One DocBlock

Including it now.

Co-authored-by: Adrien Crivelli <adrien.crivelli@gmail.com>
  • Loading branch information
oleibman and PowerKiKi committed Nov 4, 2021
1 parent da5c2b1 commit b1c9f0a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/PhpSpreadsheet/Calculation/Functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ class Functions
*/
const M_2DIVPI = 0.63661977236758134307553505349006;

/** constants */
const COMPATIBILITY_EXCEL = 'Excel';
const COMPATIBILITY_GNUMERIC = 'Gnumeric';
const COMPATIBILITY_OPENOFFICE = 'OpenOfficeCalc';

/** Use of RETURNDATE_PHP_NUMERIC is discouraged - not 32-bit Y2038-safe, no timezone. */
const RETURNDATE_PHP_NUMERIC = 'P';
/** Use of RETURNDATE_UNIX_TIMESTAMP is discouraged - not 32-bit Y2038-safe, no timezone. */
const RETURNDATE_UNIX_TIMESTAMP = 'P';
const RETURNDATE_PHP_OBJECT = 'O';
const RETURNDATE_PHP_DATETIME_OBJECT = 'O';
Expand Down
11 changes: 8 additions & 3 deletions src/PhpSpreadsheet/Shared/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ private static function validateTimeZone($timeZone)
* @param float|int $excelTimestamp MS Excel serialized date/time value
* @param null|DateTimeZone|string $timeZone The timezone to assume for the Excel timestamp,
* if you don't want to treat it as a UTC value
* Use the default (UST) unless you absolutely need a conversion
* Use the default (UTC) unless you absolutely need a conversion
*
* @return DateTime PHP date/time object
*/
Expand Down Expand Up @@ -206,11 +206,13 @@ public static function excelToDateTimeObject($excelTimestamp, $timeZone = null)

/**
* Convert a MS serialized datetime value from Excel to a unix timestamp.
* The use of Unix timestamps, and therefore this function, is discouraged.
* They are not Y2038-safe on a 32-bit system, and have no timezone info.
*
* @param float|int $excelTimestamp MS Excel serialized date/time value
* @param null|DateTimeZone|string $timeZone The timezone to assume for the Excel timestamp,
* if you don't want to treat it as a UTC value
* Use the default (UST) unless you absolutely need a conversion
* Use the default (UTC) unless you absolutely need a conversion
*
* @return int Unix timetamp for this date/time
*/
Expand All @@ -223,7 +225,8 @@ public static function excelToTimestamp($excelTimestamp, $timeZone = null)
/**
* Convert a date from PHP to an MS Excel serialized date/time value.
*
* @param mixed $dateValue Unix Timestamp or PHP DateTime object or a string
* @param mixed $dateValue PHP DateTime object or a string - Unix timestamp is also permitted, but discouraged;
* not Y2038-safe on a 32-bit system, and no timezone info
*
* @return bool|float Excel date/time value
* or boolean FALSE on failure
Expand Down Expand Up @@ -262,6 +265,8 @@ public static function dateTimeToExcel(DateTimeInterface $dateValue)

/**
* Convert a Unix timestamp to an MS Excel serialized date/time value.
* The use of Unix timestamps, and therefore this function, is discouraged.
* They are not Y2038-safe on a 32-bit system, and have no timezone info.
*
* @param int $unixTimestamp Unix Timestamp
*
Expand Down

0 comments on commit b1c9f0a

Please sign in to comment.