// NAnt - A .NET build tool
// Copyright (C) 2001-2003 Gerry Shaw
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
// Gerry Shaw (gerry_shaw@yahoo.com)
// Chris Jenkin (oneinchhard@hotmail.com)
using System;
using System.Globalization;
using NAnt.Core.Attributes;
using NAnt.Core.Types;
namespace NAnt.Core.Tasks {
///
/// Sets properties with the current date and time.
///
///
///
/// By default the displays the current date
/// and time and sets the following properties:
///
///
/// - tstamp.date to yyyyMMdd
/// - tstamp.time to HHmm
/// - tstamp.now using the default DateTime.ToString() method
///
///
/// To set an additional property with a custom date/time use the
/// and attributes.
/// To set a number of additional properties with the exact same date and
/// time use the nested element (see example).
///
///
/// The date and time string displayed by the
/// uses the computer's default long date and time string format. You
/// might consider setting these to the
/// ISO 8601 standard
/// for date and time notation.
///
///
///
/// Set the build.date property.
///
///
/// ]]>
///
///
///
/// Set a number of properties for Ant like compatibility.
///
///
///
///
///
///
/// ]]>
///
///
[TaskName("tstamp")]
public class TStampTask : Task {
#region Private Instance Fields
private string _property = null;
private string _pattern = null;
private FormatterCollection _formatters = new FormatterCollection();
#endregion Private Instance Fields
#region Public Instance Properties
///
/// The property to receive the date/time string in the given pattern.
///
[TaskAttribute("property", Required=false)]
public string Property {
get { return _property; }
set { _property = value; }
}
/// The date/time pattern to be used.
///
/// The following table lists the standard format characters for each standard pattern. The format characters are case-sensitive; for example, 'g' and 'G' represent slightly different patterns.
///
///
/// Format Character
/// Description Example Format Pattern (en-US)
///
/// - dMM/dd/yyyy
/// - Ddddd, dd MMMM yyyy
/// - fdddd, dd MMMM yyyy HH:mm
/// - Fdddd, dd MMMM yyyy HH:mm:ss
/// - gMM/dd/yyyy HH:mm
/// - GMM/dd/yyyy HH:mm:ss
/// - m, MMMMM dd
/// - r, Rddd, dd MMM yyyy HH':'mm':'ss 'GMT'
/// - syyyy'-'MM'-'dd'T'HH':'mm':'ss
/// - tHH:mm
/// - THH:mm:ss
/// - uyyyy'-'MM'-'dd HH':'mm':'ss'Z'
/// - Udddd, dd MMMM yyyy HH:mm:ss
/// - y, Yyyyy MMMM
///
/// The following table lists the patterns that can be combined to construct custom patterns. The patterns are case-sensitive; for example, "MM" is recognized, but "mm" is not. If the custom pattern contains white-space characters or characters enclosed in single quotation marks, the output string will also contain those characters. Characters not defined as part of a format pattern or as format characters are reproduced literally.
///
///
/// Format
/// Pattern Description
///
/// - dThe day of the month. Single-digit days will not have a leading zero.
/// - ddThe day of the month. Single-digit days will have a leading zero.
/// - dddThe abbreviated name of the day of the week.
/// - ddddThe full name of the day of the week.
/// - MThe numeric month. Single-digit months will not have a leading zero.
/// - MMThe numeric month. Single-digit months will have a leading zero.
/// - MMMThe abbreviated name of the month.
/// - MMMMThe full name of the month.
/// - yThe year without the century. If the year without the century is less than 10, the year is displayed with no leading zero.
/// - yyThe year without the century. If the year without the century is less than 10, the year is displayed with a leading zero.
/// - yyyyThe year in four digits, including the century.
/// - ggThe period or era. This pattern is ignored if the date to be formatted does not have an associated period or era string.
/// - hThe hour in a 12-hour clock. Single-digit hours will not have a leading zero.
/// - hhThe hour in a 12-hour clock. Single-digit hours will have a leading zero.
/// - HThe hour in a 24-hour clock. Single-digit hours will not have a leading zero.
/// - HHThe hour in a 24-hour clock. Single-digit hours will have a leading zero.
/// - mThe minute. Single-digit minutes will not have a leading zero.
/// - mmThe minute. Single-digit minutes will have a leading zero.
/// - sThe second. Single-digit seconds will not have a leading zero.
/// - ssThe second. Single-digit seconds will have a leading zero.
/// - fThe fraction of a second in single-digit precision. The remaining digits are truncated.
/// - ffThe fraction of a second in double-digit precision. The remaining digits are truncated.
/// - fffThe fraction of a second in three-digit precision. The remaining digits are truncated.
/// - ffffThe fraction of a second in four-digit precision. The remaining digits are truncated.
/// - fffffThe fraction of a second in five-digit precision. The remaining digits are truncated.
/// - ffffffThe fraction of a second in six-digit precision. The remaining digits are truncated.
/// - fffffffThe fraction of a second in seven-digit precision. The remaining digits are truncated.
/// - tThe first character in the AM/PM designator.
/// - ttThe AM/PM designator.
/// - zThe time zone offset ("+" or "-" followed by the hour only). Single-digit hours will not have a leading zero. For example, Pacific Standard Time is "-8".
/// - zzThe time zone offset ("+" or "-" followed by the hour only). Single-digit hours will have a leading zero. For example, Pacific Standard Time is "-08".
/// - zzzThe full time zone offset ("+" or "-" followed by the hour and minutes). Single-digit hours and minutes will have leading zeros. For example, Pacific Standard Time is "-08:00".
/// - :The default time separator.
/// - /The default date separator.
/// - \ cPattern Where c is any character. Displays the character literally. To display the backslash character, use "\\".
///
///
[TaskAttribute("pattern", Required=false)]
public string Pattern {
get { return _pattern; }
set { _pattern = value; }
}
[BuildElementArray("formatter")]
public FormatterCollection Formatters {
get { return _formatters; }
}
#endregion Public Instance Properties
#region Override implementation of Task
protected override void ExecuteTask() {
// get and print current date
DateTime now = DateTime.Now;
Log(Level.Info, "{0} {1}.", now.ToLongDateString(), now.ToLongTimeString());
// set default properties
Properties["tstamp.date"] = now.ToString("yyyyMMdd", CultureInfo.InvariantCulture);
Properties["tstamp.time"] = now.ToString("HHmm", CultureInfo.InvariantCulture);
Properties["tstamp.now"] = now.ToString(CultureInfo.InvariantCulture);
// set custom property
if (_property != null && _pattern != null) {
Properties[_property] = now.ToString(_pattern, CultureInfo.InvariantCulture);
Log(Level.Verbose, "{0} = {1}.", _property, Properties[_property].ToString(CultureInfo.InvariantCulture));
}
// set properties set in formatters nested elements
foreach (Formatter f in Formatters) {
if (IfDefined && !UnlessDefined) {
Properties[f.Property] = now.ToString(f.Pattern, CultureInfo.InvariantCulture);
Log(Level.Verbose, "{0} = {1}.", f.Property, Properties[f.Property].ToString(CultureInfo.InvariantCulture));
}
}
}
#endregion Override implementation of Task
}
}