php-timecop

一个 PHP 扩展,提供 "时间旅行" 功能,灵感来自于 ruby timecop gem。「A PHP extension providing "time travel" capabilities inspired by ruby timecop gem」

Github星跟踪图

php-timecop Travis CI build status CircleCI build status Wercker build statusBuild status

DESCRIPTION

A PHP extension providing "time travel" and "time freezing" capabilities, inspired by ruby timecop gem.

INSTALL (with package manager)

If you are using macOS, you can install php-timecop with Homebrew .

brew install homebrew/php/php71-timecop

In Fedora, you can install php-timecop from official repository as php-pecl-timecop

sudo dnf install php-pecl-timecop

In RHEL/CentOS, you can install php-timecop from Remi's RPM repository.

sudo yum install http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
sudo yum install yum-utils
sudo yum-config-manager --enable remi-php71
sudo yum install php-pecl-timecop

Otherwise, you can use pecl command to install php-timecop.

pecl install timecop-beta

INSTALL (with phpize)

git clone https://github.com/hnw/php-timecop.git
cd php-timecop
phpize
./configure
make
make install

After install, add these lines to your php.ini .

extension=timecop.so

INSTALL (on Windows, experimental)

You can download the extension DLL from PECL :: Package :: timecop

To install the extension, extract zip and copy php_timecop.dll to the extension directory.

After install, add these lines to your php.ini .

extension=php_timecop.dll

SYSTEM REQUIREMENTS

  • OS: Windows(experimental), Linux, macOS
  • PHP: 5.3.1 - 7.2.x (might work on 5.2.x and 5.3.0, but not tested enough)
  • SAPI: Apache, CLI
    • Other SAPIs are not tested, but there is no SAPI-dependent code.
  • non-ZTS(recommended), ZTS

FEATURES

  • Freeze time to a specific point.
  • Travel back to a specific point in time, but allow time to continue moving forward from there.
  • Scale time by a given scaling factor that will cause time to move at an accelerated pace.
  • Override the following PHP stock functions and methods, which supports freezing or traveling time.
    • time()
    • mktime()
    • gmmktime()
    • date()
    • gmdate()
    • idate()
    • getdate()
    • localtime()
    • strtotime()
    • strftime()
    • gmstrftime()
    • microtime()
    • gettimeofday()
    • unixtojd()
    • DateTime::_construct()
    • DateTime::createFromFormat() (PHP >= 5.3.0)
    • DateTimeImmutable::_construct() (PHP >= 5.5.0)
    • DateTimeImmutable::createFromFormat() (PHP >= 5.5.0)
    • date_create()
    • date_create_from_format() (PHP >= 5.3.0)
    • date_create_immutable() (PHP >= 5.5.0)
    • date_create_immutable_from_format() (PHP >= 5.5.0)
  • Rewrite value of the following global variables when the time has been moved.
    • $_SERVER['REQUEST_TIME']

USAGE

<?php
var_dump(date("Y-m-d")); // todays date
timecop_freeze(0);
var_dump(gmdate("Y-m-d H:i:s")); // string(19) "1970-01-01 00:00:00"
var_dump(strtotime("+100000 sec")); // int(100000)

The difference between timecop_freeze() and timecop_travel()

timecop_freeze() is used to statically mock the concept of now. As your program executes, time() will not change unless you make subsequent calls to timecop_freeze/travel/scale/return. timecop_travel(), on the other hand, computes an offset between what we currently think time() is and the time passed in. It uses this offset to simulate the passage of time. To demonstrate, consider the following code snippets:

<?php
$new_time = mktime(12, 0, 0, 9, 1, 2008);
timecop_freeze($new_time);
sleep(10);
var_dump($new_time == time()); // bool(true)

timecop_return(); // "turn off" php-timecop

timecop_travel($new_time);
sleep(10);
var_dump($new_time == time()); // bool(false)

Timecop Scale

timecop_scale($scaling_factor) make time move at an accelerated pace. With this function, you can emulate long-span integration test and reduce execution time of time-dependent unit test.

<?php
Timecop::freeze(new DateTime("2017-01-01 00:00:00"));
Timecop::scale(50); // time goes x50 faster
usleep(100000); // 100ms
var_dump((new DateTime())->format("c")); // string(25) "2017-01-01T00:00:05+00:00"

CHANGELOG

version 1.2.10(beta), 2017/11/23

  • Fix "double free" bug on PHP 7.2.0 (#32)

version 1.2.8(beta), 2017/7/7

  • Publish on PECL
  • Support Windows (experimental)

version 1.2.6(beta), 2017/7/4

  • Bug fixed: Calling timecop_freeze() on a fast machine sometimes fails to stop the microsecond part of current time.
  • Support PHP 7.2.0+

version 1.2.4(beta), 2017/6/8

  • Fix #18 (Fix date_create_from_format when using `

主要指标

概览
名称与所有者hnw/php-timecop
主编程语言C
编程语言C (语言数: 4)
平台Linux, Mac, Windows
许可证MIT License
所有者活动
创建于2012-06-19 09:22:31
推送于2019-10-03 03:12:11
最后一次提交2019-10-03 01:31:02
发布数18
最新版本名称v1.2.10 (发布于 )
第一版名称v1.0.0 (发布于 )
用户参与
星数387
关注者数24
派生数41
提交数173
已启用问题?
问题数36
打开的问题数18
拉请求数14
打开的拉请求数4
关闭的拉请求数1
项目设置
已启用Wiki?
已存档?
是复刻?
已锁定?
是镜像?
是私有?