Skip to content

Commit

Permalink
PHP 7 support / Now new DateTime() always returns DateTime instance
Browse files Browse the repository at this point in the history
  • Loading branch information
hnw committed Apr 15, 2016
1 parent 05a531a commit 963fab8
Show file tree
Hide file tree
Showing 15 changed files with 1,599 additions and 225 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ php:
- 5.4
- 5.5
- 5.6
- 7.0

env:
- REPORT_EXIT_STATUS=1 NO_INTERACTION=1 TESTS="--show-all"
Expand All @@ -14,4 +15,4 @@ before_script:
- phpize && ./configure && make

script:
- make test | tee test-output.txt && grep 'TEST SUMMARY$' test-output.txt > /dev/null ; test $? '!=' 0
- make test | tee tests-output.txt && grep 'TEST SUMMARY$' tests-output.txt > /dev/null ; test $? -ne 0
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ extension=timecop.so
## SYSTEM REQUIREMENTS

- OS: Linux, FreeBSD, MacOSX
- PHP: 5.2.x, 5.3.x, 5.4.x, 5.5.x
- Tested only on 5.2.17, 5.3.21, 5.4.11, and 5.5.5
- PHP: 5.3.x, 5.4.x, 5.5.x, 5.6.x, 7.0.x
- SAPI: Apache, CLI
- Other SAPIs are not tested, but there is no SAPI-dependent code.
- non-ZTS(recommended), ZTS
Expand Down Expand Up @@ -86,7 +85,12 @@ var_dump($new_time == time()); // bool(false)

## CHANGELOG

###version 1.0.6, 2016/04/13
###version 1.1.0(alpha), 2016/04/15
- Support PHP 7.0.x
- Now `new DateTime()` always returns `DateTime` instance
- The previous version returns `TimecopDateTime` instance when `timecop.func_override=1`.

###version 1.0.6, 2016/04/15
- Fix #10 (Timecop segfaults when set_error_handler throws an exception)

###version 1.0.5, 2013/11/26
Expand Down Expand Up @@ -127,7 +131,7 @@ var_dump($new_time == time()); // bool(false)
#
The MIT License

Copyright (c) 2012-2013 Yoshio HANAWA
Copyright (c) 2012-2016 Yoshio HANAWA

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

Expand Down
66 changes: 23 additions & 43 deletions config.m4
Original file line number Diff line number Diff line change
Expand Up @@ -17,47 +17,27 @@ PHP_ARG_ENABLE(timecop, whether to enable timecop support,
[ --enable-timecop Enable timecop support])

if test "$PHP_TIMECOP" != "no"; then
dnl Write more examples of tests here...

dnl # --with-timecop -> check with-path
dnl SEARCH_PATH="/usr/local /usr" # you might want to change this
dnl SEARCH_FOR="/include/timecop.h" # you most likely want to change this
dnl if test -r $PHP_TIMECOP/$SEARCH_FOR; then # path given as parameter
dnl TIMECOP_DIR=$PHP_TIMECOP
dnl else # search default path list
dnl AC_MSG_CHECKING([for timecop files in default path])
dnl for i in $SEARCH_PATH ; do
dnl if test -r $i/$SEARCH_FOR; then
dnl TIMECOP_DIR=$i
dnl AC_MSG_RESULT(found in $i)
dnl fi
dnl done
dnl fi
dnl
dnl if test -z "$TIMECOP_DIR"; then
dnl AC_MSG_RESULT([not found])
dnl AC_MSG_ERROR([Please reinstall the timecop distribution])
dnl fi

dnl # --with-timecop -> add include path
dnl PHP_ADD_INCLUDE($TIMECOP_DIR/include)

dnl # --with-timecop -> check for lib and symbol presence
dnl LIBNAME=timecop # you may want to change this
dnl LIBSYMBOL=timecop # you most likely want to change this

dnl PHP_CHECK_LIBRARY($LIBNAME,$LIBSYMBOL,
dnl [
dnl PHP_ADD_LIBRARY_WITH_PATH($LIBNAME, $TIMECOP_DIR/lib, TIMECOP_SHARED_LIBADD)
dnl AC_DEFINE(HAVE_TIMECOPLIB,1,[ ])
dnl ],[
dnl AC_MSG_ERROR([wrong timecop lib version or lib not found])
dnl ],[
dnl -L$TIMECOP_DIR/lib -lm
dnl ])
dnl
dnl PHP_SUBST(TIMECOP_SHARED_LIBADD)

AC_DEFINE(HAVE_TIMECOPLIB,1,[Whether timecop is present])
PHP_NEW_EXTENSION(timecop, timecop.c, $ext_shared)

dnl Check PHP version:
AC_MSG_CHECKING(PHP version)
if test ! -z "$phpincludedir"; then
PHP_VERSION=`grep 'PHP_VERSION ' $phpincludedir/main/php_version.h | sed -e 's/.*"\([[0-9\.]]*\)".*/\1/g' 2>/dev/null`
elif test ! -z "$PHP_CONFIG"; then
PHP_VERSION=`$PHP_CONFIG --version 2>/dev/null`
fi

if test x"$PHP_VERSION" = "x"; then
AC_MSG_WARN([not found])
else
PHP_MAJOR_VERSION=`echo $PHP_VERSION | sed -e 's/\([[0-9]]*\)\.\([[0-9]]*\)\.\([[0-9]]*\).*/\1/g' 2>/dev/null`
PHP_MINOR_VERSION=`echo $PHP_VERSION | sed -e 's/\([[0-9]]*\)\.\([[0-9]]*\)\.\([[0-9]]*\).*/\2/g' 2>/dev/null`
PHP_RELEASE_VERSION=`echo $PHP_VERSION | sed -e 's/\([[0-9]]*\)\.\([[0-9]]*\)\.\([[0-9]]*\).*/\3/g' 2>/dev/null`
AC_MSG_RESULT([$PHP_VERSION])
fi

if test "$PHP_MAJOR_VERSION" -eq 5; then
PHP_NEW_EXTENSION(timecop, timecop_php5.c, $ext_shared)
else
PHP_NEW_EXTENSION(timecop, timecop_php7.c, $ext_shared)
fi
fi
80 changes: 56 additions & 24 deletions php_timecop.h
Original file line number Diff line number Diff line change
@@ -1,27 +1,21 @@
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2012 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: |
+----------------------------------------------------------------------+
The MIT License
Copyright (c) 2012-2016 Yoshio HANAWA
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

/* $Id$ */

#ifndef PHP_TIMECOP_H
#define PHP_TIMECOP_H

#define PHP_TIMECOP_VERSION "1.0.4"
#define PHP_TIMECOP_VERSION "1.1.0"

extern zend_module_entry timecop_module_entry;
#define phpext_timecop_ptr &timecop_module_entry
Expand Down Expand Up @@ -81,18 +75,50 @@ typedef enum timecop_mode_t {
ZEND_BEGIN_MODULE_GLOBALS(timecop)
long func_override;
long sync_request_time;
#if PHP_VERSION_ID >= 70000
zval orig_request_time;
#else
zval *orig_request_time;
#endif
timecop_mode_t timecop_mode;
long freezed_timestamp;
long travel_offset;
zend_class_entry *ce_DateTime;
zend_class_entry *ce_TimecopDateTime;
ZEND_END_MODULE_GLOBALS(timecop)

struct timecop_override_def {
char *orig_name;
char *ovld_name;
char *save_name;
#if ZEND_DEBUG
# define TIMECOP_ASSERT(c) assert(c)
#else
# define TIMECOP_ASSERT(c)
#endif /* ZEND_DEBUG */

#define SAVE_FUNC_PREFIX "timecop_orig_"
#define OVRD_FUNC_PREFIX "timecop_"

#define OVRD_CLASS_PREFIX "timecop"

#define ORIG_FUNC_NAME(fname) \
(TIMECOP_G(func_override) ? (SAVE_FUNC_PREFIX fname) : fname)

#define ORIG_FUNC_NAME_SIZEOF(fname) \
(TIMECOP_G(func_override) ? sizeof(SAVE_FUNC_PREFIX fname) : sizeof(fname))

#define TIMECOP_OFE(fname) {fname, OVRD_FUNC_PREFIX fname, SAVE_FUNC_PREFIX fname}
#define TIMECOP_OCE(cname, mname) \
{cname, mname, OVRD_CLASS_PREFIX cname, SAVE_FUNC_PREFIX mname}

struct timecop_override_func_entry {
char *orig_func;
char *ovrd_func;
char *save_func;
};

struct timecop_override_class_entry {
char *orig_class;
char *orig_method;
char *ovrd_class;
char *save_method;
};

/* In every utility function you add that needs to use variables
Expand All @@ -105,15 +131,21 @@ struct timecop_override_def {
examples in any other php module directory.
*/

#ifdef ZTS
#define TIMECOP_G(v) TSRMG(timecop_globals_id, zend_timecop_globals *, v)
#if PHP_VERSION_ID >= 70000
# define TIMECOP_G(v) ZEND_MODULE_GLOBALS_ACCESSOR(timecop, v)
# if defined(ZTS) && defined(COMPILE_DL_TIMECOP)
ZEND_TSRMLS_CACHE_EXTERN();
# endif
#else
#define TIMECOP_G(v) (timecop_globals.v)
# ifdef ZTS
# define TIMECOP_G(v) TSRMG(timecop_globals_id, zend_timecop_globals *, v)
# else
# define TIMECOP_G(v) (timecop_globals.v)
# endif
#endif

#endif /* PHP_TIMECOP_H */


/*
* Local variables:
* tab-width: 4
Expand Down
4 changes: 3 additions & 1 deletion tests/011.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ timecop.func_override=0
<?php
$dt1 = new TimecopDateTime("2012-01-01 12:00:00 GMT");
$dt2 = unserialize(serialize($dt1));
var_dump($dt1->format("c"));
var_dump($dt2->format("c"));
--EXPECT--
string(25) "2012-01-01T12:00:00+00:00"
string(25) "2012-01-01T12:00:00+00:00"
string(25) "2012-01-01T12:00:00+00:00"
68 changes: 68 additions & 0 deletions tests/014.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
--TEST--
Check for timecop_date_create
--SKIPIF--
<?php
extension_loaded('timecop') or die('skip timecop not available');
$required_func = array("timecop_freeze", "strtotime", "timecop_date_create");
foreach ($required_func as $func_name) {
if (!function_exists($func_name)) {
die("skip $func_name() function is not available.");
}
}
$required_class = array("datetime");
foreach ($required_class as $class_name) {
if (!class_exists($class_name)) {
die("skip $class_name class is not available.");
}
}
--INI--
date.timezone=America/Los_Angeles
timecop.func_override=0
--FILE--
<?php
timecop_freeze(strtotime("2012-02-29 01:23:45"));

// checking class name of instance
$dt0 = new DateTime();
var_dump(get_class($dt0));

$dts = array(
// constuctor with 0 argument
timecop_date_create(),

// constuctor with 1 argument(null)
timecop_date_create(null),

// constuctor with 1 argument(empty string)
timecop_date_create(""),

// constuctor with 1 argument(absolute format)
timecop_date_create("2012-03-31 12:34:56"),

// constuctor with 1 argument(relative format)
timecop_date_create("+3days"),

// constuctor with 1 argument(including timezone info)
timecop_date_create("1970-01-01 19:00:00 EST"),

// constuctor with 1 argument(unix time)
timecop_date_create("@86400"),

// constuctor with 2 argument
timecop_date_create("now", new DateTimezone("Asia/Tokyo")),
);

foreach ($dts as $dt) {
var_dump($dt->format("c"));
}

--EXPECT--
string(8) "DateTime"
string(25) "2012-02-29T01:23:45-08:00"
string(25) "2012-02-29T01:23:45-08:00"
string(25) "2012-02-29T01:23:45-08:00"
string(25) "2012-03-31T12:34:56-07:00"
string(25) "2012-03-03T01:23:45-08:00"
string(25) "1970-01-01T19:00:00-05:00"
string(25) "1970-01-02T00:00:00+00:00"
string(25) "2012-02-29T18:23:45+09:00"
61 changes: 61 additions & 0 deletions tests/015.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
--TEST--
Check for timecop_date_create_from_format
--SKIPIF--
<?php
extension_loaded('timecop') or die('skip timecop not available');
$required_func = array("timecop_date_create_from_format");
foreach ($required_func as $func_name) {
if (!function_exists($func_name)) {
die("skip $func_name() function is not available.");
}
}
$required_class = array("datetime");
foreach ($required_class as $class_name) {
if (!class_exists($class_name)) {
die("skip $class_name class is not available.");
}
}
if (version_compare(PHP_VERSION, "5.3.4", "<")) die("skip PHP 5.3.4+ required for this test");
--INI--
date.timezone=America/Los_Angeles
timecop.func_override=0
--FILE--
<?php

// checking class name of instance
$dt0 = new DateTime();
var_dump(get_class($dt0));
$dt1 = DateTime::createFromFormat('U', 0);
var_dump(get_class($dt1));

$dts = array(
// constuctor with 2 argument(absolute format)
timecop_date_create_from_format("Y-m-d H:i:s", "2012-03-31 12:34:56"),

// constuctor with 2 argument(including timezone info)
timecop_date_create_from_format("Y-m-d H:i:s T", "1970-01-01 19:00:00 EST"),

// constuctor with 2 argument(unix time)
timecop_date_create_from_format("U", "86400"),

// constuctor with 3 argument
timecop_date_create_from_format("Y-m-d H:i:s", "2012-04-01 00:00:00", new DateTimezone("Asia/Tokyo")),

);

foreach ($dts as $dt) {
var_dump($dt->format("c"));
var_dump($dt->getTimezone()->getName());
}

--EXPECT--
string(8) "DateTime"
string(8) "DateTime"
string(25) "2012-03-31T12:34:56-07:00"
string(19) "America/Los_Angeles"
string(25) "1970-01-01T19:00:00-05:00"
string(3) "EST"
string(25) "1970-01-02T00:00:00+00:00"
string(6) "+00:00"
string(25) "2012-04-01T00:00:00+09:00"
string(10) "Asia/Tokyo"
Loading

0 comments on commit 963fab8

Please sign in to comment.