Newer
Older
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "datetime.h"
#include "datastructure.h"
#include "tools.h"
/**********************************************************************************************************************************************************************/
/* This function gets and a parameter from typr integer and checks if this intger(year) is a leap year or not and returns 0 if the given parameter is not a leap year */
/**********************************************************************************************************************************************************************/
int isLeapYear(int year)
{
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
return -1;
else
return 0;
}
/****************************************************************************************************************************************************************************************/
/* This function gets an Integer as a pararmeter and checks if the the given integer(year) is belong to the range [1000 .. 9999] or not and retuens 0 if it doesn't belong to that range*/
/****************************************************************************************************************************************************************************************/
int CheckYear(int year)
{
if (year >= 1000 && year <= 9999)
return -1;
else
return 0;
}
/*******************************************************************************************************************************/
/* This function gets a parameter from type integer and checks if its a value could be month or not, it retuens 0 id it doesn't*/
/*******************************************************************************************************************************/
int CheckMonth(int month)
{
if (month >= 1 && month <= 12)
return -1;
else
return 0;
}
/*********************************************************************************************************************************************************************************************************************************/
/* This function gets 3 parameters from type integer and checks if the given parameters are in the following sequence for a day, month and year and return 0 if the givien parameters couldn't forme valid date*****/
/*******************************************************************************************************************************************************************************************************************************/
int Checkday(int day, int month, int year)
{
if ((day >= 1 && day <= 31) && (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12))
return -1;
else if ((day >= 1 && day <= 30) && (month == 4 || month == 6 || month == 9 || month == 11))
return -1;
else if ((day >= 1 && day <= 28) && (month == 2))
return -1;
else if (day == 29 && month == 2 && (isLeapYear(year)))
return -1;
else
return 0;
}
/**********************************************************************************************************************/
/* This function gets a parameter from type sDate and return 0 if the given parameter is not a valid date *****/
/**********************************************************************************************************************/
int isDateValid(sDate date)
{
if (CheckYear(date.year))
{
if (CheckMonth(date.month))
return Checkday(date.day, date.month, date.year);
else
return 0;
}
else
return 0;
}
/**********************************************************************************************************************/
/* This function gets a parameter from type string and convert it to an integer *****************/
/**********************************************************************************************************************/
int getDay(char *day)
{
return atoi(day);
}
/**************************************************************************************************************************/
/* This functions gets a parameter from type string(a date), extract the month and convert it to an Intger*****************/
/**************************************************************************************************************************/
int getMonth(char *month)
{
unsigned int cursor = 0;
do
{
cursor++;
} while ((*(month + cursor) != ':') && (*(month + cursor) != '\0'));
return atoi((month + cursor + 1));
}
/**************************************************************************************************************************/
/* This functions gets a parameter from type string(a date), extract the year and convert it to an Intger *****************/
/**************************************************************************************************************************/
int getYear(char *Year)
{
unsigned int brake_points = 0;
unsigned int cursor = 0;
do
{
cursor++;
if (*(Year + cursor) == ':')
brake_points++;
} while (brake_points < 2);
return atoi(Year + cursor + 1);
}
/*******************************************************************************************************************************************************************/
/* This function gets a pararemeter from type string and checks if the given string is correct date format (jj.mm.tt) or not if not it returns 0 *******************/
/*******************************************************************************************************************************************************************/
int brake_points_check(char *date)
{
unsigned int brake_points = 0;
unsigned int cursor = 0;
do
{
cursor++;
if (*(date + cursor) == ':')
brake_points++;
} while ((*(date + cursor) != '\0'));
return brake_points > 2 ? 0 : -1;
}
/**********************************************************************************************************************************************************************************/
/*This fuction gets a parameter from type string and another from type sDate and checks and convert the given string into a date from type sDate***********************************/
/**********************************************************************************************************************************************************************************/
{
date->day = getDay(input);
date->month = getMonth(input);
date->year = getYear(input);
if ((isDateValid(*date)) && (brake_points_check(input)))
return -1;
else
return 0;
}
/********************************************************************************************************************************************************************************************************************************************************************************/
/*This fuction gets a parameter from type string and another from type stime and checks and convert the given string into a date from type stime and return 0 if the given srting is not a correct time format (hh:mm:ss) or if it not a correct time input**********************/
/********************************************************************************************************************************************************************************************************************************************************************************/
int getTimeFromString(char Input[20], sTime *Dauer)
{
char sub1[3] = {'\0'};
char sub2[3] = {'\0'};
char sub3[3] = {'\0'};
char c;
if (sscanf(Input, "%2[0-9]:%2[0-9]:%2[0-9]%c", sub1, sub2, sub3, c) > 3)
{
return 0;
}
if (sscanf(Input, "%2[0-9]:%2[0-9]:%2[0-9]%c", sub1, sub2, sub3, c) == 2)
{
H = (unsigned int)atoi(sub1);
M = (unsigned int)atoi(sub2);
S = (unsigned int)atoi(sub3);
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
}
Dauer->hours = H;
Dauer->minutes = M;
Dauer->seconds = S;
if ((Dauer->hours < 24) && (Dauer->seconds < 60) && (Dauer->seconds < 60) && (Dauer->minutes) && (Dauer->seconds))
return 1;
else
return 0;
}
/*******************************************************************************************************************************************/
/* This function gets a parameter from type string and another from type pointer to sTime and copy the string input in the reserved memory**/
/*******************************************************************************************************************************************/
void inputTime(char *prompt, sTime *Dauer)
{
char *Input;
int loopExit = 0;
Input = calloc(20, sizeof(char));
do
{
puts(prompt);
*Input = '\0';
scanf("%[^\n]", Input);
clearBuffer();
if (getTimeFromString(Input, &(*Dauer))) // Dereference then &
{
loopExit = 1;
}
else
printf("Die eingegebene Uhrzeit %s ist ungueltig!\n", Input);
} while (!loopExit);
free(Input);
}
/*******************************************************************************************************************************************/
/* This function gets a parameter from type sTime and another from type intger and prints the continent of the sTime to the console ********/
/*******************************************************************************************************************************************/
void printTime (sTime Dauer, int forcePrintHour)
{
if ((Dauer.hours > 0) || forcePrintHour)
printf("%02i:", Dauer.hours);
printf("%02i:%02i", Dauer.minutes, Dauer.seconds);
}