用时间跨度划分时间

Dividing time with timespan

大家好,我已经用这个 post 上传了一张图片。在图像中有四个时间戳。第一个和最后一个是动态的,因为它们不断变化。我希望中间两个也随着第一个和四个的不同而改变。例如,如果定时器 1 = 2 和定时器 4 = 5,则定时器 2 和定时器 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, 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, 0);
            totalMinutes = HoursMinutes_02 .TotalMinutes;
            result = string.Format ("{0:D2}:{1:D2}", HoursMinutes_02.Hours, HoursMinutes_02.Minutes);
}

TimeDifference

要以 {HH:MM} 格式显示时间,请使用:

TimeSpan oneAndHalfHour = new TimeSpan(1, 30, 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, 30, 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, 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, differenceInMinutes, 0));

那样按分钟进行实际上是相当昂贵的。

你 could/should 宁愿下降到最基本的价值 Ticks:

var firstTime = new TimeSpan(1, 30, 0);
var fourthTime = new TimeSpan(3, 0, 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 只需 returns 存储的报价,无需任何额外计算
  • 获取报价的构造函数只是存储这些报价,无需任何额外计算
  • 我们使用 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,30,0);
var stringValue = someTimeSpan.ToString("hh\:mm");

01:30