php-timecop

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

Github stars Tracking Chart

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 `

Overview

Name With Ownerhnw/php-timecop
Primary LanguageC
Program languageC (Language Count: 4)
PlatformLinux, Mac, Windows
License:MIT License
Release Count18
Last Release Namev1.2.10 (Posted on )
First Release Namev1.0.0 (Posted on )
Created At2012-06-19 09:22:31
Pushed At2019-10-03 03:12:11
Last Commit At2019-10-03 01:31:02
Stargazers Count384
Watchers Count27
Fork Count39
Commits Count173
Has Issues Enabled
Issues Count35
Issue Open Count17
Pull Requests Count14
Pull Requests Open Count4
Pull Requests Close Count1
Has Wiki Enabled
Is Archived
Is Fork
Is Locked
Is Mirror
Is Private
To the top