using System;
namespace VersOne.Epub
{
///
/// Represents a timestamp of the beginning or the end of the narration audio clip.
///
public readonly struct EpubNarrationTimestamp : IComparable, IComparable, IEquatable
{
private readonly TimeSpan timeSpan;
///
/// Initializes a new instance of the structure.
///
/// The hours part of the timestamp.
/// The minutes part of the timestamp.
/// The seconds part of the timestamp.
/// The milliseconds part of the timestamp.
public EpubNarrationTimestamp(int hour, int minute, int second, int millisecond)
{
timeSpan = new TimeSpan(0, hour, minute, second, millisecond);
}
///
/// Gets the hours part of the timestamp.
///
public int Hour => (timeSpan.Days * 24) + timeSpan.Hours;
///
/// Gets the minutes part of the timestamp.
///
public int Minute => timeSpan.Minutes;
///
/// Gets the seconds part of the timestamp.
///
public int Second => timeSpan.Seconds;
///
/// Gets the milliseconds part of the timestamp.
///
public int Millisecond => timeSpan.Milliseconds;
///
/// Determines whether two specified instances of are equal.
///
/// The first object to compare.
/// The second object to compare.
/// true if and represent the same timestamp; otherwise, false.
public static bool operator ==(EpubNarrationTimestamp left, EpubNarrationTimestamp right)
{
return left.Equals(right);
}
///
/// Determines whether two specified instances of are not equal.
///
/// The first object to compare.
/// The second object to compare.
/// true if and do not represent the same timestamp; otherwise, false.
public static bool operator !=(EpubNarrationTimestamp left, EpubNarrationTimestamp right)
{
return !left.Equals(right);
}
///
/// Determines whether one specified is later than another specified .
///
/// The first object to compare.
/// The second object to compare.
/// true if is later than ; otherwise, false.
public static bool operator >(EpubNarrationTimestamp left, EpubNarrationTimestamp right)
{
return left.timeSpan > right.timeSpan;
}
///
/// Determines whether one specified is the same or later than another specified .
///
/// The first object to compare.
/// The second object to compare.
/// true if is the same or later than ; otherwise, false.
public static bool operator >=(EpubNarrationTimestamp left, EpubNarrationTimestamp right)
{
return left.timeSpan >= right.timeSpan;
}
///
/// Determines whether one specified is earlier than another specified .
///
/// The first object to compare.
/// The second object to compare.
/// true if is earlier than ; otherwise, false.
public static bool operator <(EpubNarrationTimestamp left, EpubNarrationTimestamp right)
{
return left.timeSpan < right.timeSpan;
}
///
/// Determines whether one specified is the same or earlier than another specified .
///
/// The first object to compare.
/// The second object to compare.
/// true if is the same or earlier than ; otherwise, false.
public static bool operator <=(EpubNarrationTimestamp left, EpubNarrationTimestamp right)
{
return left.timeSpan <= right.timeSpan;
}
///
/// Subtracts a specified from another specified and returns a time interval.
///
/// The timestamp value to subtract from (the minuend).
/// The timestamp value to subtract (the subtrahend).
/// The time interval between and .
public static TimeSpan operator -(EpubNarrationTimestamp first, EpubNarrationTimestamp second)
{
return first.timeSpan - second.timeSpan;
}
///
/// Compares the value of this instance to a specified object that contains a specified value,
/// and returns an integer that indicates whether this instance is earlier than, the same as, or later than the specified value.
///
/// A boxed object to compare, or null.
///
/// A signed number indicating the relative values of this instance and the parameter.
/// Less than zero if this instance is earlier than .
/// Zero if this instance is the same as .
/// Greater than zero if this instance is later than .
///
/// The type of the parameter is not .
public int CompareTo(object? obj)
{
if (obj == null)
{
return 1;
}
if (obj is not EpubNarrationTimestamp epubNarrationTimestamp)
{
throw new ArgumentException($"The type of the \"{nameof(obj)}\" parameter must be {nameof(EpubNarrationTimestamp)}.", nameof(obj));
}
return CompareTo(epubNarrationTimestamp);
}
///
/// Compares the value of this instance to a specified value and returns an integer that indicates
/// whether this instance is earlier than, the same as, or later than the specified value.
///
/// The object to compare to the current instance.
///
/// A signed number indicating the relative values of this instance and the parameter.
/// Less than zero if this instance is earlier than .
/// Zero if this instance is the same as .
/// Greater than zero if this instance is later than .
///
public int CompareTo(EpubNarrationTimestamp other)
{
return timeSpan.CompareTo(other.timeSpan);
}
///
/// Returns a value indicating whether this instance is equal to a specified object.
///
/// The object to compare to this instance.
///
/// true if is an instance of and equals the value of this instance; otherwise, false.
///
public override bool Equals(object? obj)
{
return obj is EpubNarrationTimestamp epubNarrationTimestamp && Equals(epubNarrationTimestamp);
}
///
/// Returns a value indicating whether the value of this instance is equal to the value of the specified instance.
///
/// The object to compare to this instance.
/// true if the parameter equals the value of this instance; otherwise, false.
public bool Equals(EpubNarrationTimestamp other)
{
return timeSpan.Equals(other.timeSpan);
}
///
/// Returns the hash code for this instance.
///
/// A 32-bit signed integer hash code.
public override int GetHashCode()
{
return timeSpan.GetHashCode();
}
///
/// Converts the value of the current object to its equivalent string representation.
///
/// A string representation of the value of the current object in h:mm:ss[.fff] format (e.g. 1:23:45.678).
public override string ToString()
{
return $"{Hour}:{Minute:D2}:{Second:D2}{(Millisecond > 0 ? ("." + Millisecond.ToString("D3")) : String.Empty)}";
}
///
/// Converts the value of the current object to its equivalent representation.
///
/// A representation of the value of the current object.
public TimeSpan ToTimeSpan()
{
return timeSpan;
}
}
}