Lean  $LEAN_TAG$
Parse.cs
1 /*
2  * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
3  * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14 */
15 
16 using System;
17 using System.Globalization;
18 
19 namespace QuantConnect
20 {
21  /// <summary>
22  /// Provides methods for parsing strings using <see cref="CultureInfo.InvariantCulture"/>
23  /// </summary>
24  public static class Parse
25  {
26  /// <summary>
27  /// Parses the provided value as a <see cref="System.TimeSpan"/> using <see cref="System.TimeSpan.Parse(string,IFormatProvider)"/>
28  /// with <see cref="CultureInfo.InvariantCulture"/>
29  /// </summary>
30  public static TimeSpan TimeSpan(string value)
31  {
32  return System.TimeSpan.Parse(value, CultureInfo.InvariantCulture);
33  }
34 
35  /// <summary>
36  /// Tries to parse the provided value with TryParse as a <see cref="System.TimeSpan"/> using <see cref="CultureInfo.InvariantCulture"/>.
37  /// </summary>
38  public static bool TryParse(string input, out TimeSpan value)
39  {
40  return System.TimeSpan.TryParse(input, CultureInfo.InvariantCulture, out value);
41 
42  }
43 
44  /// <summary>
45  /// Tries to parse the provided value with TryParse as a <see cref="System.TimeSpan"/>, format
46  /// string, <see cref="TimeSpanStyles"/>, and using <see cref="CultureInfo.InvariantCulture"/>
47  /// </summary>
48  /// <param name="input"></param>
49  /// <param name="format"></param>
50  /// <param name="timeSpanStyle"></param>
51  /// <param name="value"></param>
52  /// <returns></returns>
53  public static bool TryParseExact(string input, string format, TimeSpanStyles timeSpanStyle, out TimeSpan value)
54  {
55  return System.TimeSpan.TryParseExact(input, format, CultureInfo.InvariantCulture, timeSpanStyle, out value);
56  }
57 
58  /// <summary>
59  /// Parses the provided value as a <see cref="System.DateTime"/> using <see cref="System.DateTime.Parse(string,IFormatProvider)"/>
60  /// with <see cref="CultureInfo.InvariantCulture"/>
61  /// </summary>
62  public static DateTime DateTime(string value)
63  {
64  return System.DateTime.Parse(value, CultureInfo.InvariantCulture);
65  }
66 
67  /// <summary>
68  /// Parses the provided value as a <see cref="System.DateTime"/> using <see cref="System.DateTime.ParseExact(string,string,IFormatProvider)"/>
69  /// with the specified <paramref name="format"/> and <see cref="CultureInfo.InvariantCulture"/>
70  /// </summary>
71  public static DateTime DateTimeExact(string value, string format)
72  {
73  return System.DateTime.ParseExact(value, format, CultureInfo.InvariantCulture);
74  }
75 
76  /// <summary>
77  /// Parses the provided value as a <see cref="System.DateTime"/> using <see cref="System.DateTime.ParseExact(string,string,IFormatProvider)"/>
78  /// with the specified <paramref name="format"/>, <paramref name="dateTimeStyles"/> and <see cref="CultureInfo.InvariantCulture"/>
79  /// </summary>
80  public static DateTime DateTimeExact(string value, string format, DateTimeStyles dateTimeStyles)
81  {
82  return System.DateTime.ParseExact(value, format, CultureInfo.InvariantCulture, dateTimeStyles);
83  }
84 
85  /// <summary>
86  /// Tries to parse the provided value with TryParse as a <see cref="System.DateTime"/> using the specified <paramref name="dateTimeStyle"/>
87  /// and <see cref="CultureInfo.InvariantCulture"/>.
88  /// </summary>
89  public static bool TryParse(string input, DateTimeStyles dateTimeStyle, out System.DateTime value)
90  {
91  return System.DateTime.TryParse(input, CultureInfo.InvariantCulture, dateTimeStyle, out value);
92  }
93 
94  /// <summary>
95  /// Tries to parse the provided value with TryParse as a <see cref="System.DateTime"/> using the
96  /// specified <paramref name="dateTimeStyle"/>, the format <paramref name="format"/>, and
97  /// <see cref="CultureInfo.InvariantCulture"/>.
98  /// </summary>
99  public static bool TryParseExact(string input, string format, DateTimeStyles dateTimeStyle, out System.DateTime value)
100  {
101  return System.DateTime.TryParseExact(input, format, CultureInfo.InvariantCulture, dateTimeStyle, out value);
102  }
103 
104  /// <summary>
105  /// Parses the provided value as a <see cref="double"/> using <see cref="CultureInfo.InvariantCulture"/>
106  /// </summary>
107  public static double Double(string value)
108  {
109  return double.Parse(value, CultureInfo.InvariantCulture);
110  }
111 
112  /// <summary>
113  /// Tries to parse the provided value with TryParse as a <see cref="double"/> using the specified <paramref name="numberStyle"/>
114  /// and <see cref="CultureInfo.InvariantCulture"/>.
115  /// </summary>
116  public static bool TryParse(string input, NumberStyles numberStyle, out double value)
117  {
118  return double.TryParse(input, numberStyle, CultureInfo.InvariantCulture, out value);
119  }
120 
121  /// <summary>
122  /// Parses the provided value as a <see cref="decimal"/> using <see cref="CultureInfo.InvariantCulture"/>
123  /// </summary>
124  public static decimal Decimal(string value)
125  {
126  return decimal.Parse(value, CultureInfo.InvariantCulture);
127  }
128 
129  /// <summary>
130  /// Parses the provided value as a <see cref="decimal"/> using the specified <paramref name="numberStyles"/>
131  /// and <see cref="CultureInfo.InvariantCulture"/>
132  /// </summary>
133  public static decimal Decimal(string value, NumberStyles numberStyles)
134  {
135  return decimal.Parse(value, numberStyles, CultureInfo.InvariantCulture);
136  }
137 
138  /// <summary>
139  /// Tries to parse the provided value with TryParse as a <see cref="decimal"/> using the specified <paramref name="numberStyle"/>
140  /// and <see cref="CultureInfo.InvariantCulture"/>.
141  /// </summary>
142  public static bool TryParse(string input, NumberStyles numberStyle, out decimal value)
143  {
144  return decimal.TryParse(input, numberStyle, CultureInfo.InvariantCulture, out value);
145  }
146 
147  /// <summary>
148  /// Parses the provided value as a <see cref="int"/> using <see cref="CultureInfo.InvariantCulture"/>
149  /// </summary>
150  public static int Int(string value)
151  {
152  return int.Parse(value, CultureInfo.InvariantCulture);
153  }
154 
155  /// <summary>
156  /// Tries to parse the provided value with TryParse as a <see cref="int"/> using the specified <paramref name="numberStyle"/>
157  /// and <see cref="CultureInfo.InvariantCulture"/>.
158  /// </summary>
159  public static bool TryParse(string input, NumberStyles numberStyle, out int value)
160  {
161  return int.TryParse(input, numberStyle, CultureInfo.InvariantCulture, out value);
162  }
163 
164  /// <summary>
165  /// Parses the provided value as a <see cref="long"/> using <see cref="CultureInfo.InvariantCulture"/>
166  /// </summary>
167  public static long Long(string value)
168  {
169  return long.Parse(value, CultureInfo.InvariantCulture);
170  }
171 
172  /// <summary>
173  /// Parses the provided value as a <see cref="long"/> using <see cref="CultureInfo.InvariantCulture"/>
174  /// and the specified <paramref name="numberStyles"/>
175  /// </summary>
176  public static long Long(string value, NumberStyles numberStyles)
177  {
178  return long.Parse(value, numberStyles, CultureInfo.InvariantCulture);
179  }
180 
181  /// <summary>
182  /// Tries to parse the provided value with TryParse as a <see cref="long"/> using the specified <paramref name="numberStyle"/>
183  /// and <see cref="CultureInfo.InvariantCulture"/>.
184  /// </summary>
185  public static bool TryParse(string input, NumberStyles numberStyle, out long value)
186  {
187  return long.TryParse(input, numberStyle, CultureInfo.InvariantCulture, out value);
188  }
189 
190  /// <summary>
191  /// Parses the provided value as a an enumeration type <typeparamref name="T"/>
192  /// </summary>
193  public static T Enum<T>(string input, bool ignoreCase = true)
194  where T : struct, IConvertible
195  {
196  T value;
197  if (!TryParse(input, out value, ignoreCase))
198  {
199  throw new ArgumentException(Messages.Parse.ValueIsNotParseable(input, typeof(T)));
200  }
201 
202  return value;
203  }
204 
205  /// <summary>
206  /// Parses the provided value as a an enumeration type <typeparamref name="T"/>
207  /// </summary>
208  public static bool TryParse<T>(string input, out T value, bool ignoreCase = true)
209  where T : struct, IConvertible
210  {
211  return System.Enum.TryParse<T>(input, ignoreCase, out value);
212  }
213  }
214 }