Ejemplo n.º 1
0
        /// <summary>
        ///     Attempts to parse the given <see cref="string"/> into a
        ///     quantity of bytes.
        /// </summary>
        /// <param name="str">
        ///     The <see cref="string"/> to parse.
        /// </param>
        /// <param name="bytes">
        ///     The quantity of bytes parsed.
        /// </param>
        /// <param name="units">
        ///     The units of the bytes parsed.
        /// </param>
        /// <param name="precision">
        ///     The precision of the parsing.
        /// </param>
        /// <returns>
        ///     <c>true</c> if <paramref name="str"/> was able to be parsed;
        ///     <c>false</c> otherwise.
        /// </returns>
        public static bool TryParse(string str, out ulong bytes, out BytesUnits units, out int precision)
        {
            if (string.IsNullOrWhiteSpace(str))
            {
                bytes     = 0;
                precision = 0;
                units     = BytesUnits.Bytes;
                return(false);
            }

            str = str.Trim();

            string numberText;

            BytesConverterUtils.SplitNumberAndUnits(str, BytesUnits.Bytes, out numberText, out units);

            decimal number;

            if (!decimal.TryParse(numberText, out number))
            {
                bytes     = 0;
                precision = 0;
                units     = BytesUnits.Bytes;
                return(false);
            }

            precision = DecimalUtils.GetPrecision(number);

            decimal tempBytes = BytesConverterUtils.ConvertToBytes(number, units);

            bool roundResult = DecimalUtils.TryRoundToUInt64(tempBytes, MidpointRounding.AwayFromZero, out bytes);

            return(roundResult);
        }
        /// <summary>
        ///     Parses the string as a timestamp which may include a specification for the units of time.
        /// </summary>
        /// <param name="s">
        ///     The string to parse.
        /// </param>
        /// <param name="defaultUnits">
        ///     The units to assume if none are specified in the string.
        /// </param>
        /// <param name="resultNS">
        ///     The parsed timestamp, in units of nanoseconds.
        /// </param>
        /// <param name="parsedUnits">
        ///     The units that were parsed from the string, or defaultUnits if none were found.
        /// </param>
        /// <returns>
        ///     <c>true</c> if the string was parsed successfully; <c>false</c> otherwise.
        /// </returns>
        public static bool TryParse(string s, TimeUnit defaultUnits, out long resultNS, out TimeUnit parsedUnits)
        {
            if (s == null)
            {
                throw new ArgumentNullException("s");
            }

            if (string.IsNullOrWhiteSpace(s))
            {
                resultNS    = 0;
                parsedUnits = TimeUnit.Nanoseconds;
                return(false);
            }

            s = s.Trim();

            string   numberText;
            TimeUnit units;

            SplitNumberAndUnits(s, defaultUnits, out numberText, out units);

            parsedUnits = units;

            decimal number;

            if (!decimal.TryParse(numberText, out number))
            {
                resultNS    = 0;
                parsedUnits = TimeUnit.Nanoseconds;
                return(false);
            }

            decimal nanoseconds = DecimalUtils.ConvertToNanoseconds(number, units);

            bool roundResult = DecimalUtils.TryRoundToInt64(nanoseconds, MidpointRounding.AwayFromZero, out resultNS);

            return(roundResult);
        }