Getting Unix time (epoch) in an Ant build file

Ant includes a Tstamp task that allows a property to be set to the current date and time in a given format. Unfortunately there is no way to get that value as a Unix time (epoch).

Here's how you can do this with the help of some Javascript:

<target name="print-epoch">
  <script language="javascript">
  <![CDATA[
    property = project.setProperty("now",Math.floor((new Date()).getTime()/1000));
  ]]>
  </script>
  <echo message="${now}" />
</target>

Check the documentation of the script task for more information about scripts and their dependencies in Ant.

Topic: 

Add new comment