计算两个不同时间之间的时差 - 毫秒时间

时间:2016-07-13 14:42:41

标签: c

我编写了以下代码,用于计算两次给定时间之间的时差。然而,这项任务也要求认识到跨越午夜。除了确保将delta报告为正值之外,我无法想到在代码中真正处理该方法的方法。我也在网上搜索,发现其他一些代码似乎没有更好地处理它。请注意;我在问这个算法。我不是在寻找能够做到这一点的功能。这是我的代码:

struct time {
   int hour;
   int minutes;
   int seconds;
};

int main (void)

{
  struct time timeDiff (struct time now, struct time later);
  struct time result, one, two;
  printf("Enter the first time (hh:mm:sec): ");
  scanf("%i:%i:%i", &one.hour, &one.minutes, &one.seconds);
  printf("Enter the second time (hh:mm:sec): ");
  scanf("%i:%i:%i", &two.hour, &two.minutes, &two.seconds);
  result = timeDiff(one, two);
  printf("Time is:  %.2i:%.2i:%.2i\n", result.hour, result.minutes, result.seconds);
  return 0;
}
struct time timeDiff ( struct time now, struct time later)
  {
    struct time timeDiff;
    timeDiff.hour = later.hour - now.hour;
    timeDiff.minutes = later.minutes - now.minutes;
    timeDiff.seconds = later.seconds - now.seconds;
    return timeDiff;
  }

以下是我在网上找到的代码:

#include <stdio.h>
struct time
{
        int hour;
        int minute;
        int second;
};
int main(void)
{
        struct time  time3;
        //struct time get_time(struct time d);
        //struct time elapsed_time(struct time d, struct time e);
        int  convert_to_seconds(struct time d);
        int elapsed_time(int d, int e);
        struct time conver_to_normal_time(int a);
        struct time time1 =  { 3, 45,15};
        struct time time2 = { 9,  44, 03};
        int a, b, c;
        a = convert_to_seconds(time1);
        b = convert_to_seconds(time2);
        c = elapsed_time(a, b);
        time3 = conver_to_normal_time(c);
        printf(" %d:%d:%d", time3.hour, time3.minute, time3.second);
        return 0;
}
struct time get_time(struct time d)
{
        printf("Give me the time\n");
        scanf(" %d:%d:%d", &d.hour, &d.minute, &d.second);
}
int  convert_to_seconds(struct time d)
{
        struct time time1_seconds;
        int totalTime1_seconds;
        time1_seconds.hour = d.hour * 3600;
        time1_seconds.minute = d.second*60;
        time1_seconds.second = d.second;
        totalTime1_seconds = time1_seconds.hour + time1_seconds.minute + time1_seconds.second;
        return totalTime1_seconds;
        totalTime1_seconds = time1_seconds.hour + time1_seconds.minute + time1_seconds.second;
        return totalTime1_seconds;
}
int elapsed_time(int d, int  e)
{
        int result;
        result = d - e;
        return result;
}
struct time conver_to_normal_time(int a)
{
        struct time final_elapse_time;
        final_elapse_time.hour = a / 3600;
        final_elapse_time.minute = (a / 60) % 60;
        final_elapse_time.second = a % 60;
        return final_elapse_time;
}

1 个答案:

答案 0 :(得分:1)

您在网上找到的解决方案确实可以很好地处理问题,它只需要通过将一天中的秒数添加到低于0的差异来处理天数变化。您的问题最终会变大一些,因为在您的解决方案中,当任何新值小于旧值时,您会遇到负面问题。因此,如果您有类似00:00:50 => 00:01:10的内容,则会获得00:01:-40。通过转换为秒,差异更容易计算。

但是,听起来你不想使用在线解决方案,所以获得经过时间的唯一方法是通过并在必要时添加差异:

if (timeDiff.seconds < 0) {
    timeDiff.seconds += 60;
    timeDiff.minutes -= 1;
}

同样地,你必须处理几分钟,然后是几个小时。按顺序执行这些操作以及向上累积也很重要。这源于你正在进行减法这一事实,但是所有的值都是连接在一起的,因此你不需要从几分钟到几秒钟,几小时到几分钟,然后隐含几天到几个小时。