Ant: How to compare timestamps?

ant, ant-contrib

Solution

Interesting question, and one which is a bit harder to answer than I originally thought.

You can use the `<tstamp>` task to set a time stamp that's five minutes old:

<tstamp>
  <format property="time_stamp"
      offset="-5"
      unit="minutes"
      pattern="MM/dd/yyyy hh:mm:ss aa"/>
</tstamp>

Once you have that timestamp, you can use the lastmodified condition of the `<condition>` task to see if a particular file has been updated since. If you don't have a file, you can use the `<touch>` task to create one.

<condition property="has.been.modified">
     <islastmodified dateTime="${time_stamp}" mode="after">
        <file file="${touch.file}"/>
     </islastmodified>
</condition>

The only issue is that default properties are immutable. Once set, you can't change them. Fortunately you're using `ant-contrib` and `ant-contrib` allows you to change that via the variable task.

Problem

In a long running Ant script, I have a target that gets called roughly once per second. (This is probably not a good thing, but let's accept it for the moment.) I only want it to execute if its last actual execution was at least five minutes ago. One idea for a solution is to maintain a `lastRunTimestamp` property, and compare the current time to that. Problem: How can I compare timestamps in Ant? Another solution that would also be welcome is a means of executing the target only at specified time intervals, so that the check would not be necessary. I am using Ant 1.7.1 and ant-contrib. Any ideas are greatly appreciated - thanks!

Original source

Related problems