实现原理:取出给定的两个时间差的秒数,再在0到该秒数之间随机获取一个整数,将其做为秒添加到较小的时间上,比如将大于 System.Int32.MaxValue 或小于 System.Int32.MinValue 的值转成 int 时,如果直接在变量前加上类型名转换((int)d),不会有有异常产生,但得到的值却是 System.Int32.MinValue,还有就是对于Math.Abs 方法,当参数 value 等于 MinValue 将会引发 System.OverflowException 异常。
一、功能代码
using System;
namespace Yyw
{
public class DateTimeHelper
{
public static DateTime GetRandomTime(DateTime time1, DateTime time2)
{
Random random = new Random();
return GetRandomTime(time1, time2, random);
}
public static DateTime GetRandomTime(DateTime time1, DateTime time2, Random random)
{
DateTime minTime = new DateTime();
DateTime maxTime = new DateTime();
System.TimeSpan ts = new System.TimeSpan(time1.Ticks - time2.Ticks);
// 获取两个时间相隔的秒数
double dTotalSecontds = ts.TotalSeconds;
int iTotalSecontds = 0;
if (dTotalSecontds > System.Int32.MaxValue)
{
iTotalSecontds = System.Int32.MaxValue;
}
else if (dTotalSecontds < System.Int32.MinValue)
{
iTotalSecontds = System.Int32.MinValue;
}
else
{
iTotalSecontds = (int)dTotalSecontds;
}
if (iTotalSecontds > 0)
{
minTime = time2;
maxTime = time1;
}
else if (iTotalSecontds < 0)
{
minTime = time1;
maxTime = time2;
}
else
{
return time1;
}
int maxValue = iTotalSecontds;
if (iTotalSecontds <= System.Int32.MinValue)
maxValue = System.Int32.MinValue + 1;
int i = random.Next(System.Math.Abs(maxValue));
return minTime.AddSeconds(i);
}
}
}
二、调用代码
using System;
using NUnit.Framework;
namespace Yyw
{
[TestFixture]
public class DateTimeHelperFixture
{
[Test]
public void TestGetRandomTime()
{
DateTime minTime = DateTime.MinValue;
DateTime maxTime = DateTime.MaxValue;
Random random = new Random();
for (int i = 0; i < 10000; i++)
{
DateTime randomTime = DateTimeHelper.GetRandomTime(maxTime, minTime, random);
CheckResult(minTime, maxTime, randomTime);
}
for (int i = 0; i < 10000; i++)
{
DateTime randomTime = DateTimeHelper.GetRandomTime(minTime, maxTime, random);
CheckResult(minTime, maxTime, randomTime);
}
}
private void CheckResult(DateTime minTime, DateTime maxTime, DateTime randomTime)
{
System.TimeSpan ts1 = new System.TimeSpan(randomTime.Ticks - minTime.Ticks);
System.TimeSpan ts2 = new System.TimeSpan(randomTime.Ticks - maxTime.Ticks);
Assert.IsTrue(ts1.Seconds >= 0 && ts2.Seconds <= 0);
}
}
}