480 lines
16 KiB
C#
480 lines
16 KiB
C#
//----------------------------------------------------------------------------
|
|
//
|
|
// Copyright (c) Intel Corporation, 2008 - 2009 All Rights Reserved.
|
|
//
|
|
// File: CimDateTime.cs
|
|
//
|
|
// Contents: CimDateTime is a part of the CimFramework project.
|
|
// It contains the implementation for all CIM date classes.
|
|
//
|
|
//----------------------------------------------------------------------------
|
|
using System;
|
|
using System.Xml;
|
|
|
|
namespace Intel.Manageability.Cim.Typed
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
/// Base class for all CIM date classes.
|
|
/// </summary>
|
|
public abstract class CimDateTime
|
|
{
|
|
const string INTERVAL = "Interval";
|
|
const string DATETIME = "Datetime";
|
|
|
|
/// <summary>
|
|
/// Enum that defines the date time type.
|
|
/// </summary>
|
|
public enum CimDateTimeType
|
|
{
|
|
/// <summary>
|
|
/// Represents the datetime data type when used as an interval value.
|
|
/// </summary>
|
|
DATE_TIME,
|
|
/// <summary>
|
|
/// Represents the datetime data type when used as a dateTime value.
|
|
/// </summary>
|
|
INTERVAL,
|
|
//DATE,
|
|
//TIME,
|
|
//CIM_DATETIME
|
|
}
|
|
|
|
|
|
#region PUBLIC_DATA_MEMBERS
|
|
|
|
|
|
/// <summary>
|
|
/// Retrieves the years portion of the interval.
|
|
/// </summary>
|
|
public int Years { get; set; }
|
|
|
|
/// <summary>
|
|
/// Retrieves the months portion of the interval.
|
|
/// </summary>
|
|
public int Months { get; set; }
|
|
|
|
/// <summary>
|
|
/// Retrieves the days portion of the interval.
|
|
/// </summary>
|
|
public int Days { get; set; }
|
|
|
|
/// <summary>
|
|
/// Retrieves the hours portion of the interval.
|
|
/// </summary>
|
|
public int Hours { get; set; }
|
|
|
|
/// <summary>
|
|
/// Retrieves the microseconds portion of the interval.
|
|
/// </summary>
|
|
public int Microseconds { get; set; }
|
|
|
|
/// <summary>
|
|
/// Retrieves the minutes portion of the interval.
|
|
/// </summary>
|
|
public int Minutes { get; set; }
|
|
|
|
/// <summary>
|
|
/// Retrieves the seconds portion of the interval.
|
|
/// </summary>
|
|
public int Seconds { get; set; }
|
|
|
|
/// <summary>
|
|
/// The concrete type of the date time.
|
|
/// </summary>
|
|
public CimDateTimeType Type { get; private set; }
|
|
|
|
|
|
#endregion PUBLIC_DATA_MEMBERS
|
|
|
|
|
|
#region PRIVATE_DATA_MEMBERS
|
|
|
|
|
|
/// <summary>
|
|
/// The type name of the object.
|
|
/// </summary>
|
|
private string Name { get; set; }
|
|
|
|
/// <summary>
|
|
/// The object as XML.
|
|
/// </summary>
|
|
private XmlDocument XmlDoc { get; set; }
|
|
|
|
/// <summary>
|
|
/// The namespace of the CimDateTime object.
|
|
/// </summary>
|
|
private const string CIMDATETIME_NAMESPACE = "http://schemas.dmtf.org/wbem/wscim/1/common";
|
|
|
|
|
|
#endregion PRIVATE_DATA_MEMBERS
|
|
|
|
/// <summary>
|
|
/// Constructor to be used by the derived classes.
|
|
/// </summary>
|
|
/// <param name="type">Type of the class</param>
|
|
protected CimDateTime(CimDateTimeType type)
|
|
{
|
|
XmlDoc = new XmlDocument();
|
|
Name = (type == CimDateTimeType.DATE_TIME) ? DATETIME : INTERVAL;
|
|
Type = type;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Converts a string into the proper date time class.
|
|
/// </summary>
|
|
/// <param name="dateTimeString">XML string that represents the date time</param>
|
|
/// <returns>Appropriate CimDateTime object</returns>
|
|
public static CimDateTime Parse(String dateTimeString)
|
|
{
|
|
XmlDocument doc = new XmlDocument();
|
|
doc.LoadXml(dateTimeString);
|
|
if (doc.FirstChild.LocalName.Equals(INTERVAL, StringComparison.CurrentCultureIgnoreCase))
|
|
{
|
|
return new CimDateTimeInterval(doc.InnerText);
|
|
}
|
|
else// "dateTime"
|
|
{
|
|
return new CIMDateTimeAbsolute(doc.InnerText);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Method that returns the outer XML string that represents the object.
|
|
/// </summary>
|
|
/// <returns>An XML string that represents the object</returns>
|
|
public override string ToString()
|
|
{
|
|
XmlElement root = XmlDoc.CreateElement(Name, CIMDATETIME_NAMESPACE);
|
|
root.InnerText = StringValue();
|
|
return root.OuterXml;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Method that returns the inner XML string (without the root tag) that represents the object.
|
|
/// </summary>
|
|
/// <returns>Inner XML string that represents the object</returns>
|
|
public abstract string StringValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// This class represents the datetime data type when used as a time interval value.
|
|
/// It is in the format: PnYnMnDTnHnMnS
|
|
/// where:
|
|
/// 'P' - is the date beginner
|
|
/// nY - represents the number of years
|
|
/// nM - the number of months
|
|
/// nD - the number of days
|
|
/// 'T' - is the date/time separator
|
|
/// nH - the number of hours
|
|
/// nM - the number of minutes
|
|
/// nS - the number of seconds. The number of seconds can include decimal digits to an arbitrary precision.
|
|
/// Example:
|
|
/// P1Y2M3DT10H30M - define duration of 1 year, 2 months, 3 days, 10 hours, and 30 minutes
|
|
/// </summary>
|
|
public class CimDateTimeInterval : CimDateTime
|
|
{
|
|
|
|
/// <summary>
|
|
/// Constructs a CimDateTimeInterval using the individual values of the interval.
|
|
/// </summary>
|
|
/// <param name="years">int, the years portion of the interval</param>
|
|
/// <param name="months">int, the months portion of the interval</param>
|
|
/// <param name="days">int, the days portion of the interval</param>
|
|
/// <param name="hours">int, the hours portion of the interval</param>
|
|
/// <param name="minutes">int, the minutes portion of the interval</param>
|
|
/// <param name="seconds">int, the seconds portion of the interval</param>
|
|
/// <param name="microseconds">int, the microseconds portion of the interval</param>
|
|
public CimDateTimeInterval(int years, int months, int days, int hours, int minutes, int seconds, int microseconds)
|
|
: base(CimDateTimeType.INTERVAL)
|
|
{
|
|
Years = years;
|
|
Months = months;
|
|
Days = days;
|
|
Hours = hours;
|
|
Minutes = minutes;
|
|
Seconds = seconds;
|
|
Microseconds = microseconds;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Constructs a CimDateTimeInterval object using a string.
|
|
/// </summary>
|
|
/// <param name="intervalString">A string in the format of PnYnMnDTnHnMnS </param>
|
|
public CimDateTimeInterval(string intervalString)
|
|
: base(CimDateTimeType.INTERVAL)
|
|
{
|
|
if (intervalString == null)
|
|
throw new ArgumentNullException("intervalString");
|
|
try
|
|
{
|
|
// Parse the interval string into the data members
|
|
string interval = intervalString;
|
|
interval = interval.Split('P')[1];
|
|
string modinterval = interval;
|
|
|
|
if (modinterval.Contains("T"))
|
|
modinterval = interval.Split('T')[0];
|
|
{
|
|
if (modinterval.IndexOf("Y") != -1)
|
|
{
|
|
Years = Convert.ToInt32(modinterval.Split('Y')[0]);
|
|
modinterval = modinterval.Split('Y')[1];
|
|
}
|
|
if (modinterval.IndexOf("M") != -1)
|
|
{
|
|
Months = Convert.ToInt32(modinterval.Split('M')[0]);
|
|
modinterval = modinterval.Split('M')[1];
|
|
}
|
|
if (modinterval.IndexOf("D") != -1)
|
|
{
|
|
Days = Convert.ToInt32(modinterval.Split('D')[0]);
|
|
modinterval = modinterval.Split('D')[1];
|
|
}
|
|
|
|
if (intervalString.Contains("T"))
|
|
{
|
|
modinterval = interval.Split('T')[1];
|
|
if (modinterval.IndexOf("H") != -1)
|
|
{
|
|
Hours = Convert.ToInt32(modinterval.Split('H')[0]);
|
|
modinterval = modinterval.Split('H')[1];
|
|
}
|
|
if (modinterval.IndexOf("M") != -1)
|
|
{
|
|
Minutes = Convert.ToInt32(modinterval.Split('M')[0]);
|
|
modinterval = modinterval.Split('M')[1];
|
|
}
|
|
if (modinterval.IndexOf("S") != -1)
|
|
{
|
|
Seconds = Convert.ToInt32(modinterval.Split('S')[0]);
|
|
modinterval = modinterval.Split('S')[1];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (IndexOutOfRangeException)
|
|
{
|
|
throw new FormatException("Input intervalString is in an invalid format.");
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Converts a string into a CimDateTimeInterval object.
|
|
/// </summary>
|
|
/// <param name="intervalString">XML string that represents an interval</param>
|
|
/// <returns>CimDateTimeInterval object</returns>
|
|
public new static CimDateTimeInterval Parse(String intervalString)
|
|
{
|
|
return new CimDateTimeInterval(intervalString);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Method that returns an inner XML string (without the root tag) representing the object.
|
|
/// </summary>
|
|
/// <returns>Inner XML string that represents the object</returns>
|
|
public override string StringValue()
|
|
{
|
|
string interval = "P";
|
|
if (Years != 0)
|
|
interval += Years.ToString() + "Y";
|
|
if (Months != 0)
|
|
{
|
|
if (Months < 10)
|
|
interval += "0" + Months.ToString() + "M";
|
|
else
|
|
interval += Months.ToString() + "M";
|
|
}
|
|
if (Days != 0)
|
|
{
|
|
if (Days < 10)
|
|
interval += "0" + Days.ToString() + "D";
|
|
else
|
|
interval += Days.ToString() + "D";
|
|
}
|
|
interval += "T";
|
|
|
|
|
|
//if (Hours != 0)
|
|
{
|
|
if (Hours <= 10)
|
|
interval += "0" + Hours.ToString() + "H";
|
|
else
|
|
interval += Hours.ToString() + "H";
|
|
}
|
|
|
|
|
|
//if (Minutes != 0)
|
|
{
|
|
if (Minutes <= 10)
|
|
interval += "0" + Minutes.ToString() + "M";
|
|
else
|
|
interval += Minutes.ToString() + "M";
|
|
}
|
|
|
|
//if (Seconds != 0)
|
|
{
|
|
if (Seconds <= 10)
|
|
interval += "0" + Seconds.ToString() + "S";
|
|
else
|
|
interval += Seconds.ToString() + "S";
|
|
}
|
|
|
|
return interval;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// This class represents the datetime data type when used as an absolute dateTime value.
|
|
/// It is in the format: YYYY-MM-DDThh:mm:00Z
|
|
/// where:
|
|
/// YYYY - four digits that represent the year
|
|
/// '-' - is a separator between parts of the date portion
|
|
/// MM - two-digits that represent the month
|
|
/// DD - two-digits that represent the day
|
|
/// 'T' - is a separator between the date and time-of-day
|
|
/// hh - two-digits that represent the hour
|
|
/// ':' - is a separator between parts of the time-of-day portion
|
|
/// mm - two-digits that represent the minutes
|
|
/// 00 - the number of seconds (must be set to zero - restricted by FW)
|
|
/// Z - represents the time zone
|
|
/// Example:
|
|
/// 2012-12-14T11:54:00Z - defines the date of December 14th, 2012, time-of-day 11:54
|
|
/// For more details see object dateTime in XML schema part 2.
|
|
/// </summary>
|
|
public class CIMDateTimeAbsolute : CimDateTime
|
|
{
|
|
#region DATA_MEMBERS
|
|
|
|
|
|
|
|
|
|
#endregion DATA_MEMBERS
|
|
|
|
|
|
/// <summary>
|
|
/// Constructs a CimDateTimeAbsolute using the individual values of the dateTime.
|
|
/// </summary>
|
|
/// <param name="years">int, the years portion of the interval</param>
|
|
/// <param name="months">int, the months portion of the interval</param>
|
|
/// <param name="days">int, the days portion of the interval</param>
|
|
/// <param name="hours">int, the hours portion of the interval</param>
|
|
/// <param name="minutes">int, the minutes portion of the interval</param>
|
|
public CIMDateTimeAbsolute(int years, int months, int days, int hours, int minutes)
|
|
: base(CimDateTimeType.DATE_TIME)
|
|
{
|
|
Years = years;
|
|
Months = months;
|
|
Days = days;
|
|
Hours = hours;
|
|
Minutes = minutes;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Constructs a CIMDateTimeAbsolute using the individual values of the dateTime.
|
|
/// </summary>
|
|
/// <param name="dateTime">Struct which represents an instant in time</param>
|
|
public CIMDateTimeAbsolute(DateTime dateTime)
|
|
: base(CimDateTimeType.DATE_TIME)
|
|
{
|
|
|
|
Years = dateTime.Year;
|
|
Months = dateTime.Month;
|
|
Days = dateTime.Day;
|
|
Hours = dateTime.Hour;
|
|
Minutes = dateTime.Minute;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Constructs a CIMDateTimeAbsolute object from a string.
|
|
/// </summary>
|
|
/// <param name="dateTimeString">A string in the format of YYYY-MM-DDThh:mm:00Z </param>
|
|
public CIMDateTimeAbsolute(string dateTimeString)
|
|
: base(CimDateTimeType.DATE_TIME)
|
|
{
|
|
if (dateTimeString == null)
|
|
throw new ArgumentNullException("dateTimeString");
|
|
|
|
try
|
|
{
|
|
//parse the dateTimeString string into the data members
|
|
string[] dateTimeArr = (dateTimeString.Split('T')[0]).Split('-');
|
|
Years = Convert.ToInt32(dateTimeArr[0]);
|
|
Months = Convert.ToInt32(dateTimeArr[1]);
|
|
Days = Convert.ToInt32(dateTimeArr[2]);
|
|
|
|
string[] timeOfDayArr = (dateTimeString.Split('T')[1]).Split(':');
|
|
Hours = Convert.ToInt32(timeOfDayArr[0]);
|
|
Minutes = Convert.ToInt32(timeOfDayArr[1]);
|
|
}
|
|
catch (IndexOutOfRangeException)
|
|
{
|
|
throw new FormatException("Invalid format of the dateTimeString input.");
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// Converts a string into a CIMDateTimeAbsolute object.
|
|
/// </summary>
|
|
/// <param name="dateTimeString">XML string that represents an absolute date time</param>
|
|
/// <returns>CIMDateTime Absolute object</returns>
|
|
public new static CIMDateTimeAbsolute Parse(String dateTimeString)
|
|
{
|
|
return new CIMDateTimeAbsolute(dateTimeString);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Create a string from the data members.
|
|
/// </summary>
|
|
/// <returns>A string in the format of YYYY-MM-DDThh:mm:00Z</returns>
|
|
public override string StringValue()
|
|
{
|
|
string datetime = "";
|
|
if (Years != 0)
|
|
datetime += Years.ToString() + "-";
|
|
if (Months != 0)
|
|
{
|
|
if (Months < 10)
|
|
datetime += "0" + Months.ToString() + "-";
|
|
else
|
|
datetime += Months.ToString() + "-";
|
|
|
|
}
|
|
if (Days != 0)
|
|
{
|
|
if (Days < 10)
|
|
datetime += "0" + Days.ToString();
|
|
else
|
|
datetime += Days.ToString();
|
|
}
|
|
datetime += "T";
|
|
|
|
// An hour can be 0
|
|
if (Hours < 10)
|
|
datetime += "0" + Hours.ToString() + ":";
|
|
else
|
|
datetime += Hours.ToString() + ":";
|
|
|
|
// A minute can be 0
|
|
if (Minutes < 10)
|
|
datetime += "0" + Minutes.ToString() + ":";
|
|
else
|
|
datetime += Minutes.ToString() + ":";
|
|
|
|
datetime += "00Z";
|
|
|
|
return datetime;
|
|
}
|
|
}
|
|
|
|
}
|