File size: 10,937 Bytes
fab29d7 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 | using System;
namespace VersOne.Epub
{
/// <summary>
/// Represents a timestamp of the beginning or the end of the narration audio clip.
/// </summary>
public readonly struct EpubNarrationTimestamp : IComparable, IComparable<EpubNarrationTimestamp>, IEquatable<EpubNarrationTimestamp>
{
private readonly TimeSpan timeSpan;
/// <summary>
/// Initializes a new instance of the <see cref="EpubNarrationTimestamp" /> structure.
/// </summary>
/// <param name="hour">The hours part of the timestamp.</param>
/// <param name="minute">The minutes part of the timestamp.</param>
/// <param name="second">The seconds part of the timestamp.</param>
/// <param name="millisecond">The milliseconds part of the timestamp.</param>
public EpubNarrationTimestamp(int hour, int minute, int second, int millisecond)
{
timeSpan = new TimeSpan(0, hour, minute, second, millisecond);
}
/// <summary>
/// Gets the hours part of the timestamp.
/// </summary>
public int Hour => (timeSpan.Days * 24) + timeSpan.Hours;
/// <summary>
/// Gets the minutes part of the timestamp.
/// </summary>
public int Minute => timeSpan.Minutes;
/// <summary>
/// Gets the seconds part of the timestamp.
/// </summary>
public int Second => timeSpan.Seconds;
/// <summary>
/// Gets the milliseconds part of the timestamp.
/// </summary>
public int Millisecond => timeSpan.Milliseconds;
/// <summary>
/// Determines whether two specified instances of <see cref="EpubNarrationTimestamp" /> are equal.
/// </summary>
/// <param name="left">The first object to compare.</param>
/// <param name="right">The second object to compare.</param>
/// <returns><c>true</c> if <paramref name="left" /> and <paramref name="right" /> represent the same timestamp; otherwise, false.</returns>
public static bool operator ==(EpubNarrationTimestamp left, EpubNarrationTimestamp right)
{
return left.Equals(right);
}
/// <summary>
/// Determines whether two specified instances of <see cref="EpubNarrationTimestamp" /> are not equal.
/// </summary>
/// <param name="left">The first object to compare.</param>
/// <param name="right">The second object to compare.</param>
/// <returns><c>true</c> if <paramref name="left" /> and <paramref name="right" /> do not represent the same timestamp; otherwise, false.</returns>
public static bool operator !=(EpubNarrationTimestamp left, EpubNarrationTimestamp right)
{
return !left.Equals(right);
}
/// <summary>
/// Determines whether one specified <see cref="EpubNarrationTimestamp" /> is later than another specified <see cref="EpubNarrationTimestamp" />.
/// </summary>
/// <param name="left">The first object to compare.</param>
/// <param name="right">The second object to compare.</param>
/// <returns><c>true</c> if <paramref name="left" /> is later than <paramref name="right" />; otherwise, false.</returns>
public static bool operator >(EpubNarrationTimestamp left, EpubNarrationTimestamp right)
{
return left.timeSpan > right.timeSpan;
}
/// <summary>
/// Determines whether one specified <see cref="EpubNarrationTimestamp" /> is the same or later than another specified <see cref="EpubNarrationTimestamp" />.
/// </summary>
/// <param name="left">The first object to compare.</param>
/// <param name="right">The second object to compare.</param>
/// <returns><c>true</c> if <paramref name="left" /> is the same or later than <paramref name="right" />; otherwise, false.</returns>
public static bool operator >=(EpubNarrationTimestamp left, EpubNarrationTimestamp right)
{
return left.timeSpan >= right.timeSpan;
}
/// <summary>
/// Determines whether one specified <see cref="EpubNarrationTimestamp" /> is earlier than another specified <see cref="EpubNarrationTimestamp" />.
/// </summary>
/// <param name="left">The first object to compare.</param>
/// <param name="right">The second object to compare.</param>
/// <returns><c>true</c> if <paramref name="left" /> is earlier than <paramref name="right" />; otherwise, false.</returns>
public static bool operator <(EpubNarrationTimestamp left, EpubNarrationTimestamp right)
{
return left.timeSpan < right.timeSpan;
}
/// <summary>
/// Determines whether one specified <see cref="EpubNarrationTimestamp" /> is the same or earlier than another specified <see cref="EpubNarrationTimestamp" />.
/// </summary>
/// <param name="left">The first object to compare.</param>
/// <param name="right">The second object to compare.</param>
/// <returns><c>true</c> if <paramref name="left" /> is the same or earlier than <paramref name="right" />; otherwise, false.</returns>
public static bool operator <=(EpubNarrationTimestamp left, EpubNarrationTimestamp right)
{
return left.timeSpan <= right.timeSpan;
}
/// <summary>
/// Subtracts a specified <see cref="EpubNarrationTimestamp" /> from another specified <see cref="EpubNarrationTimestamp" /> and returns a time interval.
/// </summary>
/// <param name="first">The timestamp value to subtract from (the minuend).</param>
/// <param name="second">The timestamp value to subtract (the subtrahend).</param>
/// <returns>The time interval between <paramref name="first" /> and <paramref name="second" />.</returns>
public static TimeSpan operator -(EpubNarrationTimestamp first, EpubNarrationTimestamp second)
{
return first.timeSpan - second.timeSpan;
}
/// <summary>
/// Compares the value of this instance to a specified object that contains a specified <see cref="EpubNarrationTimestamp" /> value,
/// and returns an integer that indicates whether this instance is earlier than, the same as, or later than the specified <see cref="EpubNarrationTimestamp" /> value.
/// </summary>
/// <param name="obj">A boxed object to compare, or null.</param>
/// <returns>
/// A signed number indicating the relative values of this instance and the <paramref name="obj" /> parameter.
/// Less than zero if this instance is earlier than <paramref name="obj" />.
/// Zero if this instance is the same as <paramref name="obj" />.
/// Greater than zero if this instance is later than <paramref name="obj" />.
/// </returns>
/// <exception cref="ArgumentException">The type of the <paramref name="obj" /> parameter is not <see cref="EpubNarrationTimestamp" />.</exception>
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);
}
/// <summary>
/// Compares the value of this instance to a specified <see cref="EpubNarrationTimestamp" /> value and returns an integer that indicates
/// whether this instance is earlier than, the same as, or later than the specified <see cref="EpubNarrationTimestamp" /> value.
/// </summary>
/// <param name="other">The object to compare to the current instance.</param>
/// <returns>
/// A signed number indicating the relative values of this instance and the <paramref name="other" /> parameter.
/// Less than zero if this instance is earlier than <paramref name="other" />.
/// Zero if this instance is the same as <paramref name="other" />.
/// Greater than zero if this instance is later than <paramref name="other" />.
/// </returns>
public int CompareTo(EpubNarrationTimestamp other)
{
return timeSpan.CompareTo(other.timeSpan);
}
/// <summary>
/// Returns a value indicating whether this instance is equal to a specified object.
/// </summary>
/// <param name="obj">The object to compare to this instance.</param>
/// <returns>
/// <c>true</c> if <paramref name="obj" /> is an instance of <see cref="EpubNarrationTimestamp" /> and equals the value of this instance; otherwise, <c>false</c>.
/// </returns>
public override bool Equals(object? obj)
{
return obj is EpubNarrationTimestamp epubNarrationTimestamp && Equals(epubNarrationTimestamp);
}
/// <summary>
/// Returns a value indicating whether the value of this instance is equal to the value of the specified <see cref="EpubNarrationTimestamp" /> instance.
/// </summary>
/// <param name="other">The object to compare to this instance.</param>
/// <returns><c>true</c> if the <paramref name="other" /> parameter equals the value of this instance; otherwise, <c>false</c>.</returns>
public bool Equals(EpubNarrationTimestamp other)
{
return timeSpan.Equals(other.timeSpan);
}
/// <summary>
/// Returns the hash code for this instance.
/// </summary>
/// <returns>A 32-bit signed integer hash code.</returns>
public override int GetHashCode()
{
return timeSpan.GetHashCode();
}
/// <summary>
/// Converts the value of the current <see cref="EpubNarrationTimestamp" /> object to its equivalent string representation.
/// </summary>
/// <returns>A string representation of the value of the current <see cref="EpubNarrationTimestamp" /> object in <c>h:mm:ss[.fff]</c> format (e.g. <c>1:23:45.678</c>).</returns>
public override string ToString()
{
return $"{Hour}:{Minute:D2}:{Second:D2}{(Millisecond > 0 ? ("." + Millisecond.ToString("D3")) : String.Empty)}";
}
/// <summary>
/// Converts the value of the current <see cref="EpubNarrationTimestamp" /> object to its equivalent <see cref="TimeSpan" /> representation.
/// </summary>
/// <returns>A <see cref="TimeSpan" /> representation of the value of the current <see cref="EpubNarrationTimestamp" /> object.</returns>
public TimeSpan ToTimeSpan()
{
return timeSpan;
}
}
}
|