-
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.
Handle microseconds accurately in time traveling / Now timecop_freeze()
and timecop_travel() accepts either DateTimeInterface or int / Implement timecop_scale() / Implement Timecop::***() as alias of timecop_***()
- Loading branch information
Showing
18 changed files
with
1,054 additions
and
325 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
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,74 @@ | ||
#include "tc_timeval.h" | ||
|
||
int tc_timeval_add(tc_timeval *ret, const tc_timeval *arg1, const tc_timeval *arg2) | ||
{ | ||
tc_timeval_long sec, usec; | ||
usec = arg1->usec + arg2->usec; | ||
sec = arg1->sec + arg2->sec; | ||
if (usec < 0) { | ||
sec -= ((-usec) / USEC_PER_SEC + 1); | ||
usec = USEC_PER_SEC - ((-usec) % USEC_PER_SEC); | ||
if (usec == USEC_PER_SEC) { | ||
sec++; | ||
usec = 0; | ||
} | ||
} else if (usec >= USEC_PER_SEC) { | ||
sec += usec / USEC_PER_SEC; | ||
usec = usec % USEC_PER_SEC; | ||
} | ||
ret->sec = sec; | ||
ret->usec = usec; | ||
|
||
return 0; | ||
} | ||
int tc_timeval_sub(tc_timeval *ret, const tc_timeval *arg1, const tc_timeval *arg2) | ||
{ | ||
tc_timeval_long sec, usec; | ||
usec = arg1->usec - arg2->usec; | ||
sec = arg1->sec - arg2->sec; | ||
if (usec < 0) { | ||
sec -= ((-usec) / USEC_PER_SEC + 1); | ||
usec = USEC_PER_SEC - ((-usec) % USEC_PER_SEC); | ||
if (usec == USEC_PER_SEC) { | ||
sec++; | ||
usec = 0; | ||
} | ||
} else if (usec >= USEC_PER_SEC) { | ||
sec += usec / USEC_PER_SEC; | ||
usec = usec % USEC_PER_SEC; | ||
} | ||
ret->sec = sec; | ||
ret->usec = usec; | ||
|
||
return 0; | ||
} | ||
int tc_timeval_mul(tc_timeval *ret, const tc_timeval *arg1, const tc_timeval_long arg2) | ||
{ | ||
tc_timeval_long sec, usec; | ||
usec = arg1->usec * arg2; | ||
sec = arg1->sec * arg2; | ||
if (usec < 0) { | ||
sec -= ((-usec) / USEC_PER_SEC + 1); | ||
usec = USEC_PER_SEC - ((-usec) % USEC_PER_SEC); | ||
if (usec == USEC_PER_SEC) { | ||
sec++; | ||
usec = 0; | ||
} | ||
} else if (usec >= USEC_PER_SEC) { | ||
sec += usec / USEC_PER_SEC; | ||
usec = usec % USEC_PER_SEC; | ||
} | ||
ret->sec = sec; | ||
ret->usec = usec; | ||
|
||
return 0; | ||
} | ||
|
||
/* | ||
* Local variables: | ||
* tab-width: 4 | ||
* c-basic-offset: 4 | ||
* End: | ||
* vim600: noet sw=4 ts=4 fdm=marker | ||
* vim<600: noet sw=4 ts=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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#ifndef TC_TIMEVAL_H | ||
#define TC_TIMEVAL_H | ||
|
||
#ifndef USEC_PER_SEC | ||
# define USEC_PER_SEC 1000000 | ||
#endif | ||
|
||
#if PHP_MAJOR_VERSION >= 7 | ||
typedef zend_long tc_timeval_long; | ||
#else | ||
typedef long tc_timeval_long; | ||
#endif | ||
|
||
typedef struct _tc_timeval { | ||
tc_timeval_long sec; | ||
tc_timeval_long usec; | ||
} tc_timeval; | ||
|
||
int tc_timeval_add(tc_timeval *ret, const tc_timeval *arg1, const tc_timeval *arg2); | ||
int tc_timeval_sub(tc_timeval *ret, const tc_timeval *arg1, const tc_timeval *arg2); | ||
int tc_timeval_mul(tc_timeval *ret, const tc_timeval *arg1, const long arg2); | ||
|
||
#endif /* TC_TIMEVAL_H */ | ||
|
||
/* | ||
* Local variables: | ||
* tab-width: 4 | ||
* c-basic-offset: 4 | ||
* End: | ||
* vim600: noet sw=4 ts=4 fdm=marker | ||
* vim<600: noet sw=4 ts=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
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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
--TEST-- | ||
Check for timecop_scale | ||
--SKIPIF-- | ||
<?php | ||
extension_loaded('timecop') or die('skip timecop not available'); | ||
$required_func = array("timecop_scale", "timecop_travel", "timecop_time"); | ||
foreach ($required_func as $func_name) { | ||
if (!function_exists($func_name)) { | ||
die("skip $func_name() function is not available."); | ||
} | ||
} | ||
?> | ||
--FILE-- | ||
<?php | ||
timecop_travel(123); | ||
timecop_scale(50); | ||
usleep(200000); // 200ms * 50 = 10sec | ||
var_dump(timecop_time()); | ||
|
||
timecop_scale(1); | ||
timecop_travel(123); | ||
sleep(1); | ||
var_dump(timecop_time()); | ||
--EXPECT-- | ||
int(133) | ||
int(124) |
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,36 @@ | ||
--TEST-- | ||
Check for issue #14 (All PHPUnit assertions involving DateTime comparison fail with PHP 7.1) | ||
--SKIPIF-- | ||
<?php | ||
extension_loaded('timecop') or die('skip timecop not available'); | ||
--INI-- | ||
date.timezone=GMT | ||
timecop.func_override=1 | ||
--FILE-- | ||
<?php | ||
class CustomerInvoice | ||
{ | ||
private $voidDate; | ||
public function void() | ||
{ | ||
$this->voidDate = new \DateTime(); | ||
} | ||
public function getvoidDate() | ||
{ | ||
return $this->voidDate; | ||
} | ||
} | ||
function test_invoice_void() | ||
{ | ||
$now = new \DateTime(); | ||
//timecop_freeze($now->getTimestamp()); | ||
Timecop::freeze($now); | ||
|
||
$invoice = new CustomerInvoice(); | ||
$invoice->void(); | ||
|
||
var_dump($now == $invoice->getVoidDate()); | ||
} | ||
test_invoice_void(); | ||
--EXPECT-- | ||
bool(true) |
Oops, something went wrong.