// 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
//
// Ian Maclean (imaclean@gmail.com)
// Jaroslaw Kowalski (jkowalski@users.sourceforge.net)
using System;
using System.Collections;
using System.Globalization;
using System.IO;
using System.Reflection;
using NAnt.Core;
using NAnt.Core.Types;
using NAnt.Core.Attributes;
namespace NAnt.Core.Functions {
[FunctionSet("datetime", "Date/Time")]
public class DateTimeFunctions : FunctionSetBase {
#region Public Instance Constructors
public DateTimeFunctions(Project project, PropertyDictionary properties) : base(project, properties) {
}
#endregion Public Instance Constructors
#region Public Static Methods
///
/// Gets a that is the current local date and
/// time on this computer.
///
///
/// A whose value is the current date and time.
///
[Function("now")]
public static DateTime Now() {
return DateTime.Now;
}
///
/// Gets the year component of the specified date.
///
/// The date of which to get the year component.
///
/// The year, between 1 and 9999.
///
[Function("get-year")]
public static int GetYear(DateTime date) {
return date.Year;
}
///
/// Gets the month component of the specified date.
///
/// The date of which to get the month component.
///
/// The month, between 1 and 12.
///
[Function("get-month")]
public static int GetMonth(DateTime date) {
return date.Month;
}
///
/// Gets the day of the month represented by the specified date.
///
/// The date of which to get the day of the month.
///
/// The day value, between 1 and 31.
///
[Function("get-day")]
public static int GetDay(DateTime date) {
return date.Day;
}
///
/// Gets the hour component of the specified date.
///
/// The date of which to get the hour component.
///
/// The hour, between 0 and 23.
///
[Function("get-hour")]
public static int GetHour(DateTime date) {
return date.Hour;
}
///
/// Gets the minute component of the specified date.
///
/// The date of which to get the minute component.
///
/// The minute, between 0 and 59.
///
[Function("get-minute")]
public static int GetMinute(DateTime date) {
return date.Minute;
}
///
/// Gets the seconds component of the specified date.
///
/// The date of which to get the seconds component.
///
/// The seconds, between 0 and 59.
///
[Function("get-second")]
public static int GetSecond(DateTime date) {
return date.Second;
}
///
/// Gets the milliseconds component of the specified date.
///
/// The date of which to get the milliseconds component.
///
/// The millisecond, between 0 and 999.
///
[Function("get-millisecond")]
public static int GetMillisecond(DateTime date) {
return date.Millisecond;
}
///
/// Gets the number of ticks that represent the specified date.
///
/// The date of which to get the number of ticks.
///
/// The number of ticks that represent the date and time of the
/// specified date.
///
[Function("get-ticks")]
public static long GetTicks(DateTime date) {
return date.Ticks;
}
///
/// Gets the day of the week represented by the specified date.
///
/// The date of which to get the day of the week.
///
/// The day of the week, ranging from zero, indicating Sunday, to six,
/// indicating Saturday.
///
[Function("get-day-of-week")]
public static int GetDayOfWeek(DateTime date) {
return (int) date.DayOfWeek;
}
///
/// Gets the day of the year represented by the specified date.
///
/// The date of which to get the day of the year.
///
/// The day of the year, between 1 and 366.
///
[Function("get-day-of-year")]
public static int GetDayOfYear(DateTime date) {
return (int) date.DayOfYear;
}
///
/// Returns the number of days in the specified month of the specified
/// year.
///
/// The year.
/// The month (a number ranging from 1 to 12).
///
/// The number of days in for the specified
/// .
///
/// is less than 1 or greater than 12.
[Function("get-days-in-month")]
public static int GetDaysInMonth(int year, int month) {
return DateTime.DaysInMonth(year, month);
}
///
/// Returns an indication whether the specified year is a leap year.
///
/// A 4-digit year.
///
/// if is a leap year;
/// otherwise, .
///
[Function("is-leap-year")]
public static bool IsLeapYear(int year) {
return DateTime.IsLeapYear(year);
}
#endregion Public Static Methods
}
[FunctionSet("datetime", "Conversion")]
public class DateTimeConversionFunctions : FunctionSetBase {
#region Public Instance Constructors
public DateTimeConversionFunctions(Project project, PropertyDictionary properties) : base(project, properties) {
}
#endregion Public Instance Constructors
#region Public Static Methods
///
/// Converts the specified string representation of a date and time to
/// its equivalent.
///
/// A string containing a date and time to convert.
///
/// A equivalent to the date and time contained
/// in .
///
/// does not contain a valid string representation of a date and time.
///
/// The for the invariant culture is
/// used to supply formatting information about .
///
[Function("parse")]
public static DateTime Parse(string s) {
return DateTime.Parse(s, CultureInfo.InvariantCulture);
}
///
/// Converts the specified to its equivalent
/// string representation.
///
/// A to convert.
///
/// A string representation of formatted using
/// the general format specifier ("G").
///
///
/// is formatted with the
/// for the invariant culture.
///
[Function("to-string")]
public static string ToString(DateTime value) {
return value.ToString(CultureInfo.InvariantCulture);
}
#endregion Public Static Methods
}
}