/// <summary>
/// Time 结构表示一天中的某个时间点,精确到秒。与 DateTime 不同,Time 结构不包含日期部分。
/// </summary>
[Serializable]
public struct Time : ISerializable, IXmlSerializable
{
private int _hour;
private int _minute;
private int _second;
/// <summary>
/// 从 TimeSpan 构造 Time,例如,使用 DateTime.TimeOfDay 作为实参。
/// </summary>
/// <param name="timeSpan"></param>
public Time(TimeSpan timeSpan)
: this(timeSpan.Hours, timeSpan.Minutes, timeSpan.Minutes)
{
}
/// <summary>
/// 构造 Time 的新实例。秒部分为 0。
/// </summary>
/// <param name="hour">小时部分,0 - 23</param>
/// <param name="minute">分钟部分,0 - 59</param>
public Time(int hour, int minute)
: this(hour, minute, 0)
{
}
/// <summary>
/// 构造 Time 的新实例。
/// </summary>
/// <param name="hour">小时部分,0 - 23</param>
/// <param name="minute">分钟部分,0 - 59</param>
/// <param name="second">秒部分,0 - 59</param>
public Time(int hour, int minute, int second)
{
if (hour < 0 || hour > 23)
{
throw new ArgumentOutOfRangeException("hour");
}
if (minute < 0 || minute > 59)
{
throw new ArgumentOutOfRangeException("minute");
}
if (second < 0 || minute > 59)
{
throw new ArgumentOutOfRangeException("second");
}
_hour = hour;
_minute = minute;
_second = second;
}
public Time(SerializationInfo info, StreamingContext context)
{
_hour = info.GetInt32("Hour");
_minute = info.GetInt32("Minute");
_second = info.GetInt32("Second");
}
/// <summary>
/// 小时部分
/// </summary>
public int Hour
{
get
{
return _hour;
}
}
/// <summary>
/// 分钟部分
/// </summary>
public int Minute
{
get
{
return _minute;
}
}
/// <summary>
/// 秒部分
/// </summary>
public int Second
{
get
{
return _second;
}
}
public override bool Equals(object obj)
{
if (obj == null || obj.GetType() != typeof(Time))
{
return false;
}
Time other = (Time)obj;
return _hour == other._hour
&& _minute == other._minute
&& _second == other._second;
}
public override int GetHashCode()
{
return _hour.GetHashCode()
^ _minute.GetHashCode()
^ _second.GetHashCode();
}
public static bool operator==(Time a, Time b)
{
return a._hour == b._hour
&& a._minute == b._minute
&& a._second == b._second;
}
public static bool operator !=(Time a, Time b)
{
return !(a == b);
}
/// <summary>
/// 将此实例转换成 TimeSpan
/// </summary>
/// <returns></returns>
public TimeSpan ToTimeSpan()
{
return new TimeSpan(_hour, _minute, _second);
}
#region ISerializable 成员
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("hour", _hour);
info.AddValue("minute", _minute);
info.AddValue("second", _second);
}
#endregion
#region IXmlSerializable 成员
public System.Xml.Schema.XmlSchema GetSchema()
{
return null;
}
public void ReadXml(System.Xml.XmlReader reader)
{
_hour = Convert.ToInt32(reader.GetAttribute("hour"));
_minute = Convert.ToInt32(reader.GetAttribute("minute"));
_second = Convert.ToInt32(reader.GetAttribute("second"));
reader.Read();
}
public void WriteXml(System.Xml.XmlWriter writer)
{
writer.WriteAttributeString("hour", _hour.ToString());
writer.WriteAttributeString("minute", _minute.ToString());
writer.WriteAttributeString("second", _second.ToString());
}
#endregion
}