我在TimeSpan
进行了单元测试,我不知道我是否正确行事。
测试案例:
/// <summary>
/// Wrapper around TimeSpan.TryParse that avoids out parameters.
/// Tries to parse a string as a TimeSpan and returns the parsed value as a TimeSpan or null if parsing failed.
/// </summary>
/// <param name="toParse">A string containing a value to convert</param>
/// <param name="formatProvider">An object that provides culture-specific formatting information</param>
/// <returns>The value parsed as a TimeSpan or null if parsing failed</returns>
public static TimeSpan? ParseTimeSpan(this string toParse, IFormatProvider formatProvider)
{
TimeSpan value;
if (TimeSpan.TryParse(toParse, formatProvider, out value))
{
return value;
}
return null;
}
UNIT TEST:
[TestMethod()]
public void ParseTimeSpanTest()
{
Assert.AreEqual(ParseTime("5:33 pm"), "5:33".ParseTimeSpan(CultureInfo.InvariantCulture));
Assert.AreEqual(ParseTime("9:22 AM"), "9:22 AM".ParseTimeSpan(CultureInfo.InvariantCulture));
Assert.AreEqual(ParseTime("8:22 pm"), "8:22 pm".ParseTimeSpan(CultureInfo.InvariantCulture));
Assert.AreEqual(ParseTime("7:22:44 AM"), "7:22:44 AM".ParseTimeSpan(CultureInfo.InvariantCulture));
Assert.AreEqual(ParseTime("9:22:44 pm"), "9:22:44 pm".ParseTimeSpan(CultureInfo.InvariantCulture));
Assert.AreEqual(ParseTime("13:22:14"), "13:22:14".ParseTimeSpan(CultureInfo.InvariantCulture));
Assert.AreEqual(ParseTime("23:59:00"), "23:59:00".ParseTimeSpan(CultureInfo.InvariantCulture));
Assert.AreEqual(null, "25:59:00".ParseTimeSpan(CultureInfo.InvariantCulture));
Assert.AreEqual(null, "23:70:00 ".ParseTimeSpan(CultureInfo.InvariantCulture));
Assert.AreEqual(null, "23:59:75".ParseTimeSpan(CultureInfo.InvariantCulture));
Assert.AreEqual(null, "five thirty".ParseTimeSpan(CultureInfo.InvariantCulture));
}
private static DateTime ParseTime(string time)
{
return DateTime.Parse(time, System.Globalization.CultureInfo.InvariantCulture);
}
答案 0 :(得分:0)
DateTime和TimeSpan类型之间可能存在误解。 DateTime是用于表示某个时间点的数据类型,例如1776年7月4日中午12点。 TimeSpan用于表示经过的时间/持续时间,即比赛持续12小时36分钟。
您可以使用两个DateTimes的差异来生成TimeSpan。试图将时间解析为时间跨度很难做到。例如,如何将9:30 AM转换为TimeSpan?
如果您正在寻找如何测试TimeSpan,可能以下内容会有所帮助。
public static TimeSpan? ParseTimeSpan(this string toParse, IFormatProvider formatProvider)
{
TimeSpan value;
if (TimeSpan.TryParse(toParse, formatProvider, out value))
{
return value;
}
return null;
}
[TestMethod()]
public void ParseTimeSpanTest()
{
Assert.AreEqual(ParseTime("5:33"), "5:33".ParseTimeSpan(CultureInfo.InvariantCulture));
Assert.AreEqual(ParseTime("9:22"), "9:22".ParseTimeSpan(CultureInfo.InvariantCulture));
Assert.AreEqual(ParseTime("8:22"), "8:22".ParseTimeSpan(CultureInfo.InvariantCulture));
Assert.AreEqual(ParseTime("7:22:44"), "7:22:44".ParseTimeSpan(CultureInfo.InvariantCulture));
Assert.AreEqual(ParseTime("9:22:44"), "9:22:44".ParseTimeSpan(CultureInfo.InvariantCulture));
Assert.AreEqual(ParseTime("13:22:14"), "13:22:14".ParseTimeSpan(CultureInfo.InvariantCulture));
Assert.AreEqual(ParseTime("23:59:00"), "23:59:00".ParseTimeSpan(CultureInfo.InvariantCulture));
Assert.AreEqual(null, "22:59:00 AM".ParseTimeSpan(CultureInfo.InvariantCulture));
Assert.AreEqual(null, "23:70:00 PM".ParseTimeSpan(CultureInfo.InvariantCulture));
Assert.AreEqual(null, "23:59:75 AM".ParseTimeSpan(CultureInfo.InvariantCulture));
Assert.AreEqual(null, "five thirty PM".ParseTimeSpan(CultureInfo.InvariantCulture));
var dt = DateTime.Now;
Assert.AreEqual(dt.AddHours(1) - dt, "1:00".ParseTimeSpan(CultureInfo.InvariantCulture));
}
private static TimeSpan ParseTime(string time)
{
return TimeSpan.Parse(time, CultureInfo.InvariantCulture);
}