# Controlling time in the app

DevFeed: [Controlling time in the app](<https://devfeed.tech/articles/controlling-time-in-the-app-21615.md>)

Original publisher: [Read original article](<http://miqu.me/blog/2016/10/26/controlling-time-in-the-app/>)

Author: Miguel Angel Quiñones

Published: 2016-10-26T18:38:37Z

Content type: tutorial

Language: en

Sources: [Miguel Quinones](<https://devfeed.tech/sources/miguel-quinones.md>)

Topics: [App](<https://devfeed.tech/topics/app.md>), [Testing](<https://devfeed.tech/topics/testing.md>), [iOS](<https://devfeed.tech/topics/ios.md>), [Swift](<https://devfeed.tech/topics/swift.md>), [Library](<https://devfeed.tech/topics/library.md>), [Code](<https://devfeed.tech/topics/code.md>), [simulator](<https://devfeed.tech/topics/simulator.md>)

Tags: [app](<https://devfeed.tech/tags/app.md>), [debugging](<https://devfeed.tech/tags/debugging.md>), [ios](<https://devfeed.tech/tags/ios.md>), [library](<https://devfeed.tech/tags/library.md>), [simulator](<https://devfeed.tech/tags/simulator.md>), [swift](<https://devfeed.tech/tags/swift.md>), [testing](<https://devfeed.tech/tags/testing.md>), [tests](<https://devfeed.tech/tags/tests.md>)

## AI overview

The article explains how to control an application's perceived system date and time to make time-dependent behavior easier to test and debug. It presents dependency injection for unit tests and describes using the TUDelorean library with an in-app debugging menu to simulate time changes and system-triggered events without changing the device or computer clock.

## Source excerpt

Today I want to share a small utility we've been using for a while at Peak, my current workplace, to control the system time inside the application and save time while testing or debugging. In most application code you'll eventually end up with tasks that need to execute periodically, or after some time has passed. The period of time will depend on the actual application requirements, and might change from the order of seconds to days. For example, you might have a data cleanup every 30 days, your application might ping a backend for synchronization every 10 minutes, or by the start of every day, some data needs to be generated and presented to the user. Foundation and dates You'll eventually use NSDate and NSCalendar (or Date and Calendar in Swift) to calculate when your tasks should be executed. As an example, say you have a task scheduler, and it will run tasks after a specific amount of time passed: 1 2 3 4 5 6 7 8 9 10 11 12 13 protocol Task { func execute() } class Scheduler { func schedule(_ task: Task, every: TimeInterval) { // Schedule and save task to run every x seconds } func run(with date: Date) { // Actually perform the calculations and fire due tasks } } For this code to be testable it's a very common practice to pass the Date in, as we do in the example above. This will improve testability as the date can be controlled from unit tests, thus isolating the system date from the date the component uses to operate. But what about the times when you want to check the integration between the code and the system date? What if you want to trigger code that reacts to UIApplicationSignificantTimeChange? The traditional way to test this kind of interactions is to change the date on the device, be it on iOS directly, or you or computer if running the simulator. But it would be very useful to control the system date and time inside the application without having to change the system date in the device or your computer. Time travel To control the time from a debugg