Initial Commit (broken due to version update)
This commit is contained in:
parent
74003df84b
commit
3f7b3b7440
3132 changed files with 1103305 additions and 0 deletions
82
Assets/Scripts/TempusReader/Time.cs
Normal file
82
Assets/Scripts/TempusReader/Time.cs
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Parsley;
|
||||
|
||||
namespace TempusReader
|
||||
{
|
||||
public class Time : IEquatable<Time>, IComparable<Time>
|
||||
{
|
||||
private static readonly Lexer Lexer = new TimeLexer();
|
||||
private static readonly IDictionary<string, Func<double, TimeSpan>> DateParts = new Dictionary<string, Func<double, TimeSpan>>
|
||||
{
|
||||
{ TimeLexer.Milliseconds.Name, TimeSpan.FromMilliseconds },
|
||||
{ TimeLexer.Seconds.Name, TimeSpan.FromSeconds },
|
||||
{ TimeLexer.Minutes.Name, TimeSpan.FromMinutes },
|
||||
{ TimeLexer.Hours.Name, TimeSpan.FromHours },
|
||||
{ TimeLexer.Days.Name, TimeSpan.FromDays },
|
||||
};
|
||||
|
||||
private readonly TimeSpan _timeSpan;
|
||||
|
||||
public Time(TimeSpan timeSpan)
|
||||
{
|
||||
_timeSpan = timeSpan;
|
||||
}
|
||||
|
||||
public Time(string input)
|
||||
{
|
||||
var tokens = Lexer.Tokenize(input).ToArray();
|
||||
|
||||
_timeSpan = TempusGrammar.TimeSpan.Parse(new TokenStream(tokens)).Value;
|
||||
}
|
||||
|
||||
public static implicit operator TimeSpan(Time time)
|
||||
{
|
||||
return time == null ? default(TimeSpan) : time._timeSpan;
|
||||
}
|
||||
|
||||
public int CompareTo(Time other)
|
||||
{
|
||||
return _timeSpan.CompareTo(other._timeSpan);
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return _timeSpan.ToString();
|
||||
}
|
||||
|
||||
public bool Equals(Time other)
|
||||
{
|
||||
return _timeSpan.Equals(other._timeSpan);
|
||||
}
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
if (ReferenceEquals(null, obj)) return false;
|
||||
if (ReferenceEquals(this, obj)) return true;
|
||||
if (obj.GetType() != typeof (Time)) return false;
|
||||
return Equals((Time)obj);
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return _timeSpan.GetHashCode();
|
||||
}
|
||||
|
||||
public static bool operator ==(Time left, Time right)
|
||||
{
|
||||
return Equals(left, right);
|
||||
}
|
||||
|
||||
public static bool operator !=(Time left, Time right)
|
||||
{
|
||||
return !Equals(left, right);
|
||||
}
|
||||
|
||||
public static TimeSpan FromDateParts(IEnumerable<KeyValuePair<string, double>> times)
|
||||
{
|
||||
return times.Aggregate(new TimeSpan(), (current, t) => current.Add(DateParts[t.Key](t.Value)));
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue