问题
I need to calculate time difference using Ant.
Basically it has 2 variables. One is assigned the current time, and the other one has a different time. I need to get the time difference using Ant. Something like below. If anyone have code please reply.
variable a = current time;
variable b = different time
echo (a - b) ;
回答1:
Here is a much simpler solution:
<script language="javascript">
project.setProperty('startTime', new Date().getTime());
</script>
...
<script language="javascript">
project.setProperty('elapsedTime', new Date().getTime() - startTime)
</script>
<echo>Elapsed time: ${elapsedTime} ms</echo>
回答2:
Alternative to @LeFunes answer (and uses the tstamp
task)
<tstamp prefix="task.start">
<format property="millis" pattern="SSS"/>
</tstamp>
<tstamp prefix="task">
<format property="start" pattern="E, dd MMM YYYY hh:mm:ss"/>
</tstamp>
<time-consuming-task/>
<tstamp prefix="task.stop">
<format property="millis" pattern="SSS"/>
</tstamp>
<tstamp prefix="task">
<format property="stop" pattern="E, dd MMM YYYY hh:mm:ss"/>
</tstamp>
<script language="javascript">
project.setProperty("task.diff",
Math.abs(
(Date.parse(project.getProperty("task.stop")) +
+project.getProperty("task.stop.millis")) -
(Date.parse(project.getProperty("task.start")) +
+project.getProperty("task.start.millis"))))
</script>
<echo>
task completed in ${task.diff} ms
</echo>
NOTE:
this doesn't consider milliseconds
updated to consider the milliseconds
回答3:
<?xml version="1.0" encoding="UTF-8"?>
<project name="TEST ANT" default="test" basedir="..">
<target name="test" description="">
<script language="javascript"> <![CDATA[
var ts1 = new Date();
project.setProperty("current.time.1", ts1.toLocaleString());
project.setProperty("current.time.1.mill", ts1.getTime());
]]></script>
<echo>Timestamp 1: ${current.time.1} [${current.time.1.mill}]</echo>
<sleep milliseconds="1300"></sleep>
<script language="javascript"> <![CDATA[
var ts2 = new Date();
project.setProperty("current.time.2", ts2.toLocaleString());
project.setProperty("current.time.2.mill", ts2.getTime());
]]></script>
<echo>Timestamp 2: ${current.time.2} [${current.time.2.mill}]</echo>
<script language="javascript"> <![CDATA[
project.setProperty("res", project.getProperty("current.time.2.mill")-project.getProperty("current.time.1.mill"));
]]></script>
<echo>Diff: ${res}</echo>
</target>
</project>
回答4:
If you don't want to use JavaScript, you can use the Math task provided by the Ant-Contrib utilities.
The Ant-Contrib are fairly old, and I don't know if anyone is still maintaining them, but they're very popular to use in Ant build files since they add some very useful tasks.
I recommend including the ant-contrib-1.0b3.jar
into the project itself. When other people use your project, they'll also have the Ant-Contrib jar. I put ant-lib/ac/ant-contrib-1.0b3.jar
under your project's home directory. I use ant-lib
for all of my optional jars:
<project name="my.project"
...
xmlns:ac="antlib:net.sf.antcontrib">
...
<taskdef uri="antlib:net.sf.antcontrib"
resource="net/sf/antcontrib/antlib.xml">
<classpath>
<fileset dir="${basedir}/antlib/ac"/>
</classpath>
</taskdef>
....
Now, you can use your math task like this:
<ac:math result="time.diff"
operation="-"
operand1="${diff.time}"
operand2="${initial.time}"/>
The ac:
is an XML namespace that was declared in your <project/>
entity, and was connected to your tasks via the uri
parameter in the <taskdef/>
entity. This allows you to have multiple optional Ant tasks that may have tasks with duplicate names. This is a good idea in case you use multiple optional task libraries that have the same task names.
来源:https://stackoverflow.com/questions/11243787/get-timestamp-difference-using-ant