{
    "mode": "perldoc",
    "parameter": "Test::MockTime",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/Test%3A%3AMockTime/json",
    "generated": "2026-06-10T09:50:17Z",
    "synopsis": "use Test::MockTime qw( :all );\nsetrelativetime(-600);\n# do some tests depending on time increasing from 600 seconds ago\nsetabsolutetime(0);\n# do some more tests depending on time starting from the epoch\n# epoch may vary according to platform.  see perlport.\nsetfixedtime(CORE::time());\n# do some more tests depending on time staying at the current actual time\nsetabsolutetime('1970-01-01T00:00:00Z');\n# do some tests depending on time starting at Unix epoch time\nsetfixedtime('01/01/1970 00:00:00', '%m/%d/%Y %H:%M:%S');\n# do some tests depending on time staying at the Unix epoch time\nrestoretime();\n# resume normal service",
    "sections": {
        "NAME": {
            "content": "Test::MockTime - Replaces actual time with simulated time\n",
            "subsections": []
        },
        "VERSION": {
            "content": "Version 0.17\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "use Test::MockTime qw( :all );\nsetrelativetime(-600);\n\n# do some tests depending on time increasing from 600 seconds ago\n\nsetabsolutetime(0);\n\n# do some more tests depending on time starting from the epoch\n# epoch may vary according to platform.  see perlport.\n\nsetfixedtime(CORE::time());\n\n# do some more tests depending on time staying at the current actual time\n\nsetabsolutetime('1970-01-01T00:00:00Z');\n\n# do some tests depending on time starting at Unix epoch time\n\nsetfixedtime('01/01/1970 00:00:00', '%m/%d/%Y %H:%M:%S');\n\n# do some tests depending on time staying at the Unix epoch time\n\nrestoretime();\n\n# resume normal service\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "This module was created to enable test suites to test code at specific points in time.\nSpecifically it overrides localtime, gmtime and time at compile time and then relies on the user\nsupplying a mock time via setrelativetime, setabsolutetime or setfixedtime to alter future\ncalls to gmtime,time or localtime.\n",
            "subsections": []
        },
        "SUBROUTINES/METHODS": {
            "content": "setabsolutetime\nIf given a single, numeric argument, the argument is an absolute time (for example, if 0 is\nsupplied, the absolute time will be the epoch), and calculates the offset to allow\nsubsequent calls to time, gmtime and localtime to reflect this.\n\nfor example, in the following code\n\nTest::MockTime::setabsolutetime(0);\nmy ($start) = time;\nsleep 2;\nmy ($end) = time;\n\nThe $end variable should contain 2 seconds past the epoch;\n\nIf given two arguments, the first argument is taken to be an absolute time in some string\nformat (for example, \"01/01/1970 00:00:00\"). The second argument is taken to be a \"strptime\"\nformat string (for example, \"%m/%d/%Y %H:%M:%S\"). If a single argument is given, but that\nargument is not numeric, a \"strptime\" format string of \"%Y-%m-%dT%H:%M:%SZ\" is assumed.\n\nfor example, in the following code\n\nTest::MockTime::setabsolutetime('1970-01-01T00:00:00Z');\nmy ($start) = time;\nsleep 2;\nmy ($end) = time;\n\nThe $end variable should contain 2 seconds past the Unix epoch;\n",
            "subsections": [
                {
                    "name": "set_relative_time",
                    "content": "takes as an argument an relative value from current time (for example, if -10 is supplied,\ncurrent time be converted to actual machine time - 10 seconds) and calculates the offset to\nallow subsequent calls to time,gmtime and localtime to reflect this.\n\nfor example, in the following code\n\nmy ($start) = time;\nTest::MockTime::setrelativetime(-600);\nsleep 600;\nmy ($end) = time;\n\nThe $end variable should contain either the same or very similar values to the $start\nvariable.\n\nsetfixedtime\nIf given a single, numeric argument, the argument is an absolute time (for example, if 0 is\nsupplied, the absolute time will be the epoch). All subsequent calls to gmtime, localtime\nand time will return this value.\n\nfor example, in the following code\n\nTest::MockTime::setfixedtime(time)\nmy ($start) = time;\nsleep 3;\nmy ($end) = time;\n\nthe $end variable and the $start variable will contain the same results\n\nIf given two arguments, the first argument is taken to be an absolute time in some string\nformat (for example, \"01/01/1970 00:00:00\"). The second argument is taken to be a \"strptime\"\nformat string (for example, \"%m/%d/%Y %H:%M:%S\"). If a single argument is given, but that\nargument is not numeric, a \"strptime\" format string of \"%Y-%m-%dT%H:%M:%SZ\" is assumed.\n"
                },
                {
                    "name": "restore",
                    "content": "restore the default time handling values. \"restoretime\" is an alias. When exported with the\n'all' tag, this subroutine is exported as \"restoretime\".\n"
                }
            ]
        },
        "CONFIGURATION AND ENVIRONMENT": {
            "content": "Test::MockTime requires no configuration files or environment variables.\n",
            "subsections": []
        },
        "DEPENDENCIES": {
            "content": "Test::MockTime depends on the following non-core Perl modules.\n\n*   Time::Piece 1.08 or greater\n",
            "subsections": []
        },
        "INCOMPATIBILITIES": {
            "content": "None reported\n",
            "subsections": []
        },
        "BUGS AND LIMITATIONS": {
            "content": "Probably.\n",
            "subsections": []
        },
        "AUTHOR": {
            "content": "David Dick <ddick@cpan.org>\n",
            "subsections": []
        },
        "LICENSE AND COPYRIGHT": {
            "content": "This program is free software; you can redistribute it and/or modify it under the same terms as\nPerl itself.\n",
            "subsections": []
        },
        "ACKNOWLEDGEMENTS": {
            "content": "Thanks to a use.perl.org journal entry <http://use.perl.org/~geoff/journal/20660> by Geoffrey\nYoung.\n",
            "subsections": []
        }
    },
    "summary": "Test::MockTime - Replaces actual time with simulated time",
    "flags": [],
    "examples": [],
    "see_also": []
}