Determine time between two Dates

I'm working on a warranty database and in need of a help on determining if a particular warranty is out of date.

I've come along the code of determining by the number of seconds,
http://www.unix.com/302086887-post2.html
which I used as a template here.
(Commented out code was trial of another code, then later realized I couldn't determine the time of greater, equal, or less than 5 years.)

I don't expect a great answer, just some outside opinions.

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
#include "Include.h"

void message(string x){
	if(x=="OOW"){
		HANDLE hOut;
		hOut = GetStdHandle(STD_OUTPUT_HANDLE);
		
		SetConsoleTextAttribute(hOut,
			BACKGROUND_RED |
			BACKGROUND_INTENSITY);
		cout << "OUT OF WARRANTY" << endl;
	}if(x=="WAR"){
		HANDLE hOut;
		hOut = GetStdHandle(STD_OUTPUT_HANDLE);
		
		SetConsoleTextAttribute(hOut,
			BACKGROUND_GREEN |
			BACKGROUND_INTENSITY);
		cout << "WARRANTIED" << endl;

	}
}

time_t to_seconds(const char *date)
{
        struct tm storage={0,0,0,0,0,0,0,0,0};
        char *p=NULL;
        time_t retval=0;

        p=(char *)strptime(date,"%d-%b-%Y",&storage);
        if(p==NULL)
        {
                retval=0;
        }
        else
        {
                retval=mktime(&storage);
        }
        return retval;
}


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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169

#include "Include.h"

void clrscr();
void message(string x);
time_t to_seconds(const char *date);
//void compareDates(string date1, string date2);

void view(){
	
	struct regArray {
		string owner;
		string location;
		string serial;
		string charger;
		string day;
		string month;
		string year;
	};
	string monthJAN;
	int size = 1;
	int x = 0;
	int y = 0;
	regArray* registration;
	registration = new regArray[size];
	string search;
	time_t rawtime;
		struct tm * timeinfo;
		char buffer1 [8];
		time ( &rawtime );
		char *date1;
		char *date2;
	string dash="-";

	clrscr();
	cout << "Loading Registry Data Base...";
	ifstream reg;
	reg.open("Registry.txt", ios::app);
	if (!reg.is_open()) { cout << "File Currupted or Lost!" << endl; }
	else {
		for (x=0; !reg.eof(); x++){
				getline(reg, registration[x].owner);	//Owner; Person's name or Business
				getline(reg, registration[x].location);	//Location bought from
				reg >> registration[x].serial;			//Serial
				reg >> registration[x].charger;			//Charger
				getline(reg, registration[x].month, '-');					//Date
				getline(reg, registration[x].day, '-');
				getline(reg, registration[x].year);
				//reg >> Temp;							//File input fix

				regArray* reg2 = new regArray[size];
				for (int i = 0; i < size; ++i){
					reg2[i] = registration[i];}
				delete[] registration;
				++size;
				registration = new regArray[size];
				for (int i = 0; i < size - 1; ++i){
					registration[i] = reg2[i];}
				delete[] reg2;
				y++;
		}
	}
	reg.close();
	cout << " DONE!" << endl;
	clrscr();

	getline(cin, search);
	string temp;
	do{
		temp = "nTrue";
		cout << "1. Old Timer          7. Standard" << endl;
		cout << "2. Super 12volt       8. Goat Herder" << endl;
		cout << "3. 12v Solar          9. 6v Solar" << endl;
		cout << "4. Beck 300          10. Beck 120" << endl;
		cout << "5. Pioneer 3000      11. Pioneer 5000" << endl;
		cout << "6. Pioneer 8000\n" << endl;
		cout << "Enter Search Term :  ";
		getline(cin, search);
		strupr((char *) search.c_str());
		clrscr();
		for(x=0; x<=y; x++){

			string str = registration[x].owner;
			string str2 = search;
			size_t found;

			found=str.find(str2);
			if (found!=string::npos){
				temp = "true";}

			if(temp!="true"){
				string str = registration[x].location;
				found=str.find(str2);
				if (found!=string::npos){
					temp = "true";}
			}

		

			if (temp == "true" || search == registration[x].serial || search == registration[x].charger || search == registration[x].day || search == registration[x].month || search == registration[x].year){
				cout << setw(12) << left << "Name:  " << setw(20) << left << registration[x].owner << setw(15) << left << "Bought From:  " << registration[x].location << endl;
				cout << setw(12) << left << "Serial #:  " << setw(20) << left << registration[x].serial;
					if(registration[x].charger=="1"){
						cout << setw(12) << left << "Old Timer" << endl;
					}if(registration[x].charger=="2"){
						cout << setw(12) << left <<"Super 12volt" << endl;
					}if(registration[x].charger=="3"){
						cout << setw(12) << left <<"12v Solar" << endl;
					}if(registration[x].charger=="4"){
						cout << setw(12) << left <<"Beck 300" << endl;
					}if(registration[x].charger=="5"){
						cout << setw(12) << left <<"Pioneer 3000" << endl;
					}if(registration[x].charger=="6"){
						cout << setw(12) << left <<"Pioneer 8000" << endl;
					}if(registration[x].charger=="7"){
						cout << setw(12) << left <<"Standard" << endl;
					}if(registration[x].charger=="8"){
						cout << setw(12) << left <<"Goat Herder" << endl;
					}if(registration[x].charger=="9"){
						cout << setw(12) << left <<"6v Solar" << endl;
					}if(registration[x].charger=="10"){
						cout << setw(12) << left <<"Beck 120" << endl;
					}if(registration[x].charger=="11"){
						cout << setw(12) << left <<"Pioneer 5000" << endl;
					}
					cout << setw(10) << left << "Registered:" << registration[x].month << "-" << registration[x].day << "-" << registration[x].year;
					if (registration[x].month == "1" || registration[x].month == "01"){
						monthJAN = "JAN";}if (registration[x].month == "2" || registration[x].month == "02"){
						monthJAN = "FEB";}if (registration[x].month == "3" || registration[x].month == "03"){
						monthJAN = "MAR";}if (registration[x].month == "4" || registration[x].month == "04"){
						monthJAN = "APR";}if (registration[x].month == "5" || registration[x].month == "05"){
						monthJAN = "MAY";}if (registration[x].month == "6" || registration[x].month == "06"){
						monthJAN = "JUN";}if (registration[x].month == "7" || registration[x].month == "07"){
						monthJAN = "JUL";}if (registration[x].month == "8" || registration[x].month == "08"){
						monthJAN = "AUG";}if (registration[x].month == "9" || registration[x].month == "09"){
						monthJAN = "SEP";}if (registration[x].month == "10"){
						monthJAN = "OCT";}if (registration[x].month == "11"){
						monthJAN = "NOV";}if (registration[x].month == "12"){
						monthJAN = "DEC";}
					temp = registration[x].day + dash + monthJAN + dash + registration[x].year;
					string::traits_type::copy( date1, temp.c_str(), temp.length() +1 );
					//temp = registration[x].day + dash + registration[x].month + dash + registration[x].year;
					//d1 = temp;
					timeinfo = localtime ( &rawtime );
					strftime (buffer1,8,"%d-%m-%y",timeinfo);
					date2 = buffer1;

					//compareDates(d1, d2);


					time_t d1=to_seconds(date1);
					time_t d2=to_seconds(date2);

					
					if((157784630= d1 - d2) || d2==d1){
						string m="WAR";
						message(m);}
					if(d2<d1){
						string m="OOW";
						message(m);}
					cout << "\n\n";
			temp = "nTrue";
			}
		}
		system("pause");
		clrscr();
	}while(search!="I'M DONE");
}


include.h
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <iomanip>
#include <string>
//#include <cctype>
#include <ctime>
#include <sstream>
#include <fstream>
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <windows.h>
using namespace std;
int main ();


errors:
1
2
3
4
5
6
7
1>c:\users\striker\desktop\serial sorter\serial sorter\view.cpp(154) : warning C4244: '=' : conversion from 'time_t' to 'int', possible loss of data
1>c:\users\striker\desktop\serial sorter\serial sorter\view.cpp(154) : error C2106: '=' : left operand must be l-value
1>misc.cpp
1>c:\users\striker\desktop\serial sorter\serial sorter\misc.cpp(58) : error C3861: 'strptime': identifier not found
1>Generating Code...
1>Build log was saved at "file://c:\Users\Striker\Desktop\Serial Sorter\Serial Sorter\Debug\BuildLog.htm"
1>Serial Sorter - 2 error(s), 5 warning(s)


I have an int main that is basically a menu that calls in the void view(); from a menu. I presume just changing "void view" to "int main" would allow the program to run just the same.
I don't believe that strptime is supported on Windows (apparently only on Unix). Perhaps this external thread can help you:

http://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/15f07391-4c2f-4479-8cb5-2688d2cd7639

The other errors are probably caused by the fact that you've got an assignment operator where there should be a comparison operator.

I.e. you have this:

1
2
if((157784630= d1 - d2) || d2==d1)
{ ...}


you should have something like this:

 
if(157784630 >= d1 - d2)


which I think is what you're trying to do, right (making sure the difference in time is less than the warranty period)?

Hope that helps.
In a project in class I made a small piece of code that creates the file name depending on the date and time.

1
2
3
4
		timeinfo = localtime ( &rawtime );
		strftime (buffer,80,"%y-%m-%d %H+%M+%S.txt",timeinfo);
		dateTime = buffer;
		cout << "\n\n>>Saved to:  " << dateTime << endl;


1
2
3
FILE * pFile;
	  pFile = fopen (dateTime.c_str(),"w");
		if (pFile!=NULL)


never got an error with it.
only difference is that the last two pieces of code were made in c++ 2005, the code in the first post was made in c++ 2008

thanks for the catch on the >=, I haven't got the program to work with this piece of code, so that is one of the main reasons I didn't notice it. But I did this morning when I took my computer out of hibernate and the code was looking at me.

edit:
I replaced the strftime with sscanf_s in both locations and the error went away. I got 1 succeed so I ran the program, now I got an issue with the line of code:

1
2
temp = registration[x].day + dash + monthJAN + dash + registration[x].year;
string::traits_type::copy( date1, temp.c_str(), temp.length() +1 );  //Error here, read below. 


Error when running the program:
Unhandled exception at 0x60abee64 in Serial Sorter.exe: 0xC0000005: Access violation writing location 0x0041d8b8.

I don't think this would help you guys any stating the error.
Each part in the temp line area all string variables, then in the second line, I was trying to convert the string temp to date1 which is "char *date1"

edit2:
I now have that line of code working by replacing:
 
string::traits_type::copy( date1, temp.c_str(), temp.length() +1 );

with
1
2
date1 = (char*)malloc( sizeof( char ) *(temp.length() +1) );
strcpy( date1, temp.c_str() );


The program runs up until it hits the line in time_t to_seconds:
 
p=(char *)sscanf_s(date,"%d-%b-%Y",&storage);

I get the Debug Assertion Failed message.
Last edited on
After messing with the code, I made my own time difference function to convert the dates to days.
Topic archived. No new replies allowed.