AcUtils
A high performance abstraction layer for AccuRev
AcDuration.cs
Go to the documentation of this file.
1 
16 using System;
17 
18 namespace AcUtils
19 {
24  [Serializable]
25  public class AcDuration : IFormattable, IEquatable<AcDuration>, IComparable<AcDuration>, IComparable
26  {
27  private TimeSpan _ts;
28 
32  public AcDuration(TimeSpan ts)
33  {
34  _ts = ts;
35  }
36 
40  public static implicit operator TimeSpan(AcDuration rhs)
41  {
42  return (rhs != null) ? rhs._ts : new TimeSpan();
43  }
44 
48  public static implicit operator AcDuration(TimeSpan rhs)
49  {
50  return new AcDuration(rhs);
51  }
52 
53  #region Equality comparison
54 
56  public bool Equals(AcDuration other)
62  {
63  if (ReferenceEquals(other, null)) return false;
64  if (ReferenceEquals(this, other)) return true;
65  return _ts.Equals(other._ts);
66  }
67 
72  public override bool Equals(object other)
73  {
74  if (ReferenceEquals(other, null)) return false;
75  if (ReferenceEquals(this, other)) return true;
76  if (GetType() != other.GetType()) return false;
77  return this.Equals(other as AcDuration);
78  }
79 
84  public override int GetHashCode()
85  {
86  return _ts.GetHashCode();
87  }
89  #endregion
90 
91  #region Order comparison
92 
94  public int CompareTo(AcDuration other)
100  {
101  int result;
102  if (AcDuration.ReferenceEquals(this, other))
103  result = 0;
104  else
105  result = _ts.CompareTo(other._ts);
106 
107  return result;
108  }
109 
116  int IComparable.CompareTo(object other)
117  {
118  if (!(other is AcDuration))
119  throw new ArgumentException("Argument is not an AcDuration", "other");
120  AcDuration o = (AcDuration)other;
121  return this.CompareTo(o);
122  }
124  #endregion
125 
126  #region ToString
127  public string ToString(string format, IFormatProvider provider)
141  {
142  if (provider != null)
143  {
144  ICustomFormatter fmt = provider.GetFormat(this.GetType()) as ICustomFormatter;
145  if (fmt != null)
146  return fmt.Format(format, this, provider);
147  }
148 
149  if (String.IsNullOrEmpty(format))
150  format = "G";
151 
152  switch (format.ToUpperInvariant())
153  {
154  case "G": // default when not using a format specifier
155  {
156  string hours = $"{_ts.Hours:D2}";
157  string minutes = $"{_ts.Minutes:D2}";
158  string seconds = $"{_ts.Seconds:D2}";
159  string elapsedTime;
160  if (_ts.Days > 0)
161  elapsedTime = $"{_ts.Days} {((_ts.Days == 1) ? "day" : "days")}, {hours}:{minutes}:{seconds}";
162  else
163  elapsedTime = $"{hours}:{minutes}:{seconds}";
164  return elapsedTime;
165  }
166  case "H":
167  return $"{_ts.Hours:D2}";
168  case "M":
169  return $"{_ts.Minutes:D2}";
170  case "S":
171  return $"{_ts.Seconds:D2}";
172  case "D":
173  return $"{_ts.Days} {((_ts.Days == 1) ? "day" : "days")}";
174  default:
175  throw new FormatException($"The {format} format string is not supported.");
176  }
177  }
178 
179  // Calls ToString(string, IFormatProvider) version with a null IFormatProvider argument.
180  public string ToString(string format)
181  {
182  return ToString(format, null);
183  }
184 
185  // Calls ToString(string, IFormatProvider) version with the general format and a null IFormatProvider argument.
186  public override string ToString()
187  {
188  return ToString("G", null);
189  }
190  #endregion ToString
191  }
192 }
override bool Equals(object other)
Overridden to determine equality.
Definition: AcDuration.cs:72
Wrapper around TimeSpan so we can return our own formatted elapsed time string and still sort correct...
Definition: AcDuration.cs:25
bool Equals(AcDuration other)
IEquatable implementation to determine the equality of instances of type AcDuration.
Definition: AcDuration.cs:61
int CompareTo(AcDuration other)
Generic IComparable implementation (default) for comparing AcDuration objects to sort by timespan...
Definition: AcDuration.cs:99
string ToString(string format, IFormatProvider provider)
The ToString implementation.
Definition: AcDuration.cs:140
override int GetHashCode()
Override appropriate for type AcDuration.
Definition: AcDuration.cs:84
AcDuration(TimeSpan ts)
Constructor.
Definition: AcDuration.cs:32