代码如下:
using System;
namespace Yyw
{
public class DateTimeHelper
{
/**////
/// 获取随机时间
///
/// 由于Random 以当前系统时间做为种值,所以当快速运行多次该方法所得到的结果可能相同,
/// 这时,你应该在外部初始化 Random 实例并调用 GetRandomTime(DateTime time1, DateTime time2, Random random)
///
///
///
///
///
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);
}
}
}
0 评论:
发表评论