问题描述
大家好,我在这篇文章中上传了一张图片。在图像中有四个时间戳。第一个和最后一个是动态的,因为它们不断变化。我希望中间的两个也随着第一个和四个的差异而改变。比如Timer 1 = 2,Timer 4 = 5,那么Timer 2和Timer 3应该分别是3和4,4条线应该是平均分配的。
现在我使用 TimeSpan 以 {HH:MM} 格式显示,我不能用整数来划分时间戳,因为它不允许我这样做。那么我在这里有什么选择?
public TimeSpan HoursMinutes_01;
public double totalMinutes_01;
private string result_01;
public TimeSpan HoursMinutes_02;
public double totalMinutes_02;
private string result_02;
void Start()
{
//For First Timer
HoursMinutes_01 = new TimeSpan (1,0);
totalMinutes = HoursMinutes_01 .TotalMinutes;
result = string.Format ("{0:D2}:{1:D2}",HoursMinutes_01.Hours,HoursMinutes_01.Minutes);
//For Second Timer
HoursMinutes_02 = new TimeSpan (4,0);
totalMinutes = HoursMinutes_02 .TotalMinutes;
result = string.Format ("{0:D2}:{1:D2}",HoursMinutes_02.Hours,HoursMinutes_02.Minutes);
}
解决方法
在 this answer 中按分钟计算实际上相当昂贵。
您可以/应该更倾向于使用 Ticks
:
var firstTime = new TimeSpan(1,30,0);
var fourthTime = new TimeSpan(3,0);
var difference = fourthTime.Ticks - firstTime.Ticks;
var step = difference / 3;
var secondTime = new TimeSpan(firstTime.Ticks + step);
var thirdTime = new TimeSpan (secondTime.Ticks + step);
由于多种原因,这更有效:
-
.Ticks
只返回存储的分时,无需任何额外计算 - 获取刻度的构造函数只是简单地存储这些刻度,无需任何额外的计算
- 我们使用 int 除法,这也比双除法快
- 我们不需要创建
TimeSpan
的实例,而是直接使用刻度 (long
) 进行计算
概括起来
/// <summary>
/// Returns an array of all time steps between from and to (including from and to)
/// </summary>
/// <param name="from">the first time</param>
/// <param name="to">the last time</param>
/// <param name="stepCount">How many steps to insert between first and last time</param>
public TimeSpan[] GetInBetweenTimes(TimeSpan from,TimeSpan to,int stepCount)
{
if(stepCount <= 0)
{
return new[]{from,to};
}
var times = new List<TimeSpan>{from};
var difference = to.Ticks - from.Ticks;
var ticksStepDelta = difference / (stepCount+1);
if(ticksStepDelta == 0)
{
Debug.LogError("Difference between from and to is too small for the given stepCount!");
return return new[]{from,to};
}
var currentStep = from.Ticks;
for(i = 0; i < stepCount; i++)
{
currentStep += ticksStepDelta;
times.Add(currentStep);
}
times.Add(to);
return times.ToArray();
}
对于 string
而简单地使用
var timeSpan = new TimeSpan(1,0);
var stringValue = someTimeSpan.ToString("hh\:mm");
01:30
,要以 {HH:MM} 格式显示时间,请使用:
TimeSpan oneAndHalfHour = new TimeSpan(1,0);
Debug.Log(string.Format("You were out of game for: {00} hours and {1:00} minutes",oneAndHalfHour.Hours,oneAndHalfHour.Minutes));
对于动态发现的第 2 次和第 3 次使用这个。
TimeSpan firstTime = new TimeSpan(1,0); // 1.5 hour
TimeSpan secondTime = new TimeSpan(); // Should be 2 hour
TimeSpan thirdTime = new TimeSpan(); // Should be 2.5 hour
TimeSpan fourthTime = new TimeSpan(3,0); // 3 Hour
TimeSpan difference = fourthTime - firstTime;
// There are 3 element between 1st and 4th time,should include 1st one too
// This variable will give us interval between every time
int differenceInMinutes = (int)(difference.TotalMinutes / 3);
secondTime = firstTime.Add(new TimeSpan(0,differenceInMinutes,0));
thirdTime = secondTime.Add(new TimeSpan(0,0));