/*
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Globalization;
namespace QuantConnect
{
///
/// Provides methods for parsing strings using
///
public static class Parse
{
///
/// Parses the provided value as a using
/// with
///
public static TimeSpan TimeSpan(string value)
{
return System.TimeSpan.Parse(value, CultureInfo.InvariantCulture);
}
///
/// Tries to parse the provided value with TryParse as a using .
///
public static bool TryParse(string input, out TimeSpan value)
{
return System.TimeSpan.TryParse(input, CultureInfo.InvariantCulture, out value);
}
///
/// Tries to parse the provided value with TryParse as a , format
/// string, , and using
///
///
///
///
///
///
public static bool TryParseExact(string input, string format, TimeSpanStyles timeSpanStyle, out TimeSpan value)
{
return System.TimeSpan.TryParseExact(input, format, CultureInfo.InvariantCulture, timeSpanStyle, out value);
}
///
/// Parses the provided value as a using
/// with
///
public static DateTime DateTime(string value)
{
return System.DateTime.Parse(value, CultureInfo.InvariantCulture);
}
///
/// Parses the provided value as a using
/// with the specified and
///
public static DateTime DateTimeExact(string value, string format)
{
return System.DateTime.ParseExact(value, format, CultureInfo.InvariantCulture);
}
///
/// Parses the provided value as a using
/// with the specified , and
///
public static DateTime DateTimeExact(string value, string format, DateTimeStyles dateTimeStyles)
{
return System.DateTime.ParseExact(value, format, CultureInfo.InvariantCulture, dateTimeStyles);
}
///
/// Tries to parse the provided value with TryParse as a using the specified
/// and .
///
public static bool TryParse(string input, DateTimeStyles dateTimeStyle, out System.DateTime value)
{
return System.DateTime.TryParse(input, CultureInfo.InvariantCulture, dateTimeStyle, out value);
}
///
/// Tries to parse the provided value with TryParse as a using the
/// specified , the format , and
/// .
///
public static bool TryParseExact(string input, string format, DateTimeStyles dateTimeStyle, out System.DateTime value)
{
return System.DateTime.TryParseExact(input, format, CultureInfo.InvariantCulture, dateTimeStyle, out value);
}
///
/// Parses the provided value as a using
///
public static double Double(string value)
{
return double.Parse(value, CultureInfo.InvariantCulture);
}
///
/// Tries to parse the provided value with TryParse as a using the specified
/// and .
///
public static bool TryParse(string input, NumberStyles numberStyle, out double value)
{
return double.TryParse(input, numberStyle, CultureInfo.InvariantCulture, out value);
}
///
/// Parses the provided value as a using
///
public static decimal Decimal(string value)
{
return decimal.Parse(value, CultureInfo.InvariantCulture);
}
///
/// Parses the provided value as a using the specified
/// and
///
public static decimal Decimal(string value, NumberStyles numberStyles)
{
return decimal.Parse(value, numberStyles, CultureInfo.InvariantCulture);
}
///
/// Tries to parse the provided value with TryParse as a using the specified
/// and .
///
public static bool TryParse(string input, NumberStyles numberStyle, out decimal value)
{
return decimal.TryParse(input, numberStyle, CultureInfo.InvariantCulture, out value);
}
///
/// Parses the provided value as a using
///
public static int Int(string value)
{
return int.Parse(value, CultureInfo.InvariantCulture);
}
///
/// Tries to parse the provided value with TryParse as a using the specified
/// and .
///
public static bool TryParse(string input, NumberStyles numberStyle, out int value)
{
return int.TryParse(input, numberStyle, CultureInfo.InvariantCulture, out value);
}
///
/// Parses the provided value as a using
///
public static long Long(string value)
{
return long.Parse(value, CultureInfo.InvariantCulture);
}
///
/// Parses the provided value as a using
/// and the specified
///
public static long Long(string value, NumberStyles numberStyles)
{
return long.Parse(value, numberStyles, CultureInfo.InvariantCulture);
}
///
/// Tries to parse the provided value with TryParse as a using the specified
/// and .
///
public static bool TryParse(string input, NumberStyles numberStyle, out long value)
{
return long.TryParse(input, numberStyle, CultureInfo.InvariantCulture, out value);
}
///
/// Parses the provided value as a an enumeration type
///
public static T Enum(string input, bool ignoreCase = true)
where T : struct, IConvertible
{
T value;
if (!TryParse(input, out value, ignoreCase))
{
throw new ArgumentException(Messages.Parse.ValueIsNotParseable(input, typeof(T)));
}
return value;
}
///
/// Parses the provided value as a an enumeration type
///
public static bool TryParse(string input, out T value, bool ignoreCase = true)
where T : struct, IConvertible
{
return System.Enum.TryParse(input, ignoreCase, out value);
}
}
}