HatchKeeper  0.90
The Free Open-Source Egg Incubation Software
HK_Date.cpp
Go to the documentation of this file.
1 /*******************************************************************/
9 #include "../headers/Date.h"
10 
13  Reset();
14 }
15 
20 HK_Date::HK_Date(int Days, int Month, int Year) { SetDate( Days, Month, Year);}
21 
26 HK_Date::HK_Date(std::string Date){SetDate(Date);}
27 
30 
32 bool HK_Date::SetDate(int Days, int Month, int Year){
33  HK_Day = Days;
34  HK_Month = Month;
35  HK_Year = Year;
36  return IsValid();
37 }
38 
40 bool HK_Date::SetDate(std::string Date){
41  std::vector<std::string> Result = SplitStrings(Date, "/");
42 
43  SetDate(std::stoi(Result[2]),std::stoi(Result[1]),std::stoi(Result[0]));
44  return IsValid();
45 }
46 
48 void HK_Date::SetDay(int Days){HK_Day = Days;}
49 
51 void HK_Date::SetMonth(int Month){HK_Month = Month;}
52 
54 void HK_Date::SetYear(int Year){HK_Year = Year;}
55 
57 int HK_Date::GetDay(){return HK_Day;}
58 
61 
63 int HK_Date::GetYear(){return HK_Year;}
64 
70 bool HK_Date::AddDays(int Days){
71  return ModifyDate(Days, true);
72 }
73 
79 bool HK_Date::SubtractDays(int Days){
80  return ModifyDate(Days, false);
81 }
82 
85 {
86  struct tm HKtm;
87  struct tm Datetm;
88  int DOY1, DOY2;
89 
90  //Get Day Of Year
91  HKtm.tm_year = HK_Year - 1900;
92  HKtm.tm_isdst= -1;
93  HKtm.tm_mon = HK_Month - 1;
94  HKtm.tm_mday = HK_Day;
95  HKtm.tm_hour = 0;
96  HKtm.tm_min = 0;
97  HKtm.tm_sec = 0;
98 
99  if (mktime(& HKtm)== -1)
100  return -1;
101 
102  //Get Second Day Of Year
103  Datetm.tm_year = Date.GetYear() - 1900;
104  Datetm.tm_isdst= -1;
105  Datetm.tm_mon = Date.GetMonth() - 1;
106  Datetm.tm_mday = Date.GetDay();
107  Datetm.tm_hour = 0;
108  Datetm.tm_min = 0;
109  Datetm.tm_sec = 0;
110 
111  if (mktime(& Datetm)== -1)
112  return -1;
113 
114  double seconds;
115  if(mktime(&HKtm) > mktime(&Datetm))
116  seconds = mktime(&HKtm) - mktime(&Datetm);
117  else
118  seconds = mktime(&Datetm) - mktime(&HKtm);
119 
120  if(seconds == 0)
121  return 0;
122 
123  return ((seconds / 60) / 60) / 24;
124 }
125 
132 std::string HK_Date::ToReadable(std::string Separator, int Mode) {
133 
134  std::string Day = std::to_string(HK_Day);
135  std::string Month = std::to_string(HK_Month);
136  std::string Year = std::to_string(HK_Year);
137 
138  //Override if day/month lower than 10
139  if(HK_Day < 10) Day = "0" + std::to_string(HK_Day);
140  if(HK_Month < 10) Month = "0" + std::to_string(HK_Month);
141 
142  switch(Mode) {
143  case HK_DateMode_YMD: {
144  return Year + Separator + Month + Separator + Day;
145  }
146  case HK_DateMode_DMY: {
147  return Day + Separator + Month + Separator + Year;
148  }
149  case HK_DateMode_MDY: {
150  return Month + Separator + Day + Separator + Year;
151  }
152  default: {
153  return Month + Separator + Day + Separator + Year;
154  }
155  }
156 }
157 
163 std::string HK_Date::ToSortable(std::string Separator){
164  return ToReadable(Separator, HK_DateMode_YMD);
165 }
166 
168 std::string HK_Date::IntToMonth(int Month){
169  std::string Names[13] = {"Error","January" ,"February","March","April", "May",
170  "June" , "July", "August", "September", "October","November", "December"};
171  return Names[Month];
172 }
173 
175 int HK_Date::MonthToInt(std::string Month){
176  std::string Names[13] = {"Error","January" ,"February","March","April", "May",
177  "June" , "July", "August", "September", "October","November", "December"};
178  for (int C = 0; C < 13; C++){
179  if(Month == Names[C]){
180  return C;
181  }
182  }
183  //Error
184  return -1;
185 }
186 
188 std::string HK_Date::MonthName()
189 {
190  return IntToMonth(HK_Month);
191 }
192 
195 {
197 }
198 
207 bool HK_Date::ModifyDate(int Days, bool Operator){
208  struct tm mytm;
209  int DayOfYear;
210  int NewDOY;
211 
212  //Get Day Of Year
213  mytm.tm_year = HK_Year - 1900;
214  mytm.tm_isdst= -1;
215  mytm.tm_mon = HK_Month - 1;
216  mytm.tm_mday = HK_Day;
217  mytm.tm_hour = 0;
218  mytm.tm_min = 0;
219  mytm.tm_sec = 0;
220 
221  if (mktime(& mytm)== -1)
222  return false;
223 
224  DayOfYear = mytm.tm_yday;
225 
226  //Add Days
227  if(Operator)
228  NewDOY = DayOfYear + Days + 1;
229  else
230  NewDOY = DayOfYear - Days + 1;
231 
232  mytm.tm_year = HK_Year - 1900;
233  mytm.tm_isdst= -1;
234  mytm.tm_mon = 0;
235  mytm.tm_mday = NewDOY;
236  mytm.tm_hour = 0;
237  mytm.tm_min = 0;
238  mytm.tm_sec = 0;
239 
240  if (mktime(& mytm)== -1)
241  return false;
242 
243  //Asign Values
244  HK_Day = mytm.tm_mday;
245  HK_Month = mytm.tm_mon + 1;
246  HK_Year = mytm.tm_year + 1900;
247 
248  //Check For Errors
249  return IsValid();
250 }
251 
254  struct tm HKtm;
255  struct tm Datetm;
256  int DOY1, DOY2;
257 
258  //Get Day Of Year
259  HKtm.tm_year = HK_Year - 1900;
260  HKtm.tm_isdst= -1;
261  HKtm.tm_mon = HK_Month - 1;
262  HKtm.tm_mday = HK_Day;
263  HKtm.tm_hour = 0;
264  HKtm.tm_min = 0;
265  HKtm.tm_sec = 0;
266 
267  if (mktime(& HKtm)== -1)
268  return false;
269 
270  //Get Second Day Of Year
271  Datetm.tm_year = Date.GetYear() - 1900;
272  Datetm.tm_isdst= -1;
273  Datetm.tm_mon = Date.GetMonth() - 1;
274  Datetm.tm_mday = Date.GetDay();
275  Datetm.tm_hour = 0;
276  Datetm.tm_min = 0;
277  Datetm.tm_sec = 0;
278 
279  if (mktime(& Datetm)== -1)
280  return false;
281 
282  if(mktime(&HKtm) < mktime(&Datetm))
283  return true;
284  else
285  return false;
286 }
287 
290  struct tm HKtm;
291  struct tm Datetm;
292  int DOY1, DOY2;
293 
294  //Get Day Of Year
295  HKtm.tm_year = HK_Year - 1900;
296  HKtm.tm_isdst= -1;
297  HKtm.tm_mon = HK_Month - 1;
298  HKtm.tm_mday = HK_Day;
299  HKtm.tm_hour = 0;
300  HKtm.tm_min = 0;
301  HKtm.tm_sec = 0;
302 
303  if (mktime(& HKtm)== -1)
304  return false;
305 
306  //Get Second Day Of Year
307  Datetm.tm_year = Date.GetYear() - 1900;
308  Datetm.tm_isdst= -1;
309  Datetm.tm_mon = Date.GetMonth() - 1;
310  Datetm.tm_mday = Date.GetDay();
311  Datetm.tm_hour = 0;
312  Datetm.tm_min = 0;
313  Datetm.tm_sec = 0;
314 
315  if (mktime(& Datetm)== -1)
316  return false;
317 
318  if(mktime(&HKtm) > mktime(&Datetm))
319  return true;
320  else
321  return false;
322 }
323 
326 {
327  if(HK_Day == Date.GetDay() &&
328  HK_Month == Date.GetMonth() &&
329  HK_Year == Date.GetYear())
330  return true;
331  else
332  return false;
333 }
334 
337 {
338  if(HK_Month == Date.GetMonth() && HK_Year == Date.GetYear())
339  return true;
340  else
341  return false;
342 }
343 
346 {
347  if(HK_Year == Date.GetYear())
348  return true;
349  else
350  return false;
351 }
352 
353 
359 //std::string HK_Date::DateToReadable(int Day, int Month, int Year,std::string Separator)
360 //{
361  //std::string Out = "";
362 
363  //if(Month < 10)
364  //Out += "0";
365  //Out += std::to_string(Month) + Separator;
366 
367  //if(Day < 10) {
368  //Out += "0";
369  //}
370  //return Out + std::to_string(Day) + Separator + std::to_string(Year);
371 //}
372 
377 bool HK_Date::IsValidDate(int Day, int Month, int Year)
378 {
379  int DaysInMonth[] = { -1,31,28,31,30,31,30,31,31,30,31,30,31 };
380 
381  //Test For Leap Year
382  if((0 == ( Year % 4 ) && 0 != ( Year % 100)) || 0 == (Year % 400))
383  DaysInMonth[2] = 29;
384 
385  int MonthLength = DaysInMonth[Month];
386 
387  //Start Checking
388  if(Day < 0 || Day > MonthLength)
389  return false;
390  if(Month < 0 || Month > 12)
391  return false;
392  if(Year < 1970)
393  return false;
394 
395  //else
396  return true;
397 }
398 
401 {
402  time_t t = time(NULL);
403  tm* timePtr = localtime(&t);
404 
405  HK_Day = timePtr->tm_mday;
406  HK_Month = timePtr->tm_mon + 1;
407  HK_Year = timePtr->tm_year + 1900;
408 }
409 
412 {
413  tm TimeIn = {0,0,0,1,HK_Month - 1, HK_Year - 1900};
414 
415  time_t Hold = mktime(&TimeIn);
416 
417  tm * TimeOut = localtime(&Hold);
418 
419  return TimeOut->tm_wday;
420 }
421 
424 {
425  if((0 == (HK_Year % 4 ) && 0 == ( HK_Year % 100)) || (0 == (HK_Year % 400)))
426  return true;
427  else
428  return false;
429 }
430 
433 {
434  int Days[] = { -1,31,28,31,30,31,30,31,31,30,31,30,31 };
435 
436  if(IsLeapYear())
437  Days[2] = 29;
438 
439  return Days[HK_Month];
440 }
441 
442 
444 std::vector<std::string> HK_Date::SplitStrings(std::string In, std::string Split)
445 {
446  std::vector<std::string> OutArray;
447  std::string Hold;
448 
449  for(unsigned int a = 0; a < In.length(); a++) {
450  if(In[a] == Split[0]) {
451  OutArray.push_back(Hold);
452  Hold.clear();
453  continue;
454  }
455  Hold.push_back(In[a]);
456  }
457  OutArray.push_back(Hold);
458  return OutArray;
459 }
const int HK_DateMode_YMD
Definition: Date.h:14
const int HK_DateMode_MDY
Definition: Date.h:13
const int HK_DateMode_DMY
Definition: Date.h:15
A Class For Managing Dates.
Definition: Date.h:25
int GetMonth()
Returns Month Value.
Definition: HK_Date.cpp:60
~HK_Date()
Destructor.
Definition: HK_Date.cpp:29
int HK_Month
Holds The Month Part Of The Date.
Definition: Date.h:29
void SetMonth(int Month)
Sets Month Value, Doesn't Check If Its Valid.
Definition: HK_Date.cpp:51
int Difference(HK_Date Date)
Returns The Number Of Days Difference Between This HK_Date And The Supplied Date.
Definition: HK_Date.cpp:84
bool IsEqualMonth(HK_Date Date)
Checks To See If The Input Date's Month and Year Is Same As Stored Date.
Definition: HK_Date.cpp:336
bool IsLeapYear()
Returns True If HK_Year Is A Leap Year.
Definition: HK_Date.cpp:423
std::string MonthName()
Returns HK_Month's Name As String.
Definition: HK_Date.cpp:188
int HK_Year
Holds The Year Part Of The Date.
Definition: Date.h:30
void Reset()
Resets Date To Today.
Definition: HK_Date.cpp:400
std::string ToSortable(std::string Separator)
Generate A Sortable String For Database.
Definition: HK_Date.cpp:163
bool SubtractDays(int Days)
Subtracts Days To This HK_Date.
Definition: HK_Date.cpp:79
bool IsBefore(HK_Date Date)
Checks To See If The Input Date Is Before The Stored Date.
Definition: HK_Date.cpp:289
void SetDay(int Days)
Sets Day Value, Doesn't Check If Its Valid.
Definition: HK_Date.cpp:48
std::string ToReadable(std::string Separator, int Mode)
Generate A Readable String.
Definition: HK_Date.cpp:132
int StartDayOfWeek()
Returns What Day Of The Week This HK_Date Month Starts.
Definition: HK_Date.cpp:411
bool AddDays(int Days)
Adds Days To This HK_Date.
Definition: HK_Date.cpp:70
bool IsEqualYear(HK_Date Date)
Checks To See If The Input Date's Year Is Same As Stored Date.
Definition: HK_Date.cpp:345
int HK_Day
Holds The Day Part Of The Date.
Definition: Date.h:28
bool IsValid()
Checks If This HK_Date Contains A Valid Date.
Definition: HK_Date.cpp:194
int GetDay()
Returns Day Value.
Definition: HK_Date.cpp:57
std::string IntToMonth(int Month)
Returns The Selected Month Name As A String. Expects 1 - 12.
Definition: HK_Date.cpp:168
HK_Date()
The Default Constructor. Sets Date To Today.
Definition: HK_Date.cpp:12
bool IsValidDate(int Days, int Month, int Year)
Takes Day, Month And Year And Generates A Readable String.
Definition: HK_Date.cpp:377
bool IsEqual(HK_Date Date)
Checks To See If The Input Date Is Same As Stored Date.
Definition: HK_Date.cpp:325
bool ModifyDate(int Days, bool Operator)
Adds Or Subtracts Days From This HK_Date.
Definition: HK_Date.cpp:207
int DaysInMonth()
Returns The Number Of Days In HK_Month. LeapYear Is Checked.
Definition: HK_Date.cpp:432
bool SetDate(int Days, int Month, int Year)
Sets Day, Month, and Year.
Definition: HK_Date.cpp:32
bool IsAfter(HK_Date Date)
Checks To See If The Input Date Is After The Stored Date.
Definition: HK_Date.cpp:253
int GetYear()
Returns Year Value.
Definition: HK_Date.cpp:63
void SetYear(int Year)
Sets Year Value, Doesn't Check If Its Valid.
Definition: HK_Date.cpp:54
std::vector< std::string > SplitStrings(std::string In, std::string Split)
Splits In By Split And Returns A Vector With Data.
Definition: HK_Date.cpp:444
int MonthToInt(std::string Month)
Converts Month Name To An Int.
Definition: HK_Date.cpp:175