Monday, January 4, 2010

Nant task to calculate GMT or UTC timestamp

I was surprised when i realized that standard implementation of <tstamp> task do not support getting time in GMT time zone. I have not found any custom extensions for nant, which will provide such functionality so i was forced to write my own custom task. Fortunately it is really easy:

    <script language="C#" prefix="test" >
<code>
<![CDATA[
[TaskName("gmttimestamp")]
public class gmttimestamp : Task
{
private string _pattern;
private string _property;

[TaskAttribute("pattern", Required = true)]
public string Pattern
{
get { return _pattern; }
set { _pattern = value; }
}

[TaskAttribute("property", Required = true)]
public string Property
{
get { return _property; }
set { _property = value; }
}

protected override void ExecuteTask()
{
string gmtTime = DateTime.UtcNow.ToString(_pattern);
Properties.Add(_property, gmtTime);

Log(Level.Info, "[gmttimestamp] :" + gmtTime);
}
}
]]>
</code>
</script>

Usage in Nant build script:

 <gmttimestamp property='today' pattern="yyMMdd"/>
<gmttimestamp property='start.selenium.timestamp' pattern="HH:mm:ss"/>

Fully integrated task can be found here: http://code.google.com/p/design-of-selenium-tests-for-asp-net/source/browse/trunk/_build/scripts/continuous/main.build

0 comments: