Skip to content

Commit d219eab

Browse files
authored
Add DateTime.Parse, TryParse and Convert.ToDateTime (#169)
1 parent 94f8bd6 commit d219eab

File tree

4 files changed

+138
-1
lines changed

4 files changed

+138
-1
lines changed

Tests/NFUnitTestSystemLib/UnitTestDateTime.cs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2287,6 +2287,68 @@ public void DateTime_AboveMaxDatTime_ArgumentOutOfRangeExceptionTest59()
22872287
Assert.Throws(typeof(ArgumentOutOfRangeException), () => { DateTime dt2 = new DateTime(10000, 1, 1, 0, 0, 0, 0); });
22882288
}
22892289

2290+
[TestMethod]
2291+
public void DateTime_ParseTest00()
2292+
{
2293+
// perform 10 random conversions
2294+
for (int i = 0; i < 10; i++)
2295+
{
2296+
DateTime dt = GetRandomDateTime();
2297+
2298+
// UniversalSortableDateTimePattern
2299+
var specifier1 = "u";
2300+
2301+
string dtOutput1 = dt.ToString(specifier1);
2302+
2303+
Assert.Equal(DateTime.Parse(dtOutput1).ToString(specifier1), dt.ToString(specifier1), $"DateTime.Parse '{dt}' failed");
2304+
2305+
Assert.True(DateTime.TryParse(dtOutput1, out DateTime result), $"DateTime.TryParse '{dt}' failed");
2306+
Assert.Equal(result.ToString(specifier1), dt.ToString(specifier1), $"DateTime.TryParse '{dt}' returning wrong value: '{result}'");
2307+
}
2308+
}
2309+
2310+
[TestMethod]
2311+
public void DateTime_ParseTest01()
2312+
{
2313+
// perform 10 random conversions
2314+
for (int i = 0; i < 10; i++)
2315+
{
2316+
DateTime dt = GetRandomDateTime();
2317+
2318+
// Round Trip ISO 8601 compatible
2319+
var specifier1 = "o";
2320+
2321+
string dtOutput1 = dt.ToString(specifier1);
2322+
2323+
// expected format is yyyy-MM-ddTHH:mm:ss.fffffffK
2324+
Assert.Equal(DateTime.Parse(dtOutput1).ToString(specifier1), dt.ToString(specifier1), $"Parsing DateTime '{dt}' failed");
2325+
2326+
Assert.True(DateTime.TryParse(dtOutput1, out DateTime result), $"DateTime.TryParse '{dt}' failed");
2327+
Assert.Equal(result.ToString(specifier1), dt.ToString(specifier1), $"DateTime.TryParse '{dt}' returning wrong value: '{result}'");
2328+
}
2329+
}
2330+
2331+
[TestMethod]
2332+
public void DateTime_ParseTest02()
2333+
{
2334+
// perform 10 random conversions
2335+
for (int i = 0; i < 10; i++)
2336+
{
2337+
DateTime dt = GetRandomDateTime();
2338+
2339+
// RFC 1123 date
2340+
var specifier1 = "r";
2341+
2342+
string dtOutput1 = dt.ToString(specifier1);
2343+
2344+
// expected format is ddd, dd MMM yyyy HH':'mm':'ss 'GMT'
2345+
Assert.Equal(DateTime.Parse(dtOutput1).ToString(specifier1), dt.ToString(specifier1), $"Parsing DateTime '{dt}' failed");
2346+
2347+
Assert.True(DateTime.TryParse(dtOutput1, out DateTime result), $"DateTime.TryParse '{dt}' failed");
2348+
Assert.Equal(result.ToString(specifier1), dt.ToString(specifier1), $"DateTime.TryParse '{dt}' returning wrong value: '{result}'");
2349+
}
2350+
}
2351+
22902352
static double[] rdmFraction = new double[] { 0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9 };
22912353

22922354
static int year, month, day, hour, minute, second, millisec;

nanoFramework.CoreLibrary/System/Convert.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ public static class Convert
3737
[MethodImpl(MethodImplOptions.InternalCall)]
3838
internal static extern double NativeToDouble(string value, bool throwException, out bool success);
3939

40+
[MethodImpl(MethodImplOptions.InternalCall)]
41+
internal static extern DateTime NativeToDateTime(string value, bool throwException, out bool success);
42+
4043
/// <summary>
4144
/// Converts the value of the specified 8-bit unsigned integer to an equivalent Boolean value.
4245
/// </summary>
@@ -121,6 +124,31 @@ public static byte ToByte(bool value)
121124
return value ? (byte)1 : (byte)0;
122125
}
123126

127+
/// <summary>
128+
/// Converts the specified string representation of a date and time to an equivalent date and time value.
129+
/// </summary>
130+
/// <param name="value">The string representation of a date and time.</param>
131+
/// <returns>The date and time equivalent of the value of <paramref name="value"/>, or the date and time equivalent of <see cref="DateTime.MinValue"/> if value is null.</returns>
132+
/// <exception cref="FormatException"><paramref name="value"/> is not a properly formatted date and time string.</exception>
133+
/// <remarks>
134+
/// <para>
135+
/// If <paramref name="value"/> is not null, the return value is the result of invoking the <see cref="DateTime.Parse"/> method on value using the formatting information of the Invariant Culture. The <paramref name="value"/> argument must contain the representation of a date and time in one of the formats described in the DateTimeFormatInfo topic. If <paramref name="value"/> is <see langword="null"/>, the method returns <see cref="DateTime.MinValue"/>.
136+
/// </para>
137+
/// <para>
138+
/// This method tries to parse <paramref name="value"/> completely and avoid throwing a <see cref="FormatException"/>. It completes missing month, day, and year information with the current date. If value contains only a date and no time, this method assumes a time of midnight. Any leading, inner, or trailing white-space characters in value are ignored.
139+
/// </para>
140+
/// <para>
141+
/// If you prefer not to handle an exception if the conversion fails, you can call the <see cref="DateTime.TryParse"/> method instead. It returns a <see cref="bool"/> value that indicates whether the conversion succeeded or failed.
142+
/// </para>
143+
/// </remarks>
144+
public static DateTime ToDateTime(string value)
145+
{
146+
return NativeToDateTime(
147+
value,
148+
true,
149+
out _);
150+
}
151+
124152
/// <summary>
125153
/// Converts the specified string representation of a number to an equivalent 16-bit signed integer.
126154
/// </summary>

nanoFramework.CoreLibrary/System/DateTime.cs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -787,6 +787,53 @@ public override int GetHashCode()
787787

788788
}
789789

790+
/// <summary>
791+
/// Converts the string representation of a date and time to its <see cref="DateTime"/> equivalent by using the conventions of the current culture.
792+
/// </summary>
793+
/// <param name="s">A string that contains a date and time to convert. See The string to parse for more information.</param>
794+
/// <returns>An object that is equivalent to the date and time contained in <paramref name="s"/>.</returns>
795+
/// <exception cref="ArgumentNullException"><paramref name="s"/> is <see langword="null"/>.</exception>
796+
/// <exception cref="FormatException">Failed to parse <paramref name="s"/>.</exception>
797+
/// <remarks>
798+
/// <para>
799+
/// .NET nanoFramework doesn't support local times so converted values will always have <see cref="Kind"/> set to <see cref="DateTimeKind.Utc"/>.
800+
/// </para>
801+
/// <para>
802+
/// This attempts to parse <paramref name="s"/> by using the formatting conventions of Invariant Culture.
803+
/// </para>
804+
/// </remarks>
805+
public static DateTime Parse(string s)
806+
{
807+
// check for null string is carried out in native code
808+
return Convert.ToDateTime(s);
809+
}
810+
811+
/// <summary>
812+
/// Converts the specified string representation of a date and time to its <see cref="DateTime"/> equivalent and returns a value that indicates whether the conversion succeeded.
813+
/// </summary>
814+
/// <param name="s">A string containing a date and time to convert.</param>
815+
/// <param name="result">When this method returns, contains the <see cref="DateTime"/> value equivalent to the date and time contained in <paramref name="s"/>, if the conversion succeeded, or <see cref="MinValue"/> if the conversion failed. The conversion fails if the <paramref name="s"/> parameter is <see langword="null"/>, is an <see cref="string.Empty"/>, or does not contain a valid string representation of a date and time. This parameter is passed uninitialized.</param>
816+
/// <returns><see langword="true"/> if the <paramref name="s"/> parameter was converted successfully; otherwise, <see langword="false"/>.</returns>
817+
/// <remarks>
818+
/// <para>
819+
/// The <see cref="TryParse"/> method is similar to the <see cref="Parse"/> method, except that the <see cref="TryParse"/> method does not throw an exception if the conversion fails.
820+
/// </para>
821+
/// <para>
822+
/// The string <paramref name="s"/> is parsed using formatting information of the Invariant Culture.
823+
/// </para>
824+
/// </remarks>
825+
public static bool TryParse(
826+
string s,
827+
out DateTime result)
828+
{
829+
result = Convert.NativeToDateTime(
830+
s,
831+
false,
832+
out bool success);
833+
834+
return success;
835+
}
836+
790837
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
791838
private string DateTimeDisplay => $"{{{new DateTime(Ticks).ToString()}}}";
792839

nanoFramework.CoreLibrary/System/Double.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ public static bool TryParse(
181181
{
182182
result = Convert.NativeToDouble(
183183
s,
184-
true,
184+
false,
185185
out bool success);
186186

187187
return success;

0 commit comments

Comments
 (0)