Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions Tests/NFUnitTestSystemLib/UnitTestDateTime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2287,6 +2287,68 @@ public void DateTime_AboveMaxDatTime_ArgumentOutOfRangeExceptionTest59()
Assert.Throws(typeof(ArgumentOutOfRangeException), () => { DateTime dt2 = new DateTime(10000, 1, 1, 0, 0, 0, 0); });
}

[TestMethod]
public void DateTime_ParseTest00()
{
// perform 10 random conversions
for (int i = 0; i < 10; i++)
{
DateTime dt = GetRandomDateTime();

// UniversalSortableDateTimePattern
var specifier1 = "u";

string dtOutput1 = dt.ToString(specifier1);

Assert.Equal(DateTime.Parse(dtOutput1).ToString(specifier1), dt.ToString(specifier1), $"DateTime.Parse '{dt}' failed");

Assert.True(DateTime.TryParse(dtOutput1, out DateTime result), $"DateTime.TryParse '{dt}' failed");
Assert.Equal(result.ToString(specifier1), dt.ToString(specifier1), $"DateTime.TryParse '{dt}' returning wrong value: '{result}'");
}
}

[TestMethod]
public void DateTime_ParseTest01()
{
// perform 10 random conversions
for (int i = 0; i < 10; i++)
{
DateTime dt = GetRandomDateTime();

// Round Trip ISO 8601 compatible
var specifier1 = "o";

string dtOutput1 = dt.ToString(specifier1);

// expected format is yyyy-MM-ddTHH:mm:ss.fffffffK
Assert.Equal(DateTime.Parse(dtOutput1).ToString(specifier1), dt.ToString(specifier1), $"Parsing DateTime '{dt}' failed");

Assert.True(DateTime.TryParse(dtOutput1, out DateTime result), $"DateTime.TryParse '{dt}' failed");
Assert.Equal(result.ToString(specifier1), dt.ToString(specifier1), $"DateTime.TryParse '{dt}' returning wrong value: '{result}'");
}
}

[TestMethod]
public void DateTime_ParseTest02()
{
// perform 10 random conversions
for (int i = 0; i < 10; i++)
{
DateTime dt = GetRandomDateTime();

// RFC 1123 date
var specifier1 = "r";

string dtOutput1 = dt.ToString(specifier1);

// expected format is ddd, dd MMM yyyy HH':'mm':'ss 'GMT'
Assert.Equal(DateTime.Parse(dtOutput1).ToString(specifier1), dt.ToString(specifier1), $"Parsing DateTime '{dt}' failed");

Assert.True(DateTime.TryParse(dtOutput1, out DateTime result), $"DateTime.TryParse '{dt}' failed");
Assert.Equal(result.ToString(specifier1), dt.ToString(specifier1), $"DateTime.TryParse '{dt}' returning wrong value: '{result}'");
}
}

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 };

static int year, month, day, hour, minute, second, millisec;
Expand Down
28 changes: 28 additions & 0 deletions nanoFramework.CoreLibrary/System/Convert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ public static class Convert
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern double NativeToDouble(string value, bool throwException, out bool success);

[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern DateTime NativeToDateTime(string value, bool throwException, out bool success);

/// <summary>
/// Converts the value of the specified 8-bit unsigned integer to an equivalent Boolean value.
/// </summary>
Expand Down Expand Up @@ -121,6 +124,31 @@ public static byte ToByte(bool value)
return value ? (byte)1 : (byte)0;
}

/// <summary>
/// Converts the specified string representation of a date and time to an equivalent date and time value.
/// </summary>
/// <param name="value">The string representation of a date and time.</param>
/// <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>
/// <exception cref="FormatException"><paramref name="value"/> is not a properly formatted date and time string.</exception>
/// <remarks>
/// <para>
/// 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"/>.
/// </para>
/// <para>
/// 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.
/// </para>
/// <para>
/// 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.
/// </para>
/// </remarks>
public static DateTime ToDateTime(string value)
{
return NativeToDateTime(
value,
true,
out _);
}

/// <summary>
/// Converts the specified string representation of a number to an equivalent 16-bit signed integer.
/// </summary>
Expand Down
47 changes: 47 additions & 0 deletions nanoFramework.CoreLibrary/System/DateTime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,53 @@ public override int GetHashCode()

}

/// <summary>
/// Converts the string representation of a date and time to its <see cref="DateTime"/> equivalent by using the conventions of the current culture.
/// </summary>
/// <param name="s">A string that contains a date and time to convert. See The string to parse for more information.</param>
/// <returns>An object that is equivalent to the date and time contained in <paramref name="s"/>.</returns>
/// <exception cref="ArgumentNullException"><paramref name="s"/> is <see langword="null"/>.</exception>
/// <exception cref="FormatException">Failed to parse <paramref name="s"/>.</exception>
/// <remarks>
/// <para>
/// .NET nanoFramework doesn't support local times so converted values will always have <see cref="Kind"/> set to <see cref="DateTimeKind.Utc"/>.
/// </para>
/// <para>
/// This attempts to parse <paramref name="s"/> by using the formatting conventions of Invariant Culture.
/// </para>
/// </remarks>
public static DateTime Parse(string s)
{
// check for null string is carried out in native code
return Convert.ToDateTime(s);
}

/// <summary>
/// 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.
/// </summary>
/// <param name="s">A string containing a date and time to convert.</param>
/// <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>
/// <returns><see langword="true"/> if the <paramref name="s"/> parameter was converted successfully; otherwise, <see langword="false"/>.</returns>
/// <remarks>
/// <para>
/// 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.
/// </para>
/// <para>
/// The string <paramref name="s"/> is parsed using formatting information of the Invariant Culture.
/// </para>
/// </remarks>
public static bool TryParse(
string s,
out DateTime result)
{
result = Convert.NativeToDateTime(
s,
false,
out bool success);

return success;
}

[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private string DateTimeDisplay => $"{{{new DateTime(Ticks).ToString()}}}";

Expand Down
2 changes: 1 addition & 1 deletion nanoFramework.CoreLibrary/System/Double.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ public static bool TryParse(
{
result = Convert.NativeToDouble(
s,
true,
false,
out bool success);

return success;
Expand Down