//----------------------------------------------------------------------------
//
// 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
{
///
/// Base class for all CIM date classes.
///
public abstract class CimDateTime
{
const string INTERVAL = "Interval";
const string DATETIME = "Datetime";
///
/// Enum that defines the date time type.
///
public enum CimDateTimeType
{
///
/// Represents the datetime data type when used as an interval value.
///
DATE_TIME,
///
/// Represents the datetime data type when used as a dateTime value.
///
INTERVAL,
//DATE,
//TIME,
//CIM_DATETIME
}
#region PUBLIC_DATA_MEMBERS
///
/// Retrieves the years portion of the interval.
///
public int Years { get; set; }
///
/// Retrieves the months portion of the interval.
///
public int Months { get; set; }
///
/// Retrieves the days portion of the interval.
///
public int Days { get; set; }
///
/// Retrieves the hours portion of the interval.
///
public int Hours { get; set; }
///
/// Retrieves the microseconds portion of the interval.
///
public int Microseconds { get; set; }
///
/// Retrieves the minutes portion of the interval.
///
public int Minutes { get; set; }
///
/// Retrieves the seconds portion of the interval.
///
public int Seconds { get; set; }
///
/// The concrete type of the date time.
///
public CimDateTimeType Type { get; private set; }
#endregion PUBLIC_DATA_MEMBERS
#region PRIVATE_DATA_MEMBERS
///
/// The type name of the object.
///
private string Name { get; set; }
///
/// The object as XML.
///
private XmlDocument XmlDoc { get; set; }
///
/// The namespace of the CimDateTime object.
///
private const string CIMDATETIME_NAMESPACE = "http://schemas.dmtf.org/wbem/wscim/1/common";
#endregion PRIVATE_DATA_MEMBERS
///
/// Constructor to be used by the derived classes.
///
/// Type of the class
protected CimDateTime(CimDateTimeType type)
{
XmlDoc = new XmlDocument();
Name = (type == CimDateTimeType.DATE_TIME) ? DATETIME : INTERVAL;
Type = type;
}
///
/// Converts a string into the proper date time class.
///
/// XML string that represents the date time
/// Appropriate CimDateTime object
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);
}
}
///
/// Method that returns the outer XML string that represents the object.
///
/// An XML string that represents the object
public override string ToString()
{
XmlElement root = XmlDoc.CreateElement(Name, CIMDATETIME_NAMESPACE);
root.InnerText = StringValue();
return root.OuterXml;
}
///
/// Method that returns the inner XML string (without the root tag) that represents the object.
///
/// Inner XML string that represents the object
public abstract string StringValue();
}
///
/// 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
///
public class CimDateTimeInterval : CimDateTime
{
///
/// Constructs a CimDateTimeInterval using the individual values of the interval.
///
/// int, the years portion of the interval
/// int, the months portion of the interval
/// int, the days portion of the interval
/// int, the hours portion of the interval
/// int, the minutes portion of the interval
/// int, the seconds portion of the interval
/// int, the microseconds portion of the interval
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;
}
///
/// Constructs a CimDateTimeInterval object using a string.
///
/// A string in the format of PnYnMnDTnHnMnS
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.");
}
}
///
/// Converts a string into a CimDateTimeInterval object.
///
/// XML string that represents an interval
/// CimDateTimeInterval object
public new static CimDateTimeInterval Parse(String intervalString)
{
return new CimDateTimeInterval(intervalString);
}
///
/// Method that returns an inner XML string (without the root tag) representing the object.
///
/// Inner XML string that represents the object
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;
}
}
///
/// 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.
///
public class CIMDateTimeAbsolute : CimDateTime
{
#region DATA_MEMBERS
#endregion DATA_MEMBERS
///
/// Constructs a CimDateTimeAbsolute using the individual values of the dateTime.
///
/// int, the years portion of the interval
/// int, the months portion of the interval
/// int, the days portion of the interval
/// int, the hours portion of the interval
/// int, the minutes portion of the interval
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;
}
///
/// Constructs a CIMDateTimeAbsolute using the individual values of the dateTime.
///
/// Struct which represents an instant in time
public CIMDateTimeAbsolute(DateTime dateTime)
: base(CimDateTimeType.DATE_TIME)
{
Years = dateTime.Year;
Months = dateTime.Month;
Days = dateTime.Day;
Hours = dateTime.Hour;
Minutes = dateTime.Minute;
}
///
/// Constructs a CIMDateTimeAbsolute object from a string.
///
/// A string in the format of YYYY-MM-DDThh:mm:00Z
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.");
}
}
///
/// Converts a string into a CIMDateTimeAbsolute object.
///
/// XML string that represents an absolute date time
/// CIMDateTime Absolute object
public new static CIMDateTimeAbsolute Parse(String dateTimeString)
{
return new CIMDateTimeAbsolute(dateTimeString);
}
///
/// Create a string from the data members.
///
/// A string in the format of YYYY-MM-DDThh:mm:00Z
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;
}
}
}