Time conversion and difference

So I am writing a program that is basically a time clock. It takes the date and makes that the name of the document that store the info and it takes the time you start and the time you stop and creates the difference, or the total hours. So I am having an issue with the total hours. So all the program really sees is number that have the potential to be endless. It is not understanding the difference between AM and PM time so when the user enters 1 34 as the start and 10 as the end it just calculates the difference between those two numbers in forward since. Also, if the time is 2 24 and end time 1:24 I will get a positive 1:0 as the time difference. I also for some reason got a -9:0 when I did 1 start time and 10 end time. I know these are a few different issues but does anyone know how to fix all of them?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
  #include "pch.h"
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

struct TIME
{
	int minutes;
	int hours;
};

void computeTimeDifference(struct TIME, struct TIME, struct TIME *);

int main()
{
	struct TIME st, et, difference;

	string date;
	string task;

	ofstream tfile;

	cout << "Enter the Date. (MM-DD-YY)" << endl;
	cin >> date;

	cout << "Enter the Start Time. (H MM)" << endl;
	cin >> st.hours >> st.minutes;

	cout << "Enter the End Time. (H MM)" << endl;
	cin >> et.hours >> et.minutes;

	cout << "Task Worked On." << endl;
	cin >> task;

	computeTimeDifference(st, et, &difference);



	tfile.open(date.c_str());
	tfile << date << endl;
	tfile << st.hours << ":" << st.minutes << "-";
	tfile << et.hours << ":" << et.minutes << endl;
	tfile << task << endl;
	tfile << "Total Time:" << difference.hours << ":" << difference.minutes << endl;
	tfile.close();

	return 0;
}

void computeTimeDifference(struct TIME st, struct TIME et, struct TIME *difference)
{
	if (et.minutes > st.minutes)
	{
		--st.hours;
		st.minutes += 60;
	}
	difference->minutes = st.minutes - et.minutes;
	difference->hours = st.hours - et.hours;
}
I would begin by suggesting you switch to std::chrono, follow some tutorial examples, and then your problems probably evaporate.

If you are trying to build your own for the sake of study, perhaps a tour of the original UNIX design of "time.h" ( time() using struct tm ) would be helpful.

UNIX marks time as the number of seconds elapsed since January 1, 1970. There are functions to convert this number into dates and times. The point being that when any input is converted into this "UNIX time", it is merely seconds, and subtraction/addition work rather naturally.

std::chrono expands on the notion to a huge degree, with high precision and resolution way beyond the old UNIX standard.

Put another way, it will not work out well to consider time from the viewpoint of days/hours/minutes for the purpose of time math. It is worse than using yards/feet/inches in combination. This is loosely related to the notion of adding fractions (where the base must match to make sense).

Time should be represented in the highest precision required as a single numeric value, and all other representations (days/dates/etc) should be converted from that.

It is also useful to differentiate between a point in time and a duration of time (as std::chrono does).
Topic archived. No new replies allowed.