Ant Script to Change System Date-Time on Win/Linux/Mac OS

Probably you are looking for a script that can change (increase/decrease) system date and time on Windows and Linux platforms. In this post, we’ll be exactly doing that. We’ll tell you how to create an ANT script which can set both past and a future date and time by a unit factor that you can specify.

This script is especially useful for automation testing, build infra operations, and in executing DevOps tasks. It’s because you need to support multiple platforms for all these requirements. And many times it requires to play with the system date/time for a quick execution.

1- Starting with Ant Script – Setup & Installation.

Ant script to change system date and time.

Ant script to change system date and time.

1.1- What is ANT Script?

First of all, you should know that ANT is a universal build tool created in Java. It supports multiple platforms like Windows, Linux, and Mac OS X. It operates using a build rule file in XML format. In this file, you can define all the build logic and add build dependencies. It primarily supports building large Java projects with dependencies. You can use this tool for extended usage like the ones we’ve mentioned in the beginning. In this blog post, we’ll teach you how to create an ANT script to change the system date and time.

Let’s first start with the ANT installation and setup.

1.2- How to Download and Setup ANT Tool for Scripting.

We’ve compiled few steps that you can follow to install and setup ANT tool.

1.2.1- You can download ANT in compressed format from its official website.
1.2.2- Download and install Java Runtime environment (JRE) from OpenJDK if not already installed.
1.2.3. Extract the ANT downloaded file into a directory.
1.2.4- Define the following environment variables.

JAVA_HOME (e.g. JAVA_HOME=C:\Program Files\Java\jdk1.7.0_51)
ANT_HOME to the directory you extracted ANT to
Add ${ANT_HOME}/bin (Unix) or %ANT_HOME%/bin (Windows) to your PATH.

1.2.5- Next update the library dependencies of ANT tasks by running the following command from the ANT_HOME folder:

ant -f fetch.xml -Ddest=system

If you don’t perform this, some of the dependent Ant tasks will not be available.

1.2.6- This is how you can check the Apache ANT Installation.

To confirm the successful ANT installation on your computer, type ant onto the console prompt.
You should see an output similar to:

C:\>ant -version
Apache Ant(TM) version 1.9.5 compiled on June 2 2015

If you do not see the similar output, then please revisit to check that you have performed the installation instructions properly.

2- Write Your First Ant Script – Example & Execution.

2.1- Sample ANT script to change system date and time.

Dear readers, in this section I’ve chipped in the full ready to use ANT script sample for your reference. Follow all the steps to setup ANT and then directly copy-paste the below code snippet into a file and save it as <SetDateTime.xml>.

<project name="SetDateTime" basedir=".">
	<!--Variables <=> Windows/Linux-->
	<property environment="env"/>
	<property name="workingDir" value="${env.WorkingDir}"/>
	<property name = "antLog" value = "antLog.log"/>
	<property name = "updateTime" value = "updateTime.properties"/>
	<condition property="format" value="dd-MM-yy">
		<os family="windows" />
	</condition>
	<condition property="format" value="YYYY-MM-dd">
		<os family="unix" />
	</condition>	

	<!--To Update Time-->
	<target name = "deleteUpdate">
		<delete file="${updateTime}" failonerror="false"/>
	</target>

	<!--Ant target to increase date -->
	<target name="increaseDate" depends="deleteUpdate">
		<delete file="${antLog}" failonerror="false"/>
		<propertyfile file="updateTime.properties">
			<entry  key="formated.tomorrow" type="date" default = "now" pattern="${format}" operation="+" value="${value}" unit="${unit}"/>
		</propertyfile>
		<property file="updateTime.properties"/>
		<exec executable="cmd" output="${antLog}" append="true" osfamily="windows">
			<arg value="/c"/>
			<arg value="date ${formated.tomorrow}"/>
		</exec>
		<exec executable="date" output="${antLog}" append="true" osfamily="unix">
			<arg value="-s"/>
			<arg value="${formated.tomorrow}"/>
		</exec>
	</target>

	<!--Ant target to decrease date -->
	<target name="decreaseDate" depends="deleteUpdate">
		<delete file="${antLog}" failonerror="false"/>
		<propertyfile file="updateTime.properties">
			<entry  key="formated.tomorrow" type="date" default = "now" pattern="${format}" operation="-" value="${value}" unit="${unit}"/>
		</propertyfile>
		<property file="updateTime.properties"/>
		<exec executable="cmd" output="${antLog}" append="true" osfamily="windows">
			<arg value="/c"/>
			<arg value="date ${formated.tomorrow}"/>
		</exec>
		<exec executable="date" output="${antLog}" append="true" osfamily="unix">
			<arg value="-s"/>
			<arg value="${formated.tomorrow}"/>
		</exec>
	</target>

	<!--Ant target to increase time -->
	<target name="increaseTime" depends="deleteUpdate">
		<delete file="${antLog}" failonerror="false"/>
		<propertyfile file="updateTime.properties">
			<entry key="futureTime" type="date" value="now" pattern="HH:mm"/>
			<entry key="futureTime" type="date" default="now" operation="+" value="${value}" unit="${unit}" pattern="HH:mm"/>
		</propertyfile>
		<property file="updateTime.properties"/>
		<exec executable="cmd" output="${antLog}" append="true" osfamily="windows">
			<arg value="/c"/>
			<arg value="time ${futureTime}"/>
		</exec>
		<exec executable="date" output="${antLog}" append="true" osfamily="unix">
			<arg value="-s"/>
			<arg value="${futureTime}"/>
		</exec>
	</target>

	<!--Ant target to decrease time -->
	<target name="decreaseTime" depends="deleteUpdate">
		<delete file="${antLog}" failonerror="false"/>
		<propertyfile file="updateTime.properties">
			<entry key="futureTime" type="date" value="now" pattern="HH:mm"/>
			<entry key="futureTime" type="date" default="now" operation="-" value="${value}" unit="${unit}" pattern="HH:mm"/>
		</propertyfile>
		<property file="updateTime.properties"/>
		<exec executable="cmd" output="${antLog}" append="true" osfamily="windows">
			<arg value="/c"/>
			<arg value="time ${futureTime}"/>
		</exec>
		<exec executable="date" output="${antLog}" append="true" osfamily="unix">
			<arg value="-s"/>
			<arg value="${futureTime}"/>
		</exec>
	</target>
</project>

 

2.2- How to call the ANT targets to change system date/time.

Calling the ANT target is pretty simple. But before you run the ANT script, make sure the following.

2.2.1- Set Java/JDK environment variables like JAVA_HOME, JDK_HOME etc.

2.2.2- Define the ANT variables like ANT_HOME to set the ANT path.

2.2.2- Save the code given the previous section as <SetDateTime.xml>.

In the next few lines, you will the find the desired commands to invoke the ANT targets.

Example-1) Increase Date by 2 days.

ant.bat -Dunit=day -Dvalue=2 -f SetDateTime.xml increaseDate

Example-2) Decrease Date by 2 days.

ant.bat -Dunit=day -Dvalue=2 -f SetDateTime.xml DecreaseDate

Example-3) Increase Time by 2 hours.

ant.bat -Dunit=hour -Dvalue=2 -f SetDateTime.xml increaseTime

Example-4) Decrease Time by 2 hours.

ant.bat -Dunit=hour -Dvalue=2 -f SetDateTime.xml DecreaseTime

So from the above examples, you can observe the ANT script’s (ant.bat) command line input parameters. There are two main parameters i.e. “unit” and “value“. These parameters accept date/time unit in day or hour and value is the factor which you would like the date/time to increase/decrease by.

 

Final Thought.

We wish that while reading this article, you would have created a basic understanding about the ANT script. And probably you all would be using this information in your live test automation or build infra environments.

Finally, we would like to request you to leave your feedback/comments in the comment section. Your inputs will keep us motivated and drive us to produce improved contents every time.

 

Learn to Succeed,

TechBeamers.