internal bool TryParsePreviousCoastDuration(string text, out double value) { value = 0; if (!PrincipiaTimeSpan.TryParse(text, out PrincipiaTimeSpan ts)) { return(false); } value = ts.total_seconds; return(true); }
internal bool TryParsePlanLength(string text, out double value) { value = 0; if (!PrincipiaTimeSpan.TryParse(text, out PrincipiaTimeSpan ts)) { return(false); } value = ts.total_seconds + plugin.FlightPlanGetInitialTime(predicted_vessel.id.ToString()); return(true); }
public static bool TryParse(string text, bool with_seconds, out PrincipiaTimeSpan time_span) { time_span = new PrincipiaTimeSpan(double.NaN); // Using a technology that is customarily used to parse HTML. string pattern = @"^[+]?\s*(\d+)\s*" + day_symbol + @"\s*(\d+)\s*h\s*(\d+)\s*min"; if (with_seconds) { pattern += @"\s*([0-9.,']+)\s*s$"; } else { pattern += @"$"; } var regex = new Regex(pattern); var match = regex.Match(text); if (!match.Success) { return(false); } string days = match.Groups[1].Value; string hours = match.Groups[2].Value; string minutes = match.Groups[3].Value; string seconds = "0"; if (with_seconds) { seconds = match.Groups[4].Value; } if (!int.TryParse(days, out int d) || !int.TryParse(hours, out int h) || !int.TryParse(minutes, out int min) || !double.TryParse(seconds.Replace(',', '.'), NumberStyles.AllowDecimalPoint | NumberStyles.AllowThousands, Culture.culture.NumberFormat, out double s)) { return(false); } time_span = new PrincipiaTimeSpan(d * date_time_formatter.Day + h * date_time_formatter.Hour + min * date_time_formatter.Minute + s); return(true); }
public static bool TryParseMissionDuration(string str, out double seconds) { seconds = 0; if (PrincipiaTimeSpan.TryParse(str, out PrincipiaTimeSpan ts)) { seconds = ts.total_seconds; return(true); } else { return(false); } }
public static bool TryParse(string text, out PrincipiaTimeSpan time_span) { time_span = new PrincipiaTimeSpan(double.NaN); // Using a technology that is customarily used to parse HTML. string pattern = @"^[+]?\s*(?:(?<days>\d+)\s*" + day_symbol + @"\s*)?" + @"(?:(?<hours>\d+)\s*h\s*)?" + @"(?:(?<minutes>\d+)\s*min\s*)?" + @"(?:(?<seconds>[0-9.,']+)\s*s\s*)?$"; var regex = new Regex(pattern); var match = regex.Match(text); if (!match.Success) { return(false); } var days_group = match.Groups["days"]; var hours_group = match.Groups["hours"]; var minutes_group = match.Groups["minutes"]; var seconds_group = match.Groups["seconds"]; string days = days_group.Success ? days_group.Value : "0"; string hours = hours_group.Success ? hours_group.Value : "0"; string minutes = minutes_group.Success ? minutes_group.Value : "0"; string seconds = seconds_group.Success ? seconds_group.Value : "0"; if (!int.TryParse(days, out int d) || !int.TryParse(hours, out int h) || !int.TryParse(minutes, out int min) || !double.TryParse(seconds.Replace(',', '.'), NumberStyles.AllowDecimalPoint | NumberStyles.AllowThousands, Culture.culture.NumberFormat, out double s)) { return(false); } time_span = new PrincipiaTimeSpan(d * date_time_formatter.Day + h * date_time_formatter.Hour + min * date_time_formatter.Minute + s); return(true); }
public static bool TryParse(string text, out PrincipiaTimeSpan time_span) { time_span = new PrincipiaTimeSpan(double.NaN); // Using a technology that is customarily used to parse HTML. // Wrapping the literal in a Regex constructor and then substituting the day // symbols in order to get VS to syntax highlight the regex. var regex = new Regex(@" ^[+]?\s* (?:(?<days>\d+)\s*(?:{day_symbol}|{short_day_symbol})\s*)? (?:(?<hours>\d+)\s*[hʰ]\s*)? (?:(?<minutes>\d+)\s*(?:[mᵐ]|min)\s*)? (?:(?<seconds>[0-9.,']+)\s*[sˢ]\s*| (?<integer_seconds>[0-9']+)[sˢ][.,] (?<fractional_seconds>[0-9']+))?$", RegexOptions.IgnorePatternWhitespace); regex = new Regex( regex.ToString().Replace("{day_symbol}", day_symbol) .Replace("{short_day_symbol}", short_day_symbol), RegexOptions.IgnorePatternWhitespace); var match = regex.Match(text); if (!match.Success) { return(false); } var days_group = match.Groups["days"]; var hours_group = match.Groups["hours"]; var minutes_group = match.Groups["minutes"]; var seconds_group = match.Groups["seconds"]; var integer_seconds_group = match.Groups["integer_seconds"]; var fractional_seconds_group = match.Groups["fractional_seconds"]; string days = days_group.Success ? days_group.Value : "0"; string hours = hours_group.Success ? hours_group.Value : "0"; string minutes = minutes_group.Success ? minutes_group.Value : "0"; string seconds = seconds_group.Success ? seconds_group.Value : integer_seconds_group.Success ? string.Join(".", integer_seconds_group.Value, fractional_seconds_group.Value) : "0"; if (!int.TryParse(days, out int d) || !int.TryParse(hours, out int h) || !int.TryParse(minutes, out int min) || !double.TryParse(seconds.Replace(',', '.').Replace("'", ""), NumberStyles.AllowDecimalPoint | NumberStyles.AllowThousands, Culture.culture.NumberFormat, out double s)) { return(false); } time_span = new PrincipiaTimeSpan( Convert.ToDouble(d) * date_time_formatter.Day + Convert.ToDouble(h) * date_time_formatter.Hour + Convert.ToDouble(min) * date_time_formatter.Minute + s); return(true); }