{"id":13428131,"url":"https://github.com/travisjeffery/timecop","last_synced_at":"2025-05-12T05:22:59.631Z","repository":{"id":449917,"uuid":"72991","full_name":"travisjeffery/timecop","owner":"travisjeffery","description":"A gem providing \"time travel\", \"time freezing\", and \"time acceleration\" capabilities, making it simple to test time-dependent code. It provides a unified method to mock Time.now, Date.today, and DateTime.now in a single call.","archived":false,"fork":false,"pushed_at":"2024-12-30T18:38:03.000Z","size":403,"stargazers_count":3396,"open_issues_count":8,"forks_count":227,"subscribers_count":31,"default_branch":"master","last_synced_at":"2025-05-12T04:01:53.700Z","etag":null,"topics":["rails","ruby","test","time"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/travisjeffery.png","metadata":{"files":{"readme":"README.markdown","changelog":"History.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2008-11-07T20:37:52.000Z","updated_at":"2025-05-12T03:12:33.000Z","dependencies_parsed_at":"2024-05-01T13:20:09.883Z","dependency_job_id":"e406b2fa-2002-4d7b-9d1c-f65ecdf0ed8d","html_url":"https://github.com/travisjeffery/timecop","commit_stats":{"total_commits":409,"total_committers":96,"mean_commits":4.260416666666667,"dds":0.8092909535452323,"last_synced_commit":"d695c6f8e69c88d61bdb25d74867dc560860f019"},"previous_names":[],"tags_count":51,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/travisjeffery%2Ftimecop","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/travisjeffery%2Ftimecop/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/travisjeffery%2Ftimecop/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/travisjeffery%2Ftimecop/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/travisjeffery","download_url":"https://codeload.github.com/travisjeffery/timecop/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253679515,"owners_count":21946417,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["rails","ruby","test","time"],"created_at":"2024-07-31T01:00:46.988Z","updated_at":"2025-05-12T05:22:59.605Z","avatar_url":"https://github.com/travisjeffery.png","language":"Ruby","readme":"# timecop\n\n[![Gem Version](https://badge.fury.io/rb/timecop.svg)](https://rubygems.org/gems/timecop)\n[![Build Status](https://github.com/travisjeffery/timecop/workflows/CI/badge.svg)](https://github.com/travisjeffery/timecop/actions?query=workflow%3ACI)\n\n## DESCRIPTION\n\nA gem providing \"time travel\" and \"time freezing\" capabilities, making it dead simple to test time-dependent code. It provides a unified method to mock `Time.now`, `Date.today`, `DateTime.now`, and `Process.clock_gettime` in a single call.\n\n## INSTALL\n\n`bundle add timecop`\n\n## FEATURES\n\n- Freeze time to a specific point.\n- Travel back to a specific point in time, but allow time to continue moving forward from there.\n- Scale time by a given scaling factor that will cause time to move at an accelerated pace.\n- No dependencies, can be used with _any_ ruby project\n- Timecop api allows arguments to be passed into `#freeze` and `#travel` as one of the following:\n  - Time instance\n  - DateTime instance\n  - Date instance\n  - individual arguments (year, month, day, hour, minute, second)\n  - a single integer argument that is interpreted as an offset in seconds from `Time.now`\n- Nested calls to `Timecop#travel` and `Timecop#freeze` are supported -- each block will maintain its interpretation of now.\n- Works with regular Ruby projects, and Ruby on Rails projects\n\n## USAGE\n\nRun a time-sensitive test\n\n```ruby\njoe = User.find(1)\njoe.purchase_home()\nassert !joe.mortgage_due?\n# move ahead a month and assert that the mortgage is due\nTimecop.freeze(Date.today + 30) do\n  assert joe.mortgage_due?\nend\n```\n\nYou can mock the time for a set of tests easily via setup/teardown methods\n\n```ruby\ndescribe \"some set of tests to mock\" do\n  before do\n    Timecop.freeze(Time.local(1990))\n  end\n\n  after do\n    Timecop.return\n  end\n\n  it \"should do blah blah blah\" do\n  end\nend\n```\n\nSet the time for the test environment of a rails app -- this is particularly\nhelpful if your whole application is time-sensitive.  It allows you to build\nyour test data at a single point in time, and to move in/out of that time as\nappropriate (within your tests)\n\nin `config/environments/test.rb`\n\n```ruby\nconfig.after_initialize do\n  # Set Time.now to September 1, 2008 10:05:00 AM (at this instant), but allow it to move forward\n  t = Time.local(2008, 9, 1, 10, 5, 0)\n  Timecop.travel(t)\nend\n```\n\n### The difference between Timecop.freeze and Timecop.travel\n\n`freeze` is used to statically mock the concept of now. As your program executes,\n`Time.now` will not change unless you make subsequent calls into the Timecop API.\n`travel`, on the other hand, computes an offset between what we currently think\n`Time.now` is (recall that we support nested traveling) and the time passed in.\nIt uses this offset to simulate the passage of time.  To demonstrate, consider\nthe following code snippets:\n\n```ruby\nnew_time = Time.local(2008, 9, 1, 12, 0, 0)\nTimecop.freeze(new_time)\nsleep(10)\nnew_time == Time.now # ==\u003e true\n\nTimecop.return # \"turn off\" Timecop\nTimecop.travel(new_time)\nsleep(10)\nnew_time == Time.now # ==\u003e false\n```\n\n### Timecop.scale\n\nLet's say you want to test a \"live\" integration wherein entire days could pass by\nin minutes while you're able to simulate \"real\" activity. For example, one such use case\nis being able to test reports and invoices that run in 30 day cycles in very little time, while also\nbeing able to simulate activity via subsequent calls to your application.\n\n```ruby\n# seconds will now seem like hours\nTimecop.scale(3600)\nTime.now\n# =\u003e 2012-09-20 21:23:25 -0500\n# seconds later, hours have passed and it's gone from 9pm at night to 6am in the morning\nTime.now\n# =\u003e 2012-09-21 06:22:59 -0500\n```\n\nSee [#42](https://github.com/travisjeffery/timecop/pull/42) for more information, thanks to Ken Mayer, David Holcomb, and Pivotal Labs.\n\n### Timecop.safe_mode\n\nSafe mode forces you to use Timecop with the block syntax since it always puts time back the way it was. If you are running in safe mode and use Timecop without the block syntax `Timecop::SafeModeException` will be raised to tell the user they are not being safe.\n\n``` ruby\n# turn on safe mode\nTimecop.safe_mode = true\n\n# check if you are in safe mode\nTimecop.safe_mode?\n# =\u003e true\n\n# using method without block\nTimecop.freeze\n# =\u003e Timecop::SafeModeException: Safe mode is enabled, only calls passing a block are allowed.\n```\n\n### Configuring Mocking Process.clock_gettime\n\nBy default Timecop does not mock Process.clock_gettime. You must enable it like this:\n\n``` ruby\n# turn on\nTimecop.mock_process_clock = true\n```\n\n### Rails v Ruby Date/Time libraries\n\nSometimes [Rails Date/Time methods don't play nicely with Ruby Date/Time methods.](https://rails.lighthouseapp.com/projects/8994/tickets/6410-dateyesterday-datetoday)\n\nBe careful mixing Ruby `Date.today` with Rails `Date.tomorrow` / `Date.yesterday` as things might break.\n\n## Contribute\n\ntimecop is maintained by [travisjeffery](http://github.com/travisjeffery), and\nwas created by [jtrupiano](https://github.com/jtrupiano).\n\nHere's the most direct way to get your work merged into the project.\n\n- Fork the project\n- Clone down your fork\n- Create a feature branch\n- Hack away and add tests, not necessarily in that order\n- Make sure everything still passes by running tests\n- If necessary, rebase your commits into logical chunks without errors\n- Push the branch up to your fork\n- Send a pull request for your branch\n\n","funding_links":[],"categories":["Testing","Ruby","测试","Gems"],"sub_categories":["Omniauth","Time Warping","Profiling and Performance"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftravisjeffery%2Ftimecop","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftravisjeffery%2Ftimecop","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftravisjeffery%2Ftimecop/lists"}