Skip to content

Instantly share code, notes, and snippets.

@demius
Last active October 8, 2019 06:48
Show Gist options
  • Save demius/675b0590fdb43c2252fa0fd48c95406f to your computer and use it in GitHub Desktop.
Save demius/675b0590fdb43c2252fa0fd48c95406f to your computer and use it in GitHub Desktop.
C# Time class
/// <summary>
/// A simple representation of time
/// </summary>
public sealed class Time : IEquatable<Time>
{
/// <summary>
/// Gets the hour
/// </summary>
public int Hour { get; }
/// <summary>
/// Gets the minute
/// </summary>
public int Minute { get; }
/// <summary>
/// Gets the second
/// </summary>
public int Second { get; }
/// <summary>
/// Gets the millisecond
/// </summary>
public int Millisecond { get; }
/// <summary>
/// Gets the current local time
/// </summary>
public static Time Now { get; } = From(DateTime.Now);
/// <summary>
/// Gets the current UTC standard time
/// </summary>
public static Time UtcNow { get; } = From(DateTime.UtcNow);
/// <summary>
/// Creates a new <see cref="Time"/> instance
/// </summary>
/// <exception cref="ArgumentOutOfRangeException">Raised when any of the given time parts are outside the allowed range of values</exception>
public Time(int hour, int minute, int second, int millisecond = 0)
{
if (hour < 0 || hour > 23)
throw new ArgumentOutOfRangeException(nameof(hour));
if (minute < 0 || minute > 59)
throw new ArgumentOutOfRangeException(nameof(minute));
if (second < 0 || second > 59)
throw new ArgumentOutOfRangeException(nameof(second));
if (millisecond < 0 || millisecond > 999)
throw new ArgumentOutOfRangeException(nameof(millisecond));
Hour = hour;
Minute = minute;
Second = second;
Millisecond = millisecond;
}
/// <summary>
/// Create a time instance based on the provided <see cref="DateTime"/>
/// </summary>
public static Time From(DateTime dateTime)
{
return new Time(dateTime.Hour, dateTime.Minute, dateTime.Second, dateTime.Millisecond);
}
/// <summary>
/// Parses the provided <paramref name="input"/> according to the following opinionated format: HH:mm:ss.fff
/// </summary>
public static Time Parse(string input)
{
int h = 0, m = 0, s = 0, ms = 0;
var parts = input.Split(':', ',', '.');
if (parts.Length > 0)
h = int.Parse(parts[0]);
if (parts.Length > 1)
m = int.Parse(parts[1]);
if (parts.Length > 2)
s = int.Parse(parts[2]);
if (parts.Length > 3)
ms = int.Parse(parts[3]);
return new Time(h, m, s, ms);
}
public static bool operator >(Time t1, Time t2)
{
if (t1.Hour != t2.Hour)
return t1.Hour > t2.Hour;
if (t1.Minute != t2.Minute)
return t1.Minute > t2.Minute;
if (t1.Second != t2.Second)
return t1.Second > t2.Second;
if (t1.Millisecond != t2.Millisecond)
return t1.Millisecond > t2.Millisecond;
return false;
}
public static bool operator >=(Time t1, Time t2)
{
if (t1 > t2) return true;
if (t1 == t2) return true;
return false;
}
public static bool operator <=(Time t1, Time t2)
{
if (t1 < t2) return true;
if (t1 == t2) return true;
return false;
}
public static bool operator <(Time t1, Time t2)
{
if (t1.Hour != t2.Hour)
return t1.Hour < t2.Hour;
if (t1.Minute != t2.Minute)
return t1.Minute < t2.Minute;
if (t1.Second != t2.Second)
return t1.Second < t2.Second;
if (t1.Millisecond != t2.Millisecond)
return t1.Millisecond < t2.Millisecond;
return false;
}
public static bool operator ==(Time t1, Time t2)
{
return !(t1 > t2) && !(t1 < t2);
}
public static bool operator !=(Time t1, Time t2)
{
return (t1 > t2) || (t1 < t2);
}
public static implicit operator string(Time time)
{
return time.ToString();
}
public override bool Equals(object obj)
{
return Equals(obj as Time);
}
public bool Equals(Time other)
{
return other != null &&
Hour == other.Hour &&
Minute == other.Minute &&
Second == other.Second &&
Millisecond == other.Millisecond;
}
public override int GetHashCode()
{
unchecked
{
var hashCode = -165222881;
hashCode = hashCode * -1521134295 + Hour.GetHashCode();
hashCode = hashCode * -1521134295 + Minute.GetHashCode();
hashCode = hashCode * -1521134295 + Second.GetHashCode();
hashCode = hashCode * -1521134295 + Millisecond.GetHashCode();
return hashCode;
}
}
public override string ToString()
{
return $"{Hour.ToString().PadLeft(2, '0')}:{Minute.ToString().PadLeft(2, '0')}:{Second.ToString().PadLeft(2, '0')}.{Millisecond.ToString().PadLeft(3, '0')}";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment