public bool Equals(ShortTimeSpan other) { return(other.Hour == this.Hour && other.Minute == this.Minute && other.Second == this.Second && other.Positive == this.Positive); }
public static ShortTimeSpan FromString(string s) { string st = s; if (s.StartsWith("+") || s.StartsWith("-")) { st = s.Substring(1); } string[] parts = st.Split(':'); ShortTimeSpan timeSpan = new ShortTimeSpan(); timeSpan.OriginalString = s; timeSpan.Hour = int.Parse(parts[0]); if (parts.Length > 1) { timeSpan.Minute = int.Parse(parts[1]); } if (parts.Length > 2) { timeSpan.Second = int.Parse(parts[2]); } if (s.StartsWith("-")) { timeSpan.Positive = false; } return(timeSpan); }
public void ShortTimeSpanTest() { var v = new[] { 0.01, 0.001, 0.0001, 10e-6, 1e-6, 1e-9, 1e-10 }; var s = new[] { "10 ms", "1 ms", "100 us", "10 μs", "1 μs", "1 ns", "0 s" }; for (int i = 0; i < v.Length; i++) { var s1 = ShortTimeSpan.FromSeconds(v[i]); var s2 = ShortTimeSpan.FromString(s[i]); StringAssert.AreEqualIgnoringCase(s1.ToString(), s2.ToString()); } }
public int Execute(CancellationToken cancellationToken) { if (ProfileTimeSpanToString) { StringBuilder sb = new StringBuilder(); ShortTimeSpan.FromSeconds(0.01).ToString(sb); var sw = Stopwatch.StartNew(); for (int i = 0; i < 1000000; i++) { //ShortTimeSpan.FromSeconds(0.01 * i).ToString(sb); ShortTimeSpan.FromSeconds(0.01 * i).ToString(sb); if (i % 10 == 0) { sb.Clear(); } } Console.WriteLine("TimeSpan: {0}ms", sw.ElapsedMilliseconds); } if (ProfileTestPlan) { new TestPlanPerformanceTest().GeneralPerformanceTest(10000); } if (ProfileSearch) { var sw = Stopwatch.StartNew(); for (int i = 0; i < Iterations; i++) { PluginManager.Search(); } Console.WriteLine("Search Took {0}ms in total.", sw.ElapsedMilliseconds); } return(0); }
bool ParseTimeZone(string timeZone, TimeZoneDescription tz, out string reason) { if (string.IsNullOrEmpty(timeZone)) { reason = "TimeZone string is null or empty"; return(false); } int i = 0; reason = string.Empty; // find Std while (i < timeZone.Length) { char nextChar = timeZone[i]; if (nextChar == '+' || nextChar == '-' || Char.IsDigit(nextChar)) { break; } i++; } tz.Std = timeZone.Substring(0, i); // std offset int nextPartStart = i; while (i < timeZone.Length) { char nextChar = timeZone[i]; if (nextChar != ':' && nextChar != '+' && nextChar != '-' && !Char.IsDigit(nextChar)) { break; } i++; } string stdOffsetString = timeZone.Substring(nextPartStart, i - nextPartStart); Regex regex = new Regex(@"^[-\+]{0,1}\d{1,2}(:\d{1,2}){0,2}$"); if (!regex.IsMatch(stdOffsetString)) { reason = string.Format("Standart offset part format is incorrect ({0})", stdOffsetString); return(false); } tz.StdOffset = ShortTimeSpan.FromString(stdOffsetString); if (!tz.StdOffset.IsValid()) { reason = string.Format("Standart offset is incorrect ({0})", stdOffsetString); return(false); } nextPartStart = i; bool noDstOffset = (i == timeZone.Length); // Dst while (i < timeZone.Length) { char nextChar = timeZone[i]; if (nextChar == '+' || nextChar == '-' || Char.IsDigit(nextChar)) { break; } if (nextChar == ',') { // comman means "no offset" noDstOffset = true; break; } i++; } tz.Dst = timeZone.Substring(nextPartStart, i - nextPartStart); if (noDstOffset) { i++; } nextPartStart = i; if (!noDstOffset) { while (i < timeZone.Length) { char nextChar = timeZone[i]; if (nextChar != ':' && nextChar != '+' && nextChar != '-' && !Char.IsDigit(nextChar)) { break; } i++; } string dstOffsetString = timeZone.Substring(nextPartStart, i - nextPartStart); if (!regex.IsMatch(dstOffsetString)) { reason = string.Format("DST offset part format is incorrect ({0})", dstOffsetString); return(false); } tz.DstOffset = ShortTimeSpan.FromString(dstOffsetString); if (!tz.DstOffset.IsValid()) { reason = string.Format("DST offset is incorrect ({0})", dstOffsetString); return(false); } nextPartStart = i; } else { tz.DstOffset = new ShortTimeSpan(tz.StdOffset.Hour - 1, tz.StdOffset.Minute, tz.StdOffset.Second); tz.DstOffset.OriginalString = string.Format("{0}:{1}:{2}", tz.DstOffset.Hour.ToString("00"), tz.DstOffset.Minute.ToString("00"), tz.DstOffset.Second.ToString("00")); } bool noStartTime = (i >= timeZone.Length); while (i < timeZone.Length) { char nextChar = timeZone[i]; if (nextChar == '/' || nextChar == ',') { noStartTime = (nextChar == ','); break; } i++; } if (!noStartTime) { tz.Start = timeZone.Substring(nextPartStart, i - nextPartStart); if (!IsValidTimeZoneDay(tz.Start)) { reason = string.Format("Start part format is incorrect ({0})", tz.Start); return(false); } } i++; nextPartStart = i; if (!noStartTime) { while (i < timeZone.Length) { char nextChar = timeZone[i]; if (nextChar == ',') { break; } i++; } string startTimeString = timeZone.Substring(nextPartStart, i - nextPartStart); if (!regex.IsMatch(startTimeString)) { reason = string.Format("Start time part format is incorrect ({0})", startTimeString); return(false); } tz.StartTime = ShortTimeSpan.FromString(startTimeString); if (!tz.StdOffset.IsValid()) { reason = string.Format("Start time is incorrect ({0})", startTimeString); return(false); } i++; nextPartStart = i; } else { tz.StartTime = new ShortTimeSpan(2, 0, 0); tz.StartTime.OriginalString = "02:00:00"; } bool noEndTime = (i > timeZone.Length); while (i < timeZone.Length) { char nextChar = timeZone[i]; if (nextChar == '/' || nextChar == ',') { noEndTime = (nextChar == ','); break; } i++; } if (!noEndTime) { tz.End = timeZone.Substring(nextPartStart, i - nextPartStart); if (!IsValidTimeZoneDay(tz.End)) { reason = string.Format("End part format is incorrect ({0})", tz.End); return(false); } } i++; if (i > timeZone.Length) { noEndTime = true; } nextPartStart = i; if (!noEndTime) { while (i < timeZone.Length) { char nextChar = timeZone[i]; if (nextChar == ',') { break; } i++; } string endTimeString = timeZone.Substring(nextPartStart, i - nextPartStart); if (!regex.IsMatch(endTimeString)) { reason = string.Format("End time part format is incorrect ({0})", endTimeString); return(false); } tz.EndTime = ShortTimeSpan.FromString(endTimeString); if (!tz.StdOffset.IsValid()) { reason = string.Format("End time is incorrect ({0})", endTimeString); return(false); } } else { tz.EndTime = new ShortTimeSpan(2, 0, 0); tz.EndTime.OriginalString = "02:00:00"; } return(true); }