CoerceFloat() public static method

public static CoerceFloat ( string str ) : float
str string
return float
Example #1
0
        // ex: "2 hours 1 day"
        public static DateTime FromTimeAgo(string str)
        {
            str = str.ToLowerInvariant();
            if (str.Contains("now"))
            {
                return(DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Local));
            }

            str = str.Replace(",", "");
            str = str.Replace("ago", "");
            str = str.Replace("and", "");

            TimeSpan timeAgo        = TimeSpan.Zero;
            Regex    TimeagoRegex   = new Regex(@"\s*?([\d\.]+)\s*?([^\d\s\.]+)\s*?");
            var      TimeagoMatches = TimeagoRegex.Match(str);

            while (TimeagoMatches.Success)
            {
                string expanded = string.Empty;

                var val  = ParseUtil.CoerceFloat(TimeagoMatches.Groups[1].Value);
                var unit = TimeagoMatches.Groups[2].Value;
                TimeagoMatches = TimeagoMatches.NextMatch();

                if (unit.Contains("sec") || unit == "s")
                {
                    timeAgo += TimeSpan.FromSeconds(val);
                }
                else if (unit.Contains("min") || unit == "m")
                {
                    timeAgo += TimeSpan.FromMinutes(val);
                }
                else if (unit.Contains("hour") || unit.Contains("hr") || unit == "h")
                {
                    timeAgo += TimeSpan.FromHours(val);
                }
                else if (unit.Contains("day") || unit == "d")
                {
                    timeAgo += TimeSpan.FromDays(val);
                }
                else if (unit.Contains("week") || unit.Contains("wk") || unit == "w")
                {
                    timeAgo += TimeSpan.FromDays(val * 7);
                }
                else if (unit.Contains("month") || unit == "mo")
                {
                    timeAgo += TimeSpan.FromDays(val * 30);
                }
                else if (unit.Contains("year") || unit == "y")
                {
                    timeAgo += TimeSpan.FromDays(val * 365);
                }
                else
                {
                    throw new Exception("TimeAgo parsing failed, unknown unit: " + unit);
                }
            }

            return(DateTime.SpecifyKind(DateTime.Now - timeAgo, DateTimeKind.Local));
        }
Example #2
0
        // ex: "2 hours 1 day"
        public static DateTime FromTimeAgo(string str)
        {
            str = str.ToLowerInvariant();
            if (str.Contains("now"))
            {
                return(DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Local));
            }

            var      dateParts = str.Split(new char[0], StringSplitOptions.RemoveEmptyEntries);
            TimeSpan timeAgo   = TimeSpan.Zero;

            for (var i = 0; i < dateParts.Length / 2; i++)
            {
                var val  = ParseUtil.CoerceFloat(dateParts[i * 2]);
                var unit = dateParts[i * 2 + 1];
                if (unit.Contains("sec"))
                {
                    timeAgo += TimeSpan.FromSeconds(val);
                }
                else if (unit.Contains("min"))
                {
                    timeAgo += TimeSpan.FromMinutes(val);
                }
                else if (unit.Contains("hour"))
                {
                    timeAgo += TimeSpan.FromHours(val);
                }
                else if (unit.Contains("day"))
                {
                    timeAgo += TimeSpan.FromDays(val);
                }
                else if (unit.Contains("week"))
                {
                    timeAgo += TimeSpan.FromDays(val * 7);
                }
                else if (unit.Contains("month"))
                {
                    timeAgo += TimeSpan.FromDays(val * 30);
                }
                else if (unit.Contains("year"))
                {
                    timeAgo += TimeSpan.FromDays(val * 365);
                }
                else
                {
                    throw new Exception("TimeAgo parsing failed");
                }
            }

            return(DateTime.SpecifyKind(DateTime.Now - timeAgo, DateTimeKind.Local));
        }