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.S);
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 Units.Ms;
case Unit.S:
return Units.S;
default:
return string.Empty;
}
}
}
public Time(float value, Unit unit)
{
_value = value;
_unit = unit;
}
public static explicit operator float(Time time)
{
return time.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)
{
switch (s) {
case "s":
return Unit.S;
case "ms":
return Unit.Ms;
default:
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 int CompareTo(Time other)
{
return ToMilliseconds().CompareTo(other.ToMilliseconds());
}
public override bool Equals(object obj)
{
if (obj is Length)
return Equals((Length)obj);
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;
}
}
}