Time
Represents a time value.
using AngleSharp.Extensions;
using System;
namespace AngleSharp.Css.Values
{
public struct Time : IEquatable<Time>, IComparable<Time>, IFormattable
{
public enum Unit : ushort
{
None,
Ms,
S
}
public static readonly Time Zero = new Time(0, Unit.Ms);
private readonly float _value;
private readonly Unit _unit;
public float Value => _value;
public Unit Type => _unit;
public string UnitString {
get {
switch (_unit) {
case Unit.Ms:
return UnitNames.Ms;
case Unit.S:
return UnitNames.S;
default:
return string.Empty;
}
}
}
public Time(float value, Unit unit)
{
_value = value;
_unit = unit;
}
public static bool operator >=(Time a, Time b)
{
int num = a.CompareTo(b);
if (num != 0)
return num == 1;
return true;
}
public static bool operator >(Time a, Time b)
{
return a.CompareTo(b) == 1;
}
public static bool operator <=(Time a, Time b)
{
int num = a.CompareTo(b);
if (num != 0)
return num == -1;
return true;
}
public static bool operator <(Time a, Time b)
{
return a.CompareTo(b) == -1;
}
public int CompareTo(Time other)
{
return ToMilliseconds().CompareTo(other.ToMilliseconds());
}
public static bool TryParse(string s, out Time result)
{
float result2 = 0;
Unit unit = GetUnit(s.CssUnit(out result2));
if (unit != 0) {
result = new Time(result2, unit);
return true;
}
result = default(Time);
return false;
}
public static Unit GetUnit(string s)
{
if (s == "s")
return Unit.S;
if (s == "ms")
return Unit.Ms;
return Unit.None;
}
public float ToMilliseconds()
{
if (_unit != Unit.S)
return _value;
return _value * 1000;
}
public bool Equals(Time other)
{
return ToMilliseconds() == other.ToMilliseconds();
}
public static bool operator ==(Time a, Time b)
{
return a.Equals(b);
}
public static bool operator !=(Time a, Time b)
{
return !a.Equals(b);
}
public override bool Equals(object obj)
{
Time? nullable = obj as Time?;
if (nullable.HasValue)
return Equals(nullable.Value);
return false;
}
public override int GetHashCode()
{
return _value.GetHashCode();
}
public override string ToString()
{
return _value.ToString() + UnitString;
}
public string ToString(string format, IFormatProvider formatProvider)
{
return _value.ToString(format, formatProvider) + UnitString;
}
}
}