Problems with ant tstamp pattern - always a leading 0 - ant

Take a look at the following portion of my ant script:
<tstamp>
<format property="SHORT_DAY" pattern="d MMM yyyy"/>
<format property="SHORT_DAY_FORMATTED" pattern="d MMM yyyy"/>
</tstamp>
<echo message="${SHORT_DAY}"/>
<echo message="${SHORT_DAY_FORMATTED}"/>
Today is the 4th January 2013, so I expect the output to be:
[echo] 4 Jan 2013
[echo] 4 Jan, 2013
But it is not. What I get is:
[echo] 04 Jan 2013
[echo] 4 Jan, 2013
Note the leading 0 in the first echo. For some reason, ant always prints it with a leading zero. The only difference between the 2 properties is that there is a comma in between the month and year.
Does anyone know what this is happening, and more importantly, how to stop the leading 0?
Ant version is 1.7.0

It turns out that I had SHORT_DAY defined earlier in the ant file (yes, it's a large file!)
<format property="SHORT_DAY" pattern="dd MMM yyyy"/>
And since ANT doesn't let you override properties, my second definition (the one I expected to be used) was being ignored.
Chalk it down to another case of human error :(

Related

Build variable $(DayOfYear) with preceding zeros?

In VSTS (or TFS 2015) does the $(DayOfYear) build variable output with preceding zeros when under 100?
For example, would it output 063 or 63? Similarly, 003 or 3?
The reason I ask is because we use the following build number format:
$(Major).$(Minor).$(Year:yy)$(DayOfYear)$(Rev:.rr)
Technically, without preceeding zeros, 1.0.16179.01 (a build from 27th June 2016) would be considered as a later build than 1.0.173.01 (a build on 3rd Jan 2017). With preceding zeros, this version number would be correctly represented as 1.0.17003.01.
$(DayOfYear) always output a number with XXX format. So you will get "003" or "063" with zero filled.

write Regex to find match

I never wrote any complex regular expression before, and what I need seems to be (at least) a bit complicated.
I need a Regex to find matches for the following:
"On Fri, Jan 16, 2015 at 4:39 PM"
Where On will always be there;
then 3 characters for week day;
, is always there;
space is always there;
then 3 characters for month name;
space is always there;
day of month (one or two numbers);
, is always there;
space is always there;
4 numbers for year;
space at space always there;
time (have to match 4:39 as well as 10:39);
space and 2 caps letters for AM or PM.
Here's a very simple and readable one:
/On \w{3}, \w{3} \d{1,2}, \d{4} at \d{1,2}:\d{2} [AP]M/
See it on rubular
Try this:
On\s+(?:Mon|Tue|Wed|Thu|Fri|Sat|Sun), (?:Jan|Feb|Mar|Apr|May|June|July|Aug|Sept|Oct|Nov|Dec) \d{1,2}, \d{4} at \d{1,2}:\d{2} (?:AM|PM)
/On \w{3}, \w{3} \d{1,2}, \d{4} at \d{1,2}:\d{1,2} [A-Z]{2}/
# \w{3} for 3 charecters
# \d{1,2} for a or 2 digits
# \d{4} for 4 digits
# [A-Z]{2} for 2 capital leters
You could try the below regex and it won't check for the month name or day name or date.
^On\s[A-Z][a-z]{2},\s[A-Z][a-z]{2}\s\d{1,2},\s\d{4}\sat\s(?:10|4):39\s[AP]M$
DEMO
You can use Rubular to construct and test Ruby Regular Expressions.
I have put together an Example: http://rubular.com/r/45RIiwheqs
Since it looks you try to parse dates, you should use Date.strptime.
/On [A-Za-z]{3}, [A-Za-z]{3} \d{1,2}, \d{4} at \d{1,2}:\d{1,2}/g
The way you are describing the problem makes me thing that the format will always be preserved.
I would then in your case use the Time.parse function, passing the format string
format = "On %a, %b"On Fri, Jan 16, 2015 at 4:39 PM", format)
which is more readable than a regexp (in my opinion) and has the added value that it returns a Time object, which is easier to use than a regexp match, in case you need to perform other time-based calculations.
Another good thing is that if the string contains an invalid date (like "On Mon, Jan 59, 2015 at 37:99 GX" ) the parse function will raise an exception, so that validation is done for free for you.

How to add the AM or PM to time date format in ANT task?

I have a date and time stamp that I would like to add the AM or PM value to. I haven't found the way to do this. Here is my code:
<target name="updateDate">
<propertyset id="tdate"></propertyset>
<tstamp>
<format property="tdate" pattern="MM-dd-yyyy HH:mm"/>
</tstamp>
<echo>Updating build date to: ${tdate}</echo>
<replaceregexp byline="true">
<regexp pattern="BUILD_DATE:String\s+=\s+(['|"])([a-zA-Z0-9-:\s]+)(['|"])"/>
<substitution expression="BUILD_DATE:String = \1${tdate}\3"/>
<fileset dir="project/src/model">
<include name="ProjectModel.as"/>
</fileset>
</replaceregexp>
</target>
Straight from the documentation:
a Am/pm marker Text PM
So
<format property="tdate" pattern="MM-dd-yyyy HH:mm a"/>
Note that this is redundant with HH, since HH prints the hour from 00 to 23. Use hh to get the hour from 01 to 12.

Ant add trailing zeros

Hello i have a variable in Ant: ${env.BUILD_NUMBER}
And i wan't that this variable is always 6 characters long.
If not Ant should add trailing zeros.
1 should be 100000
39 should be 390000
5656 should be 565600
How is this possible using a Ant build script?
Thanks!
There is a solution using the PropertyFile Ant task described here: Re: Zero-Padding an Int Ant Property
You may also want to checkout the propertyregex task from Ant Contrib. See Replacing characters in Ant property

Can an Ant target depend on completion of one of its dependencies, but not both?

I'm trying to make an Ant target that runs if ONE of two other targets completes. Basically, assuming I have three targets A1, A2, and B, I want B to run only if A1 OR A2 run. A1 and A2 depend on a condition, so either A1 or A2 will run (but never both).
For example:
<target name="A1" if="${conditionalVar}">
<target name="A2" unless="${conditionalVar}">
<target name="B" depends="????????">
What should the 'depends' for target B be? Is there anyway to do this?
Yes, such a configuration is possible and not very complicated:
The trick is to set a property that will be tested if set (e.g. call it "taskA1.use").
<target name="A1" if="taskA1.use" />
<target name="A2" unless="taskA1.use" />
<target name="B" depends="A1,A2" />
Therefore even if B depends on both tasks A1 and A2 only one will be executed depending of the property "taskA1.use" has been set or not.

Resources