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 that can set both a past and future date-time with a unit factor that you specify.
Starting with Ant Script – Setup & Installation
This script is especially useful for automation testing, building infra operations, and executing DevOps tasks. It’s because you need to support multiple platforms for all these requirements. And, many times it requires playing with the system date/time for a quick execution.
Also Read: Learn to Create a Linux Service in 2 Minutes
Why Should You Use 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’s date and time.
Let’s first start with the ANT installation and setup.
How to Download and Setup ANT Tool for Scripting
We’ve compiled a few steps that you can follow to install and set up the ANT tool.
- You can download ANT in compressed format from its official website.
- Download and install Java Runtime Environment (JRE) from OpenJDK if not already installed.
- Extract the ANT downloaded file into a directory.
- 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.
- Next update the library dependencies of ANT tasks by running the following command from the ANT_HOME folder. If you don’t perform this, some of the dependent Ant tasks will not be available.
ant -f fetch.xml -Ddest=system
- 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 a similar output, then please revisit to check that you have performed the installation instructions properly.
Write Your First Ant Script – Example & Execution
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 set up 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>
Call the ANT Targets to Change the System Date-Time
Calling the ANT target is pretty simple. But before you run the ANT script, make sure the following.
- Set Java/JDK environment variables like JAVA_HOME, JDK_HOME, etc.
- Define the ANT variables like ANT_HOME to set the ANT path.
- Save the code given in the previous section as
SetDateTime.xml
.
In the next few lines, you will find the desired commands to invoke the ANT targets.
Example-1) Increase the Date by 2 days.
ant.bat -Dunit=day -Dvalue=2 -f SetDateTime.xml increaseDate
Example-2) Decrease the 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 units in day or hour and value is the factor by which you would like the date/time to increase/decrease.
Final Thought
We wish that while reading this article, you would have created a basic understanding of 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 input will keep us motivated and drive us to produce improved content every time.
Keep Learning,
TechBeamers.