-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PHP 7 support / Now
new DateTime()
always returns DateTime
instance
- Loading branch information
Showing
15 changed files
with
1,599 additions
and
225 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
@@ -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 | ||
|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
Oops, something went wrong.