KSquare Utilities
DateTime.cpp
Go to the documentation of this file.
1 /* DateTime.cpp -- Classes to support Date and Time functionality.
2  * Copyright (C) 1994-2011 Kurt Kramer
3  * For conditions of distribution and use, see copyright notice in KKB.h
4  */
5 
6 /* Originally developed in Borland Pascal 1986; converted to c++ in 1995 */
7 
8 #include "FirstIncludes.h"
9 #include <iostream>
10 #include <vector>
11 #include "MemoryDebug.h"
12 using namespace std;
13 
14 
15 #include "KKBaseTypes.h"
16 #include "KKStrParser.h"
17 using namespace KKB;
18 
19 #include "DateTime.h"
20 
21 
22 const char* DateType::monthlyNames[] =
23 {
24  "",
25  "January", // 1
26  "February", // 2
27  "March", // 3
28  "April", // 4
29  "May", // 5
30  "June", // 6
31  "July", // 7
32  "August", // 8
33  "September", // 9
34  "October", // 10
35  "November", // 11
36  "December" // 12
37 };
38 
39 
40 const char* DateType::monthlyShortNames[] =
41 {
42  "",
43  "Jan", // 1
44  "Feb", // 2
45  "Mar", // 3
46  "Apr", // 4
47  "May", // 5
48  "Jun", // 6
49  "Jul", // 7
50  "Aug", // 8
51  "Sep", // 9
52  "Oct", // 10
53  "Nov", // 11
54  "Dec" // 12
55 };
56 
57 
58 const uchar DateType::daysInEachMonth[] =
59 {
60  0,
61  31, // Jan
62  28, // Feb
63  31, // Mar
64  30, // Apr
65  31, // MAy
66  30, // Jun
67  31, // Jul
68  31, // Aug
69  30, // Sep
70  31, // Oct
71  30, // Nov
72  31 // Dec
73 };
74 
75 
76 const short DateType::daysYTDforMonth[] =
77 {
78  0,
79  31, // Jan
80  59, // Feb
81  90, // Mar
82  120, // Apr
83  151, // MAy
84  181, // Jun
85  212, // Jul
86  243, // Aug
87  273, // Sep
88  304, // Oct
89  334, // Nov
90  365 // Dec
91 };
92 
93 
94 
95 uchar DateType::MonthFromStr (const KKStr& monthStr)
96 {
97  uchar month = 0;
98 
99  for (month = 1; month < 13; month++)
100  {
101  if (monthStr.CompareIgnoreCase (monthlyShortNames[month]) == 0)
102  break;
103 
104  if (monthStr.CompareIgnoreCase (monthlyNames[month]) == 0)
105  break;
106  }
107 
108  return month;
109 } /* MonthFromStr */
110 
111 
112 
113 
115  uchar month
116  )
117 {
118  if ((month < 1) || (month > 12))
119  return 0;
120 
121  if (month == 2)
122  {
123  if ((year % 4) == 0)
124  return 29;
125  }
126 
127  return daysInEachMonth[month];
128 }
129 
130 
131 
133  uchar month
134  )
135 {
136  if ((month < 1) || (month > 12))
137  return 0;
138 
139  if (month < 3)
140  return daysYTDforMonth[month - 1];
141 
142  if ((year % 4) == 0)
143  {
144  return daysYTDforMonth[month - 1] + 1;
145  }
146  else
147  {
148  return daysYTDforMonth[month - 1];
149  }
150 } /* DaysYTDforMonth */
151 
152 
153 
154 
155 
156 
158  year (0),
159  month (0),
160  day (0)
161 {
162 }
163 
164 
165 
166 DateType::DateType (const DateType& _date):
167  year (_date.year),
168  month (_date.month),
169  day (_date.day)
170 {
171 }
172 
173 
174 
175 DateType::DateType (short _year,
176  uchar _month,
177  uchar _day
178  ):
179  year (_year),
180  month (_month),
181  day (_day)
182 
183 {
184 }
185 
186 
188  year (0),
189  month (0),
190  day (0)
191 {
192  KKStr field1 = s.ExtractToken ("\n\r\t/- ");
193  KKStr field2 = s.ExtractToken ("\n\r\t/- ");
194  KKStr field3 = s.ExtractToken ("\n\r\t/- ");
195 
196  uchar m;
197  uchar d;
198  short y;
199 
200  m = MonthFromStr (field1);
201  if ((m > 0) && (m < 13))
202  {
203  // The first field is a month, assume mmm/dd/yyyy
204  d = (uchar)atoi (field2.Str ());
205  y = (short)atoi (field3.Str ());
206 
207  if ((d < 1) || (d > DaysInTheMonth (y, m)))
208  d = 1;
209 
210  year = y;
211  month = m;
212  day = d;
213 
214  return;
215  }
216 
217  m = MonthFromStr (field2);
218  if ((m > 0) && (m < 13))
219  {
220  // Second field is a month field, assuming yyyy/mmm/dd
221  kkint32 f3 = atoi (field3.Str ());
222  kkint32 f1 = atoi (field1.Str ());
223 
224  if (f3 > 1900)
225  {
226  y = (short)f3;
227  d = (uchar)f1;
228  }
229  else
230  {
231  y = (short)f1;
232  d = (uchar)f3;
233  }
234 
235  if (d < 1)
236  d = 1;
237  else if (d > DaysInTheMonth (y, m))
238  d = DaysInTheMonth (y, m);
239 
240  year = y;
241  month = m;
242  day = d;
243 
244  AdjustYear ();
245 
246  return;
247  }
248 
249  kkint32 f1 = atoi (field1.Str ());
250  kkint32 f2 = atoi (field2.Str ());
251  kkint32 f3 = atoi (field3.Str ());
252 
253  if (((f1 >= 1) && (f1 <= 12)) &&
254  ((f2 >= 1) && (f2 <= 31)) &&
255  ((f3 >= 0) && (f3 <= 9999))
256  )
257  {
258  // mm/dd/yyyy
259  year = short (f3);
260  month = uchar (f1);
261  day = uchar (f2);
262 
263  AdjustYear ();
264  return;
265  }
266 
267  if (((f1 >= 0) && (f1 <= 9999)) &&
268  ((f2 >= 1) && (f2 <= 12)) &&
269  ((f3 >= 1) && (f3 <= 31))
270  )
271  {
272  // yyyy/mm/dd
273  year = short (f1);
274  month = uchar (f2);
275  day = uchar (f3);
276  AdjustYear ();
277  return;
278  }
279 
280  if (((f1 >= 0) && (f1 <= 31)) &&
281  ((f2 >= 1) && (f2 <= 12)) &&
282  ((f3 >= 1) && (f3 <= 9999))
283  )
284  {
285  // dd/mm/yyyy
286  year = short (f3);
287  month = uchar (f2);
288  day = uchar (f1);
289  AdjustYear ();
290  return;
291  }
292 
293  year = 0;
294  month = 1;
295  day = 1;
296 }
297 
298 
299 
300 
302 {
303  if ((year % 4) == 0)
304  return 366;
305  else
306  return 365;
307 }
308 
309 
310 
312 {
313  SetFromNumOfDaysInTime (days);
314 }
315 
316 
318 {
319  return DaysInTheMonth (year, month);
320 }
321 
322 
323 void DateType::SetFromNumOfDaysInTime (kkint32 days)
324 {
325  kkint32 numOf4Years = days / 1461;
326 
327  if (days < 0)
328  days--;
329 
330  year = (short)(numOf4Years * 4);
331  days = days % 1461;
332 
333  while (days < 0)
334  {
335  days += DaysInYear (year);
336  year--;
337  }
338 
339  if (days == 0)
340  {
341  year--;
342  days = DaysInYear (year);
343  }
344 
345  kkint32 daysInYear = DaysInYear (year);
346  while (days > daysInYear)
347  {
348  year++;
349  days = days - daysInYear;
350  daysInYear = DaysInYear (year);
351  }
352 
353  month = 1;
354  kkint32 daysInMonth = DaysInTheMonth (year, month);
355  while (days > daysInMonth)
356  {
357  month++;
358  days = days - daysInMonth;
359  daysInMonth = DaysInTheMonth (year, month);
360  }
361 
362  day = (uchar)days;
363 } /* SetFromNumOfDaysInTime */
364 
365 
366 
367 void DateType::AdjustYear ()
368 {
369  if (year < 100)
370  {
371  if (year > 50)
372  year += 1900;
373  else
374  year += 2000;
375  }
376 } /* AdjustYear */
377 
378 
379 
380 
381 void DateType::AddDays (kkint32 _days)
382 {
383  if (_days < 0)
384  {
385  SubtractDays (0 - _days);
386  return;
387  }
388 
389  if (_days > 365)
390  {
391  kkint32 numDays = Days () + _days;
392  SetFromNumOfDaysInTime (numDays);
393  return;
394  }
395 
396  kkint32 newDay = day + _days;
397  while (newDay > DaysThisMonth ())
398  {
399  newDay = newDay - DaysThisMonth ();
400  month++;
401  if (month > 12)
402  {
403  month = 1;
404  year++;
405  }
406  }
407 
408  day = (uchar)newDay;
409 } /* AddDays */
410 
411 
412 
413 
415 {
416  if (_days < 0)
417  {
418  AddDays (0 - _days);
419  return;
420  }
421 
422  if (_days > 365)
423  {
424  kkint32 numDays = Days () - _days;
425  SetFromNumOfDaysInTime (numDays);
426  return;
427  }
428 
429  kkint32 newDay = day - _days;
430  while (newDay < 1)
431  {
432  month--;
433  while (month < 1)
434  {
435  month += 12;
436  year--;
437  }
438  newDay = newDay + DaysThisMonth ();
439  }
440 
441  day = (uchar)newDay;
442 } /* SubtractDays */
443 
444 
445 
446 void DateType::AddMonths (kkint32 _months)
447 {
448  kkint32 newMonth = month + _months;
449  year = year + (short)(newMonth / 12);
450  newMonth = newMonth % 12;
451  if (newMonth == 0)
452  {
453  year--;
454  newMonth += 12;
455  }
456 } /* AddMonths */
457 
458 
459 
460 
461 
463 {
464  year = right.year;
465  month = right.month;
466  day = right.day;
467 
468  return *this;
469 }
470 
471 
472 
473 
474 
476 {
477  if ((month < 1) || (month > 12))
478  return 0;
479 
480 
481  kkint32 totDays = 0;
482 
483  if (year < 0)
484  {
485  kkint32 x = 0 - year;
486  totDays = totDays - ((x * 365) + (x / 4));
487  }
488 
489  else if (year > 0)
490  {
491  totDays += (year * 365) + ((year - 1) / 4) + 1; // The "+ 1" is for year 0 which is a leap year
492  }
493 
494  totDays += day + DaysYTDforMonth (year, month);
495 
496  return totDays;
497 } /* Days */
498 
499 
500 
501 
503 {
504  kkuint64 numDays = (kkuint64)Days ();
505  kkuint64 numSecs = numDays * (kkuint64)86400;
506  return numSecs;
507 }
508 
509 
510 kkuint32 DateType::ToDays () const {return Days ();}
511 kkuint32 DateType::ToHours () const {return Days () * 24;}
512 kkuint64 DateType::ToSeconds () const {return (kkuint64)(Days ()) * (kkuint64)86400;}
513 
514 
515 
516 
517 kkint32 DateType::Compare (const DateType& right) const
518 {
519  if (year > right.year)
520  return 1;
521 
522  else if (year < right.year)
523  return -1;
524 
525  else if (month > right.month)
526  return 1;
527 
528  else if (month < right.month)
529  return -1;
530 
531  else if (day > right.day)
532  return 1;
533 
534  else if (day < right.day)
535  return -1;
536 
537  else
538  return 0;
539 } /* Compare */
540 
541 
542 bool DateType::operator== (const DateType& right) const
543 {
544  return (Compare (right) == 0);
545 }
546 
547 
548 bool DateType::operator!= (const DateType& right) const
549 {
550  return (Compare (right) != 0);
551 }
552 
553 
554 
555 bool DateType::operator> (const DateType& right) const
556 {
557  return (Compare (right) > 0);
558 }
559 
560 
561 
562 bool DateType::operator>= (const DateType& right) const
563 {
564  return (Compare (right) >= 0);
565 }
566 
567 
568 
569 bool DateType::operator< (const DateType& right) const
570 {
571  return (Compare (right) < 0);
572 }
573 
574 
575 
576 
577 bool DateType::operator<= (const DateType& right) const
578 {
579  return (Compare (right) <= 0);
580 }
581 
582 
583 
584 void DateType::AdjustDateFields (kkint32& years,
585  kkint32& months,
586  kkint32& days
587  )
588 {
589  years += (months / 12);
590  months = (months % 12);
591 
592  kkint32 daysInTheMonth = DaysInTheMonth (years, (uchar)months);
593 
594  while ((days < 1) && (days > daysInTheMonth))
595  {
596  if (days < 1)
597  {
598  if (months == 1)
599  months = 12;
600  else
601  months--;
602 
603  daysInTheMonth = DaysInTheMonth (years, (uchar)months);
604  days = days + daysInTheMonth;
605  }
606  else
607  {
608  if (months >= 12)
609  months = 1;
610  else
611  months++;
612  days = days - daysInTheMonth;
613 
614  daysInTheMonth = DaysInTheMonth (years, (uchar)months);
615  }
616  }
617 } /* AdjustDateFields */
618 
619 
620 
621 DateType DateType::operator+ (const DateType& right) const
622 {
623  return DateType (Days () + right.Days ());
624 }
625 
626 
627 
629 {
630  return DateType (Days () + right);
631 }
632 
633 
635 {
636  return DateType (Days () - right);
637 }
638 
639 
640 
641 DateType DateType::operator- (const DateType& right) const
642 {
643  return DateType (Days () - right.Days ());
644 }
645 
646 
648 {
649  day++;
650  if (day > DaysInTheMonth (year, month))
651  {
652  day = 1;
653  month++;
654  if (month > 12)
655  {
656  month = 1;
657  year++;
658  }
659  }
660 
661  return *this;
662 }
663 
664 
666 {
667  KKStr s (9);
668  s << StrFormatInt (Month (), "00") << "/"
669  << StrFormatInt (Day (), "00") << "/"
670  << StrFormatInt (Year () % 100, "00");
671  return s;
672 } /* MM_DD_YY */
673 
674 
675 
677 {
678  KKStr s (12);
679 
680  if ((month < 1) || (month > 12))
681  s << "***";
682  else
683  s << monthlyShortNames[month];
684 
685  s << StrFormatInt (Day (), "00") << "/"
686  << StrFormatInt (Year (), "0000");
687  return s;
688 } /* MMM_DD_YYYY */
689 
690 
692 {
693  KKStr s (9);
694  s << StrFormatInt (year % 100, "00") << "/"
695  << StrFormatInt (month, "00") << "/"
696  << StrFormatInt (day, "00");
697  return s;
698 } /* YY_MM_DD */
699 
700 
702 {
703  KKStr s (11);
704  s << StrFormatInt (year, "0000") << "/"
705  << StrFormatInt (month, "00") << "/"
706  << StrFormatInt (day, "00");
707  return s;
708 } /* YYYY_MM_DD */
709 
710 
711 
713 {
714  const char* monthStr = "***";
715  if ((month >= 1) && (month <= 12))
716  monthStr = monthlyShortNames[month];
717 
718  KKStr s (12);
719  s << StrFormatInt (year, "0000") << "/"
720  << monthStr << "/"
721  << StrFormatInt (day, "00");
722  return s;
723 } /* YYYY_MM_DD */
724 
725 
726 
728 {
729  KKStr s (8);
730  s << StrFormatInt (year, "0000")
731  << StrFormatInt (month, "00")
732  << StrFormatInt (day, "00");
733  return s;
734 }
735 
736 
737 KKStr& KKB::operator<< ( KKStr& left,
738  const DateType& right
739  )
740 {
741  left << right.YYYY_MMM_DD ();
742  return left;
743 }
744 
745 
746 
747 std::ostream& KKB::operator<< ( std::ostream& os,
748  const DateType& right
749  )
750 {
751  os << right.YYYY_MMM_DD ();
752  return os;
753 }
754 
755 
756 
758  hour (0),
759  minute (0),
760  second (0)
761 {}
762 
763 
764 
765 
766 TimeType::TimeType (const TimeType& _time):
767  hour (_time.hour),
768  minute (_time.minute),
769  second (_time.second)
770 {}
771 
772 
773 
775  uchar _minute,
776  uchar _second
777  ):
778  hour (_hour),
779  minute (_minute),
780  second (_second)
781 {}
782 
783 
784 
786 {
787  kkint32 hours = seconds / 3600;
788  seconds = seconds % 3600;
789 
790  kkint32 minutes = seconds / 60;
791  seconds = seconds % 60;
792 
793  hour = (uchar)hours;
794  minute = (uchar)minutes;
795  second = (uchar)seconds;
796 }
797 
798 
799 
800 
801 
803  hour (0),
804  minute (0),
805  second (0)
806 
807 {
808  KKStr field1, field2, field3;
809 
810  if (s.Len () == 6)
811  {
812  field1 = s.SubStrPart (0, 1);
813  field2 = s.SubStrPart (2, 3);
814  field3 = s.SubStrPart (4, 5);
815  }
816  else
817  {
818  field1 = s.ExtractToken ("\n\r\t: ");
819  field2 = s.ExtractToken ("\n\r\t: ");
820  field3 = s.ExtractToken ("\n\r\t: ");
821  }
822 
823  kkint32 f1 = atoi (field1.Str ());
824  kkint32 f2 = atoi (field2.Str ());
825  kkint32 f3 = atoi (field3.Str ());
826 
827 
828  if (((f1 >= 0) && (f1 <= 24)) &&
829  ((f2 >= 0) && (f2 <= 59)) &&
830  ((f3 >= 0) && (f3 <= 59))
831  )
832  {
833  hour = (uchar)f1;
834  minute = (uchar)f2;
835  second = (uchar)f3;
836  }
837 }
838 
839 
840 
842  {
843  return StrFormatInt (hour, "00") + ":" + StrFormatInt (minute, "00") + ":" + StrFormatInt (second, "00");
844  }
845 
846 
848 {
849  return StrFormatInt (hour, "00") + StrFormatInt (minute, "00") + StrFormatInt (second, "00");
850 }
851 
852 
853 kkint32 TimeType::Compare (const TimeType& right) const
854 {
855  if (hour > right.hour)
856  return 1;
857 
858  else if (hour < right.hour)
859  return -1;
860 
861  else if (minute > right.minute)
862  return 1;
863 
864  else if (minute < right.minute)
865  return -1;
866 
867  else if (second > right.second)
868  return 1;
869 
870  else if (second < right.second)
871  return -1;
872 
873  else
874  return 0;
875 }
876 
877 
878 
880 {
881  hour = right.hour;
882  minute = right.minute;
883  second = right.second;
884  return *this;
885 }
886 
887 
888 
889 
890 bool TimeType::operator== (const TimeType& right) const
891 {
892  return (Compare (right) == 0);
893 }
894 
895 
896 
897 bool TimeType::operator!= (const TimeType& right) const
898 {
899  return (Compare (right) == 0);
900 }
901 
902 
903 
904 
905 bool TimeType::operator> (const TimeType& right) const
906 {
907  return (Compare (right) > 0);
908 }
909 
910 
911 
912 bool TimeType::operator>= (const TimeType& right) const
913 {
914  return (Compare (right) >= 0);
915 }
916 
917 
918 
919 bool TimeType::operator< (const TimeType& right) const
920 {
921  return (Compare (right) < 0);
922 }
923 
924 
926 {
927  return hour * 3600 + minute * 60 + second;
928 }
929 
930 
931 double TimeType::ToHours () const
932 {
933  return (double)hour + (double)(minute * 60 + second) / 3600.0;
934 }
935 
936 
937 double TimeType::ToMinutes () const
938 {
939  return (double)hour + (double)(minute * 60 + second) / 60.0;
940 }
941 
942 
943 
944 
945 
946 TimeType TimeType::operator+ (const TimeType& right) const
947 {
948  kkint32 totSeconds = Seconds () + right.Seconds ();
949  return TimeType (totSeconds);
950 }
951 
952 
953 TimeType TimeType::operator- (const TimeType& right) const
954 {
955  kkint32 totSeconds = Seconds () - right.Seconds ();
956  return TimeType (totSeconds);
957 }
958 
959 
960 
961 bool TimeType::operator<= (const TimeType& right) const
962 {
963  return (Compare (right) <= 0);
964 }
965 
966 
967 
968 
969 KKStr& KKB::operator<< ( KKStr& left,
970  const TimeType& right
971  )
972 {
973  left << StrFormatInt (right.Hour (), "00") << ":"
974  << StrFormatInt (right.Minute (), "00") << ":"
975  << StrFormatInt (right.Second (), "00");
976 
977  return left;
978 }
979 
980 
981 
982 std::ostream& KKB::operator<< ( std::ostream& os,
983  const TimeType& right
984  )
985 {
986  os << StrFormatInt (right.Hour (), "00") << ":"
987  << StrFormatInt (right.Minute (), "00") << ":"
988  << StrFormatInt (right.Second (), "00");
989 
990  return os;
991 }
992 
993 
994 
995 
996 
998  date(),
999  time ()
1000 {}
1001 
1002 
1003 
1004 
1005 
1006 DateTime::DateTime (const DateTime& dateTime):
1007  date (dateTime.date),
1008  time (dateTime.time)
1009 {}
1010 
1011 
1012 
1013 
1014 
1015 
1016 DateTime::DateTime (const DateType& _date,
1017  const TimeType& _time
1018  ):
1019  date (_date),
1020  time (_time)
1021 {}
1022 
1023 
1024 
1025 
1026 
1027 
1028 DateTime::DateTime (short _year,
1029  uchar _month,
1030  uchar _day,
1031  uchar _hour,
1032  uchar _minute,
1033  uchar _second
1034  ):
1035  date (_year, _month, _day),
1036  time (_hour, _minute, _second)
1037 {}
1038 
1039 
1040 
1041 
1042 
1044  date(),
1045  time ()
1046 
1047 {
1048  if (s.Empty ())
1049  return;
1050 
1051  KKStrParser parser (s);
1052 
1053  if ((s.CountInstancesOf ('-') > 2) && (s.CountInstancesOf ('/') < 1))
1054  {
1055  // Looks like the date part is broken up by '-' not '/'
1056  KKStr datePart1 = parser.GetNextToken ("- ");
1057  KKStr datePart2 = parser.GetNextToken ("- ");
1058  KKStr datePart3 = parser.GetNextToken ("- ");
1059  KKStr timeStr = parser.GetRestOfStr ();
1060 
1061  KKStr dateStr = datePart1 + "/" + datePart2 + "/" + datePart3;
1062  date = DateType (dateStr);
1063  time = TimeType (timeStr);
1064  }
1065  else
1066  {
1067  KKStr field1 = parser.GetNextToken ("- ");
1068  KKStr field2 = parser.GetNextToken ("- ");
1069  date = DateType (field1);
1070  time = TimeType (field2);
1071  }
1072 }
1073 
1074 
1075 
1076 
1077 
1078 
1079 kkint32 DateTime::Compare (const DateTime& right) const
1080 {
1081  if (date < right.date)
1082  return -1;
1083 
1084  else if (date > right.date)
1085  return 1;
1086 
1087  return time.Compare (right.time);
1088 }
1089 
1090 
1091 
1093 {
1094  kkuint64 secsInDate = (kkuint64)date.Seconds ();
1095  kkuint64 secsInTime = (kkuint64)time.Seconds ();
1096  return secsInDate + secsInTime;
1097 }
1098 
1099 
1100 kkuint32 DateTime::ToDays () const {return date.Days ();}
1101 double DateTime::ToHours () const {return date.Days () * 24 + time.ToHours ();}
1102 
1103 
1104 ///<summary>Number seconds since "0000/01/01 00:00:00"</summary>
1105 kkuint64 DateTime::ToSeconds () const {return (kkuint64)(date.Days ()) * (kkuint64)86400 + time.Seconds ();};
1106 
1107 
1108 void DateTime::AddDays (kkint32 _days)
1109 {
1110  date.AddDays (_days);
1111 }
1112 
1113 
1114 void DateTime::AddHours (kkint32 _hours)
1115 {
1116  if (_hours < 0)
1117  {
1118  kkint32 newHour = time.Hour () + _hours;
1119  while (newHour < 0)
1120  {
1121  date = date - 1;
1122  newHour += 24;
1123  }
1124  time.Hour ((uchar)newHour);
1125  }
1126  else
1127  {
1128  kkint32 newHour = time.Hour () + _hours;
1129  while (newHour > 23)
1130  {
1131  date = date + 1;
1132  newHour -= 24;
1133  }
1134  time.Hour ((uchar)newHour);
1135  }
1136 } /* AddHours */
1137 
1138 
1140 {
1141  kkint32 newMins = time.Minute () + _mins;
1142 
1143  while (newMins < 0)
1144  {
1145  HoursAdd (-1);
1146  newMins += 60;
1147  }
1148 
1149  while (newMins >= 60)
1150  {
1151  HoursAdd (1);
1152  newMins -= 60;
1153  }
1154 
1155  time.Minute ((uchar)newMins);
1156 } /* AddMinutes */
1157 
1158 
1159 void DateTime::AddSeconds (long _secs)
1160 {
1161  long newSecs = time.Second () + _secs;
1162 
1163  long minsToAdd = newSecs / 60;
1164  newSecs = newSecs - (minsToAdd * 60);
1165  if (minsToAdd != 0)
1166  MinutesAdd (minsToAdd);
1167 
1168  while (newSecs < 0)
1169  {
1170  MinutesAdd (-1);
1171  newSecs += 60;
1172  }
1173 
1174  while (newSecs >= 60)
1175  {
1176  MinutesAdd (1);
1177  newSecs -= 60;
1178  }
1179 
1180  time.Second ((uchar)newSecs);
1181 } /* AddSeconds */
1182 
1183 
1184 
1185 
1186 void DateTime::HoursAdd (kkint32 _hours)
1187 {
1188  AddHours (_hours);
1189 } /* HoursAdd */
1190 
1191 
1192 
1193 
1195 {
1196  AddMinutes (_mins);
1197 } /* MinutesAdd */
1198 
1199 
1200 
1201 
1202 void DateTime::SecondsAdd (long _secs)
1203 {
1204  AddSeconds (_secs);
1205 } /* SecondsAdd */
1206 
1207 
1208 
1210 {
1211  return date.YYYYMMDD () + time.HHMMSS ();
1212 }
1213 
1214 
1215 
1217 {
1218  return date.YYYY_MM_DD () + "-" + time.HH_MM_SS ();
1219 }
1220 
1221 
1223 {
1224  return StrFormatInt ((date.ToHours () + time.Hour ()), "###00") + ":" +
1225  StrFormatInt (time.Minute (), "00") + ":" +
1226  StrFormatInt (time.Second (), "00");
1227 }
1228 
1229 
1230 
1232 {
1233  date = right.date;
1234  time = right.time;
1235  return *this;
1236 }
1237 
1238 
1239 
1240 
1241 bool DateTime::operator== (const DateTime& right) const
1242 {
1243  return Compare (right) == 0;
1244 }
1245 
1246 
1247 
1248 bool DateTime::operator!= (const DateTime& right) const
1249 {
1250  return Compare (right) != 0;
1251 }
1252 
1253 
1254 
1255 bool DateTime::operator> (const DateTime& right) const
1256 {
1257  return Compare (right) > 0;
1258 }
1259 
1260 
1261 
1262 bool DateTime::operator>= (const DateTime& right) const
1263 {
1264  return Compare (right) >= 0;
1265 }
1266 
1267 
1268 bool DateTime::operator< (const DateTime& right) const
1269 {
1270  return Compare (right) < 0;
1271 }
1272 
1273 
1274 bool DateTime::operator<= (const DateTime& right) const
1275 {
1276  return Compare (right) <= 0;
1277 }
1278 
1279 
1280 
1281 DateTime DateTime::operator+ (const DateTime& right) const
1282 {
1283  kkint32 netDays = date.Days () + right.date.Days ();
1284  kkint32 netSecs = time.Seconds () + right.time.Seconds ();
1285  netSecs = netSecs % 86400;
1286 
1287  return DateTime (DateType (netDays), TimeType (netSecs));
1288 }
1289 
1290 
1291 
1292 
1293 DateTime DateTime::operator- (const DateTime& right) const
1294 {
1295  kkint32 netDays = date.Days () - right.date.Days ();
1296 
1297  kkint32 leftSecs = time.Seconds ();
1298  kkint32 rightSecs = right.time.Seconds ();
1299 
1300  while (rightSecs > leftSecs)
1301  {
1302  netDays--;
1303  leftSecs = leftSecs + 86400;
1304  }
1305 
1306  kkint32 netSecs = leftSecs - rightSecs;
1307 
1308  kkint32 numOfDaysInSecs = netSecs / 86400;
1309  netSecs = netSecs % 86400;
1310 
1311  return DateTime (DateType (netDays + numOfDaysInSecs), TimeType (netSecs));
1312 }
1313 
1314 
1315 
1316 
1317 
1318 
1319 KKStr& KKB::operator<< ( KKStr& left,
1320  const DateTime& right
1321  )
1322 {
1323  left << right.Date () << "-" << right.Time ();
1324  return left;
1325 }
1326 
1327 
1328 
1329 
1330 std::ostream& KKB::operator<< ( std::ostream& os,
1331  const DateTime& right
1332  )
1333 {
1334  os << right.Date () << "-" << right.Time ();
1335  return os;
1336 }
bool operator>(const DateTime &right) const
Definition: DateTime.cpp:1255
bool operator==(const TimeType &right) const
Definition: DateTime.cpp:890
KKStr(kkint32 size)
Creates a KKStr object that pre-allocates space for &#39;size&#39; characters.
Definition: KKStr.cpp:655
void AddMinutes(kkint32 _mins)
Definition: DateTime.cpp:1139
bool operator==(const DateType &right) const
Definition: DateTime.cpp:542
bool operator<=(const DateTime &right) const
Definition: DateTime.cpp:1274
kkint32 Days() const
Definition: DateTime.cpp:475
bool operator>=(const DateType &right) const
Definition: DateTime.cpp:562
uchar Month() const
Definition: DateTime.h:71
kkuint32 Seconds() const
Definition: DateTime.cpp:925
kkuint32 ToDays() const
Definition: DateTime.cpp:510
void AddDays(kkint32 _days)
Definition: DateTime.cpp:1108
KKStr YYYYMMDDHHMMSS() const
Definition: DateTime.cpp:1209
bool operator<(const DateTime &right) const
Definition: DateTime.cpp:1268
DateType operator-(kkint32 right) const
Definition: DateTime.cpp:634
DateTime operator+(const DateTime &right) const
Definition: DateTime.cpp:1281
double ToHours() const
summary>Number seconds since "0000/01/01 00:00:00"
Definition: DateTime.cpp:1101
void AddSeconds(long _secs)
Definition: DateTime.cpp:1159
void MinutesAdd(kkint32 _mins)
Definition: DateTime.cpp:1194
__int32 kkint32
Definition: KKBaseTypes.h:88
KKStr HHMMSS() const
Definition: DateTime.cpp:847
DateType(KKStr s)
Definition: DateTime.cpp:187
bool operator==(const DateTime &right) const
Definition: DateTime.cpp:1241
kkint32 CountInstancesOf(char ch) const
Definition: KKStr.cpp:1122
KKStr YYYYMMDD() const
Convert into displayable string; ex: 20110517.
Definition: DateTime.cpp:727
bool operator<=(const TimeType &right) const
Definition: DateTime.cpp:961
TimeType & operator=(const TimeType &right)
Definition: DateTime.cpp:879
KKStr HH_MM_SS() const
Definition: DateTime.cpp:841
uchar Hour() const
Definition: DateTime.h:165
KKStr GetNextToken(const char *delStr="\n\t\r ")
Extract next Token from string, tokens will be separated by delimiter characters. ...
KKStr ExtractToken(const char *delStr="\n\t\r ")
Definition: KKStr.cpp:2969
DateType(short _year, uchar _month, uchar _day)
summary> Constructs a date from an integer that represents the total number of days since 1/1/0000 */...
Definition: DateTime.cpp:175
DateType(const DateType &_date)
Definition: DateTime.cpp:166
void HoursAdd(kkint32 hours)
Add _hours to DateTime, will adjust date to accommodate 24 hour clock.
Definition: DateTime.cpp:1186
void SubtractDays(kkint32 _days)
Definition: DateTime.cpp:414
TimeType(kkint32 seconds)
Definition: DateTime.cpp:785
bool operator!=(const TimeType &right) const
Definition: DateTime.cpp:897
DateTime(const KKStr &s)
Definition: DateTime.cpp:1043
KKStr HH_MM_SS() const
Definition: DateTime.cpp:1222
void Hour(uchar _hour)
Definition: DateTime.h:173
DateType operator-(const DateType &right) const
Definition: DateTime.cpp:641
KKStr YYYY_MM_DD() const
Convert into displayable string; ex: 20011/05/17.
Definition: DateTime.cpp:701
KKStr YY_MM_DD() const
Convert into displayable string; ex: 11/05/17.
Definition: DateTime.cpp:691
kkuint64 ToSeconds() const
Definition: DateTime.cpp:512
KKStr MMM_DD_YYYY() const
Convert into displayable string; ex: May/02/2010.
Definition: DateTime.cpp:676
void AddDays(kkint32 _days)
Definition: DateTime.cpp:381
static uchar MonthFromStr(const KKStr &monthStr)
Definition: DateTime.cpp:95
KKStr operator+(const char *right) const
Definition: KKStr.cpp:3986
unsigned __int32 kkuint32
Definition: KKBaseTypes.h:89
static uchar DaysInTheMonth(kkint32 year, uchar month)
Definition: DateTime.cpp:114
KKStr MM_DD_YY() const
Definition: DateTime.cpp:665
uchar Day() const
Definition: DateTime.h:72
KKStr & operator=(KKStr &&src)
Definition: KKStr.cpp:1369
const KKB::DateType & Date() const
Definition: DateTime.h:230
bool operator>(const TimeType &right) const
Definition: DateTime.cpp:905
kkuint32 Len() const
Returns the number of characters in the string.
Definition: KKStr.h:366
bool operator>(const DateType &right) const
Definition: DateTime.cpp:555
bool operator<=(const DateType &right) const
Definition: DateTime.cpp:577
KKTHread * KKTHreadPtr
bool operator<(const DateType &right) const
Definition: DateTime.cpp:569
KKStrParser(const KKStr &_str)
Definition: KKStrParser.cpp:42
void AddHours(kkint32 _hours)
Definition: DateTime.cpp:1114
DateType operator+(kkint32 right) const
Definition: DateTime.cpp:628
uchar Second() const
Definition: DateTime.h:167
kkuint64 ToSeconds() const
Definition: DateTime.cpp:1105
bool Empty() const
Definition: KKStr.h:241
void Second(uchar _second)
Definition: DateTime.h:175
static kkint32 DaysYTDforMonth(kkint32 year, uchar month)
Definition: DateTime.cpp:132
DateType & operator=(const DateType &right)
Definition: DateTime.cpp:462
KKStr SubStrPart(kkint32 firstChar, kkint32 lastChar) const
returns a SubString consisting of all characters starting at index &#39;firstChar&#39; and ending at &#39;lastInd...
Definition: KKStr.cpp:2802
DateTime operator-(const DateTime &right) const
Definition: DateTime.cpp:1293
bool operator<(const TimeType &right) const
Definition: DateTime.cpp:919
unsigned char uchar
Unsigned character.
Definition: KKBaseTypes.h:77
TimeType(KKStr s)
Definition: DateTime.cpp:802
static KKStr Concat(const std::vector< std::string > &values)
Concatenates the list of &#39;std::string&#39; strings.
Definition: KKStr.cpp:1082
DateTime(short _year, uchar _month, uchar _day, uchar _hour, uchar _minute, uchar _second)
Definition: DateTime.cpp:1028
bool operator>=(const TimeType &right) const
Definition: DateTime.cpp:912
DateTime(const DateTime &dateTime)
Definition: DateTime.cpp:1006
Represents a Time, consisting of three fields, Hour, Minute, and Second. summary>Represents Date and ...
Definition: DateTime.h:149
unsigned __int64 kkuint64
Definition: KKBaseTypes.h:91
kkint32 DaysInYear(kkint32 year)
Definition: DateTime.cpp:301
KKStr StrFormatInt(kkint32 val, const char *mask)
Definition: KKStr.cpp:5004
KKStr YYYY_MM_DD_HH_MM_SS() const
Definition: DateTime.cpp:1216
kkuint32 ToDays() const
Definition: DateTime.cpp:1100
std::ostream &__cdecl operator<<(std::ostream &os, const KKStr &str)
KKStr operator+(const KKStr &right) const
Definition: KKStr.cpp:3998
uchar DaysThisMonth() const
returns the number of days in &#39;month&#39; and if leapYear and February returns 29.
Definition: DateTime.cpp:317
uchar Minute() const
Definition: DateTime.h:166
kkuint64 Seconds() const
Definition: DateTime.cpp:1092
TimeType operator+(const TimeType &right) const
Definition: DateTime.cpp:946
kkuint32 ToHours() const
Definition: DateTime.cpp:511
DateType operator+(const DateType &right) const
Definition: DateTime.cpp:621
bool operator!=(const DateType &right) const
Definition: DateTime.cpp:548
double ToMinutes() const
Definition: DateTime.cpp:937
DateType(kkint32 days)
Definition: DateTime.cpp:311
TimeType(const TimeType &time)
Definition: DateTime.cpp:766
void SecondsAdd(long _secs)
Definition: DateTime.cpp:1202
TimeType operator-(const TimeType &right) const
Definition: DateTime.cpp:953
DateType & operator++()
Definition: DateTime.cpp:647
Class that manages the extraction of tokens from a String without being destructive to the original s...
Definition: KKStrParser.h:18
void Minute(uchar _minute)
Definition: DateTime.h:174
double ToHours() const
Definition: DateTime.cpp:931
bool operator!=(const DateTime &right) const
Definition: DateTime.cpp:1248
KKStr YYYY_MMM_DD() const
Convert into displayable string; ex: 20011/May/17.
Definition: DateTime.cpp:712
short Year() const
Definition: DateTime.h:70
TimeType(uchar _hour, uchar _minute, uchar _second)
Definition: DateTime.cpp:774
bool operator>=(const DateTime &right) const
Definition: DateTime.cpp:1262
kkuint64 Seconds() const
Definition: DateTime.cpp:502
Represents a calendar date consisting of three fields, Year, Month, and Day.
Definition: DateTime.h:28
kkint32 CompareIgnoreCase(const char *s2) const
summary>Compares to Strings and returns -1, 0, or 1, indicating if less than, equal, or greater.
Definition: KKStr.cpp:955
DateTime(const DateType &_date, const TimeType &_time)
Definition: DateTime.cpp:1016
DateTime & operator=(const DateTime &right)
Definition: DateTime.cpp:1231
const TimeType & Time() const
Definition: DateTime.h:232
void AddMonths(kkint32 _months)
Definition: DateTime.cpp:446