Ant: How to compare timestamps?

In a long Ant script, I have a target that gets called about once per second. (This is probably not very good, but let's accept it for now.)
I only want it to be executed if its last actual execution was at least five minutes ago.

One of the ideas for the solution is to save the property lastRunTimestampand compare the current time with this.

Problem: How to compare timestamps in Ant?

Another solution, which will also be welcomed, is a means of fulfilling the goal only at certain intervals so that verification is not required.

I am using Ant 1.7.1 and ant -contrib.

Any ideas are welcome - thanks!

+3
source share
1 answer

An interesting question, and one that is a bit more difficult to answer than I originally thought.

You can use the task <tstamp>to set the timestamp to five minutes:

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

Once you have this timestamp, you can use the last modified task condition <condition>to find out if a specific file has been updated since then. If you do not have a file, you can use the task <touch>to create it.

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

The only problem is that the default properties are immutable. After that you cannot change them. Fortunately, you use ant-contriband ant-contribto change this using a task variable.

+2
source

All Articles