11

I have a Delphi program that starts behaving wrong when run over midnight. To make a reproducable test case I have to change the local time of the system to something before midnight, run the program for some time, check for an error and switch back. While this can surely be done manually, I have problems to automate that for testing.

Are there any possibilities to give an application a special time when run whithout changing the system time? This can be from outside the program or even inside.

Although this might be a general Windows question I tagged it with Delphi as any solution working with Delphi is fine, while some with other programming environments are useless in this case.

5
  • Except some existing tool (which might be quite difficult to develop for almost no reason, since you can easily make a tool which will change the system time for a test and change it back after by yourself), I doubt there is a way other than redirecting Windows API functions whose are getting time.
    – TLama
    Commented Nov 29, 2013 at 8:59
  • @TLama, sure I can write a tool that does the time switch. As the tests should run inside a CI system, this time switching might interfer with other processes running on that system at the same time. It might even interfer with the CI system itself. I already thought of redirecting the calls to GetLocalTime, but I decided to ask here first.
    – Uwe Raabe
    Commented Nov 29, 2013 at 9:11
  • Maybe you can use an alternate time server in your test environment? I found a post (Googling google.com/search?q="change+windows+time"+testing) from someone wanting to do that in XP and he found answers by Googling google.com/search?hl=en&q=XP+TIME+SERVER
    – Jan Doggen
    Commented Nov 29, 2013 at 9:42
  • what is a "CI system"?
    – Sam
    Commented Dec 2, 2013 at 0:08
  • @Sam CI = Continuous Integration en.wikipedia.org/wiki/Continuous_integration
    – Uwe Raabe
    Commented Dec 2, 2013 at 11:06

1 Answer 1

16

There is an existing tool for that called RunAsDate:

Description

RunAsDate is a small utility that allows you to run a program in the date and time that you specify. This utility doesn't change the current system date and time of your computer, but it only injects the date/time that you specify into the desired application. You can run multiple applications simultaneously, each application works with different date and time, while the real date/time of your system continues to run normally.

How does it work ?

RunAsDate intercepts the kernel API calls that returns the current date and time (GetSystemTime, GetLocalTime, GetSystemTimeAsFileTime), and replaces the current date/time with the date/time that you specify.

System Requirements

RunAsDate works under Windows 2000, Windows XP, Windows Server 2003, Windows Server 2008, Windows Vista, Windows 7, and Windows 8. Older versions of Windows are not supported. The is also x64 version available in a separated download, for using with x64 applications.

enter image description here

4
  • 1
    Does this allow time to pass, or is the time static? Commented Nov 29, 2013 at 10:35
  • 5
    @David, haven't tested it (yet), but the current version has this option. I cannot believe someone made such tool. Nice!
    – TLama
    Commented Nov 29, 2013 at 11:15
  • 4
    @TLama All we have to do now is wait until the next Y2K bug and we'll be all tooled up!! Commented Nov 29, 2013 at 11:16
  • 4
    This is also quite helpful for testing daylight saving time shifts. Thanks a lot for that answer. Accepted!
    – Uwe Raabe
    Commented Nov 29, 2013 at 12:04

Not the answer you're looking for? Browse other questions tagged or ask your own question.