How can I run a Delphi Win32 application with a specific local time?
Asked Answered
M

1

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.

Minna answered 29/11, 2013 at 8:46 Comment(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.Alpenstock
@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.Minna
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+SERVEREpiphysis
what is a "CI system"?Fix
@Fix CI = Continuous Integration en.wikipedia.org/wiki/Continuous_integrationMinna
B
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

Bandolier answered 29/11, 2013 at 10:24 Comment(4)
Does this allow time to pass, or is the time static?Alvey
@David, haven't tested it (yet), but the current version has this option. I cannot believe someone made such tool. Nice!Alpenstock
@Alpenstock All we have to do now is wait until the next Y2K bug and we'll be all tooled up!!Alvey
This is also quite helpful for testing daylight saving time shifts. Thanks a lot for that answer. Accepted!Minna

© 2022 - 2024 — McMap. All rights reserved.