using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Forms; namespace TradeIdeas.TIProGUI { /// /// This class is associated with 2-D Lookup class /// public class MiscSupport { /// /// Converts a string to a double, no default value /// /// /// public static double nfroma(string s) { double m; bool parsed = Double.TryParse(s, out m); if (!parsed) { MessageBox.Show("Could not parse " + s + " to double"); return -1; } return m; } /// /// Convert a string to an int64 /// /// /// if string can't be parsed /// public static Int64 strtollDefault(string input, Int64 defaultValue) { Int64 m; bool parsed = Int64.TryParse(input, out m); if (!parsed) { return defaultValue; } return m; } /// /// convert string to a double /// /// /// if string cannot be parsed /// public static double strtodDefault(string input, double defaultValue) { double m; bool parsed = double.TryParse(input, out m); if (!parsed) { return defaultValue; } return m; } /////Time Maniuplations//// /// /// process Delphi time to a c# DateTime object /// /// /// public static DateTime importFromDelphi(double delphiTime) { int days = (int)delphiTime; // Note: Delphi time is precise to the millisecond. 4.999 seconds means // that we will hit 5 seconds in 1 millisecond, so we should still call it // 4 seconds. 4.999999997 means that there was roundoff error, and this // should be interpreted as 5.000 seconds. So we round to the neareast // millisecond, then truncate to the correct second. We have to follow this // algorithm to show the right time. Clocks that display things precise to // the minute still hit midnight at the same time as clocks that are precise // to the millisecond. If we rounded more, the clock that was precise to the // minute would chime 30 seconds too soon. int second = (int)((delphiTime - days) * 24 * 60 * 60 + 0.0005); uint daysToSeconds = (uint)(days * 60 * 60 * 24); uint totalSeconds = (uint)second + daysToSeconds; DateTime dtBase = new DateTime(1900, 01, 01, 0, 0, 0); DateTime test = dtBase.AddSeconds((double)totalSeconds); DateTime retVal = test; return retVal; } public static DateTime importFromDelphi(string rawTime) { return importFromDelphi((strtodDefault(rawTime, 0.0))); } public static DateTime importTime(string rawTime) { if (rawTime.Contains('.')) { return importFromDelphi(rawTime); } Int64 asInt = strtollDefault(rawTime, 0); const Int64 MIN_DELPHI_TIME = 3654; const Int64 MAX_DELPHI_TIME = 76703; // MIN_DELPHI_TIME would be 1/1/1910 if we interpreted it as a Delphi time, // or 1/1/1970 1:00:54 AM Pacific if we interpreted it as UNIX time. // MAX_DELPHI_TIME would be 1/1/2110 if we interpreted it as a Delphi time, // or 1/1/1970 9:18:23 PM. If a value is between these two values, we // assume the input was a Delphi time. Otherwise we assume it's a UNIX time. // This seems like a reasonable guess. if ((asInt < MAX_DELPHI_TIME) && (asInt > MIN_DELPHI_TIME)) { return importFromDelphi(asInt); } if (asInt == 0) //have an invalid time { return new DateTime(); //this can represent a "null" time (1/1/001) } return ToCSharpTime(asInt); //asInt would be a unix time in this scenario } public static DateTime ToCSharpTime(long unixTime) { DateTime unixStartTime = new DateTime(1970, 1, 1, 0, 0, 0, 0); return unixStartTime.AddSeconds(Convert.ToDouble(unixTime)); } } }