-
Notifications
You must be signed in to change notification settings - Fork 1
/
dateutils.cpp
1337 lines (1152 loc) · 34.7 KB
/
dateutils.cpp
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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Authors : Guru Kathiresan - [email protected] ,
FreePascal Team - http://www.freepascal.org
License :
Short Verion : wxVCL is distributed under Modified LGPL
(the same license used by FCL, LCL). In short,
this license allows you to use wxVCL in your application either
statically or dynamically linked (and keep your source code as
closed source) but you cannot sell wxVCL alone as a seperate
product or claim owner ship for it and when you make a change to
the wxVCL library (not your application code), you have to give
the changes (of the wxVCL library code) back to the community.
Long Version : The source code of wxVCL is distributed under the
Library GNU General Public License with the following modification:
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent modules,
and to copy and distribute the resulting executable under terms of your choice,
provided that you also meet, for each linked independent module, the terms
and conditions of the license of that module. An independent module is a module
which is not derived from or based on this library. If you modify this
library, you may extend this exception to your version of the library, but you are
not obligated to do so. If you do not wish to do so, delete this exception
statement from your version.
*/
#include "dateutils.h"
#include <wx/wx.h>
#include <wx/datetime.h>
#include "sysconst.h"
#include "sysutils.h"
int DaysPerYear(bool LeapYear)
{
if (LeapYear)
return 365;
else
return 366;
}
wxDateTime Frac(const wxDateTime& AValue)
{
wxDateTime Result(AValue.GetHour(), AValue.GetMinute(), AValue.GetSecond(),
AValue.GetMillisecond());
return Result;
}
wxDateTime DateOf(const wxDateTime& AValue)
{
return Trunc(AValue);
}
wxDateTime TimeOf(const wxDateTime & AValue)
{
return Frac(AValue);
}
bool IsInLeapYear(const wxDateTime & AValue)
{
return wxDateTime::IsLeapYear(AValue.GetYear());
}
bool IsPM(const wxDateTime & AValue)
{
return AValue.GetHour() >= 12;
}
bool IsValidDate(const unsigned short & AYear, const unsigned short & AMonth,
const unsigned short & ADay)
{
wxDateTime datetm(AYear, AMonth, ADay);
return datetm.IsValid();
}
bool IsValidTime(const unsigned short & AHour, const unsigned short & AMinute,
const unsigned short & ASecond, unsigned short const & AMilliSecond)
{
wxDateTime datetm = wxDateTime::Now();
datetm.Set(AHour, AMinute, ASecond, AMilliSecond);
return datetm.IsValid();
}
bool IsValidDateTime(const unsigned short & AYear,
const unsigned short & AMonth, const unsigned short & ADay,
unsigned short const & AHour, unsigned short const & AMinute,
unsigned short const & ASecond, const unsigned short & AMilliSecond)
{
return IsValidDate(AYear, AMonth, ADay) && IsValidTime(AHour, AMinute,
ASecond, AMilliSecond);
}
bool IsValidDateDay(const unsigned short & AYear,
const unsigned short & ADayOfYear)
{
return (AYear != 0) && (ADayOfYear != 0) && (AYear < 10000) &&
(ADayOfYear <= DaysPerYear(wxDateTime::IsLeapYear(AYear))); ;
}
bool IsValidDateWeek(const unsigned short & AYear,
const unsigned short & AWeekOfYear, const unsigned short & ADayOfWeek)
{
return (AYear != 0) && (AYear < 10000) && (InDayOfWeekRange(ADayOfWeek)) &&
(AWeekOfYear != 0) && (AWeekOfYear <= WeeksInAYear(AYear));
}
bool IsValidDateMonthWeek(const unsigned short & AYear,
const unsigned short & AMonth, const unsigned short & AWeekOfMonth,
const unsigned short & ADayOfWeek)
{
return (AYear != 0) && (AYear < 10000) && (InMonthRange(AMonth)) &&
(InWeekOfMonthRange(AWeekOfMonth)) && (InDayOfWeekRange(ADayOfWeek));
}
unsigned short WeeksInYear(const wxDateTime& AValue)
{
return WeeksInAYear(AValue.GetYear()); ;
}
unsigned short WeeksInAYear(const unsigned short& AYear)
{
unsigned short Result = 52;
unsigned short DOW = DayOfTheWeek(StartOfAYear(AYear));
if ((DOW == 4) || ((DOW == 3) && wxDateTime::IsLeapYear(AYear)))
++Result;
return Result;
}
unsigned short DaysInYear(const wxDateTime& AValue)
{
return DaysPerYear(wxDateTime::IsLeapYear(AValue.GetYear()));
}
unsigned short DaysInAYear(const unsigned short& AYear)
{
return DaysPerYear(wxDateTime::IsLeapYear(AYear));
}
unsigned short DaysInMonth(const wxDateTime & AValue)
{
return MonthDays[wxDateTime::IsLeapYear(AValue.GetYear())]
[AValue.GetMonth()];
}
unsigned short DaysInAMonth(const unsigned short& AYear,
const unsigned short& AMonth)
{
return MonthDays[wxDateTime::IsLeapYear(AYear)][AMonth]; ;
}
wxDateTime Today(void)
{
return wxDateTime::Today();
}
wxDateTime Yesterday(void)
{
return wxDateTime::Today().Subtract(wxDateSpan::Day());
}
wxDateTime Tomorrow()
{
return wxDateTime::Today().Add(wxDateSpan::Day());
}
bool IsToday(const wxDateTime& AValue)
{
return wxDateTime::Today().IsSameDate(AValue);
}
bool IsSameDay(const wxDateTime& AValue, const wxDateTime& ABasis)
{
return Trunc(AValue).IsSameDate(Trunc(ABasis));
}
static unsigned short const DOWMap[7] =
{7, 1, 2, 3, 4, 5, 6};
unsigned short PreviousDayOfWeek(const unsigned short DayOfWeek)
{
if (!(InDayOfWeekRange(DayOfWeek)))
throw EConvertError::CreateFmt(wxString::Format(SErrInvalidDayOfWeek,
DayOfWeek));
return DOWMap[DayOfWeek];
}
unsigned short YearOf(const wxDateTime& AValue)
{
return AValue.GetYear();
}
unsigned short MonthOf(const wxDateTime& AValue)
{
return AValue.GetMonth();
}
unsigned short WeekOf(const wxDateTime& AValue)
{
unsigned short Result = WeekOfTheYear(AValue);
return Result;
}
unsigned short DayOf(const wxDateTime& AValue)
{
return AValue.GetDay();
}
unsigned short HourOf(const wxDateTime& AValue)
{
return AValue.GetHour();
}
unsigned short MinuteOf(const wxDateTime& AValue)
{
return AValue.GetMinute();
}
unsigned short SecondOf(const wxDateTime& AValue)
{
return AValue.GetSecond();
}
unsigned short MilliSecondOf(const wxDateTime& AValue)
{
return AValue.GetMillisecond(); ;
}
wxDateTime StartOfTheYear(const wxDateTime& AValue)
{
wxDateTime Result = AValue;
return Result.SetMonth((wxDateTime::Month)0).SetDay(1);
}
wxDateTime EndOfTheYear(const wxDateTime& AValue)
{
return wxDateTime(AValue.GetYear(), (wxDateTime::Month)11, 30, 23, 59,
59, 999);
}
wxDateTime StartOfAYear(const unsigned short & AYear)
{
return wxDateTime(AYear, 0, 0);
}
wxDateTime EndOfAYear(const unsigned short & AYear)
{
return wxDateTime(AYear, (wxDateTime::Month)12, 31, 23, 59, 59, 999);
}
wxDateTime StartOfTheMonth(const wxDateTime & AValue)
{
wxDateTime Result = AValue;
Result.SetDay(0);
return Result;
}
wxDateTime EndOfTheMonth(const wxDateTime & AValue)
{
wxDateTime Result(MonthDays[IsInLeapYear(AValue)][AValue.GetMonth()],
(wxDateTime::Month) AValue.GetMonth(), AValue.GetYear(),
(wxDateTime::wxDateTime_t)23, (wxDateTime::wxDateTime_t)59,
(wxDateTime::wxDateTime_t)59, (wxDateTime::wxDateTime_t)999);
return Result;
}
wxDateTime StartOfAMonth(const unsigned short & AYear,
const unsigned short & AMonth)
{
return wxDateTime(AYear, AMonth, 0);
}
wxDateTime EndOfAMonth(const unsigned short & AYear,
const unsigned short & AMonth)
{
return EncodeDateTime(AYear, AMonth, MonthDays[IsInLeapYear(AYear)][AMonth],
23, 59, 59, 999);
}
wxDateTime StartOfTheWeek(const wxDateTime & AValue)
{
return Trunc(AValue - wxDateSpan(0, DayOfTheWeek(AValue) + 1));
}
wxDateTime EndOfTheWeek(const wxDateTime & AValue)
{
return EndOfTheDay(AValue - wxDateSpan(0, DayOfTheWeek(AValue) + 7));
}
wxDateTime StartOfAWeek(const unsigned short & AYear,
const unsigned short & AWeekOfYear, const unsigned short & ADayOfWeek)
{
return EncodeDateWeek(AYear, AWeekOfYear, ADayOfWeek);
}
wxDateTime StartOfAWeek(const unsigned short & AYear,
const unsigned short & AWeekOfYear)
{
return StartOfAWeek(AYear, AWeekOfYear, 1);
}
wxDateTime EndOfAWeek(const unsigned short & AYear,
const unsigned short & AWeekOfYear, const unsigned short & ADayOfWeek)
{
return EndOfTheDay(EncodeDateWeek(AYear, AWeekOfYear, ADayOfWeek));
}
wxDateTime EndOfAWeek(const unsigned short & AYear,
const unsigned short & AWeekOfYear)
{
return EndOfAWeek(AYear, AWeekOfYear, 7);
}
wxDateTime StartOfTheDay(const wxDateTime & AValue)
{
return Trunc(AValue);
}
wxDateTime EndOfTheDay(const wxDateTime & AValue)
{
wxDateTime Result = AValue;
Result.Set(23, 59, 59, 999);
return Result;
}
wxDateTime StartOfADay(const unsigned short & AYear,
const unsigned short & AMonth, const unsigned short & ADay)
{
wxDateTime Result(AYear, AMonth, ADay);
return Result;
}
wxDateTime StartOfADay(const unsigned short& AYear,
const unsigned short & ADayOfYear)
{
wxDateTime Result(AYear, 1, 1);
Result = Result.Add(wxDateSpan(0, 0, 0, ADayOfYear - 1));
return Result;
}
wxDateTime EndOfADay(const unsigned short & AYear,
const unsigned short & AMonth, const unsigned short & ADay)
{
return EndOfTheDay(EncodeDate(AYear, AMonth, ADay));
}
wxDateTime EndOfADay(const unsigned short & AYear,
const unsigned short & ADayOfYear)
{
return StartOfADay(AYear, ADayOfYear).Set(23, 59, 59, 999);
}
unsigned short MonthOfTheYear(const wxDateTime & AValue)
{
unsigned short Result, Y, D;
DecodeDate(AValue, Y, Result, D);
return Result;
}
unsigned short WeekOfTheYear(const wxDateTime & AValue)
{
unsigned short Result, Y, DOW;
DecodeDateWeek(AValue, Y, Result, DOW);
return Result;
}
unsigned short WeekOfTheYear(const wxDateTime & AValue, unsigned short & AYear)
{
unsigned short Result, DOW;
DecodeDateWeek(AValue, AYear, Result, DOW);
return Result;
}
unsigned short DayOfTheYear(const wxDateTime & AValue)
{
wxTimeSpan tsp = AValue.Subtract(StartOfTheYear(AValue));
return tsp.GetDays();
}
unsigned short HourOfTheYear(const wxDateTime & AValue)
{
unsigned short Result, H, M, S, MS;
DecodeTime(AValue, H, M, S, MS);
Result = H + ((DayOfTheYear(AValue) - 1) * 24);
return Result;
}
unsigned long MinuteOfTheYear(const wxDateTime & AValue)
{
unsigned long Result;
unsigned short H, M, S, MS;
DecodeTime(AValue, H, M, S, MS);
Result = M + (H + ((DayOfTheYear(AValue) - 1) * 24)) * 60;
return Result;
}
unsigned long SecondOfTheYear(const wxDateTime & AValue)
{
unsigned long Result;
unsigned short H, M, S, MS;
DecodeTime(AValue, H, M, S, MS);
Result = (M + (H + ((DayOfTheYear(AValue) - 1) * 24)) * 60) * 60 + S;
return Result;
}
long MilliSecondOfTheYear(const wxDateTime & AValue)
{
long Result;
unsigned short H, M, S, MS;
DecodeTime(AValue, H, M, S, MS);
Result = ((M + (H + ((DayOfTheYear(AValue) - 1) * 24)) * 60) * 60 + S)
* 1000 + MS;
return Result;
}
unsigned short WeekOfTheMonth(const wxDateTime & AValue)
{
unsigned short Result, Y, M, DOW;
DecodeDateMonthWeek(AValue, Y, M, Result, DOW);
return Result;
}
unsigned short WeekOfTheMonth(const wxDateTime & AValue, unsigned short & AYear,
unsigned short & AMonth)
{
unsigned short Result, DOW;
DecodeDateMonthWeek(AValue, AYear, AMonth, Result, DOW);
return Result;
}
unsigned short DayOfTheMonth(const wxDateTime & AValue)
{
unsigned short Result, Y, M;
DecodeDate(AValue, Y, M, Result);
return Result;
}
unsigned short HourOfTheMonth(const wxDateTime & AValue)
{
unsigned short Result, Y, M, D, H, N, S, MS;
DecodeDateTime(AValue, Y, M, D, H, N, S, MS);
Result = (D - 1) * 24 + H;
return Result;
}
unsigned short MinuteOfTheMonth(const wxDateTime & AValue)
{
unsigned short Result, Y, M, D, H, N, S, MS;
DecodeDateTime(AValue, Y, M, D, H, N, S, MS);
Result = ((D - 1) * 24 + H) * 60 + N;
return Result;
}
unsigned long SecondOfTheMonth(const wxDateTime & AValue)
{
unsigned long Result;
unsigned short Y, M, D, H, N, S, MS;
DecodeDateTime(AValue, Y, M, D, H, N, S, MS);
Result = (((D - 1) * 24 + H) * 60 + N) * 60 + S;
return Result;
}
unsigned long MilliSecondOfTheMonth(const wxDateTime & AValue)
{
unsigned long Result;
unsigned short Y, M, D, H, N, S, MS;
DecodeDateTime(AValue, Y, M, D, H, N, S, MS);
Result = ((((D - 1) * 24 + H) * 60 + N) * 60 + S) * 1000 + MS;
return Result;
}
unsigned short DayOfTheWeek(const wxDateTime & AValue)
{
unsigned short Result;
Result = DOWMap[DayOfWeek(AValue)];
return Result;
}
unsigned short HourOfTheWeek(const wxDateTime & AValue)
{
unsigned short Result, H, M, S, MS;
DecodeTime(AValue, H, M, S, MS);
Result = (DayOfTheWeek(AValue) - 1) * 24 + H;
return Result;
}
unsigned short MinuteOfTheWeek(const wxDateTime & AValue)
{
unsigned short Result, H, M, S, MS;
DecodeTime(AValue, H, M, S, MS);
Result = ((DayOfTheWeek(AValue) - 1) * 24 + H) * 60 + M;
return Result;
}
unsigned long SecondOfTheWeek(const wxDateTime & AValue)
{
unsigned long Result;
unsigned short H, M, S, MS;
DecodeTime(AValue, H, M, S, MS);
Result = (((DayOfTheWeek(AValue) - 1) * 24 + H) * 60 + M) * 60 + S;
return Result;
}
unsigned long MilliSecondOfTheWeek(const wxDateTime & AValue)
{
unsigned long Result;
unsigned short H, M, S, MS;
DecodeTime(AValue, H, M, S, MS);
Result = ((((DayOfTheWeek(AValue) - 1) * 24 + H) * 60 + M) * 60 + S)
* 1000 + MS;
return Result;
}
unsigned short HourOfTheDay(const wxDateTime & AValue)
{
unsigned short Result, M, S, MS;
DecodeTime(AValue, Result, M, S, MS);
return Result;
}
unsigned short MinuteOfTheDay(const wxDateTime & AValue)
{
unsigned short Result, H, M, S, MS;
DecodeTime(AValue, H, M, S, MS);
Result = (H * 60) + M;
return Result;
}
unsigned long SecondOfTheDay(const wxDateTime & AValue)
{
unsigned long Result;
unsigned short H, M, S, MS;
DecodeTime(AValue, H, M, S, MS);
Result = ((H * 60) + M) * 60 + S;
return Result;
}
unsigned long MilliSecondOfTheDay(const wxDateTime & AValue)
{
unsigned long Result;
unsigned short H, M, S, MS;
DecodeTime(AValue, H, M, S, MS);
Result = (((H * 60) + M) * 60 + S) * 1000 + MS;
return Result;
}
unsigned short MinuteOfTheHour(const wxDateTime & AValue)
{
unsigned short Result, H, S, MS;
DecodeTime(AValue, H, Result, S, MS);
return Result;
}
unsigned short SecondOfTheHour(const wxDateTime & AValue)
{
unsigned short Result, H, M, S, MS;
DecodeTime(AValue, H, M, S, MS);
Result = M * 60 + S;
return Result;
}
unsigned long MilliSecondOfTheHour(const wxDateTime & AValue)
{
unsigned long Result;
unsigned short H, M, S, MS;
DecodeTime(AValue, H, M, S, MS);
Result = (M * 60 + S) * 1000 + MS;
return Result;
}
unsigned short SecondOfTheMinute(const wxDateTime & AValue)
{
unsigned short Result;
unsigned short H, M, MS;
DecodeTime(AValue, H, M, Result, MS);
return Result;
}
unsigned long MilliSecondOfTheMinute(const wxDateTime & AValue)
{
unsigned long Result;
unsigned short H, M, S, MS;
DecodeTime(AValue, H, M, S, MS);
Result = S * 1000 + MS;
return Result;
}
unsigned short MilliSecondOfTheSecond(const wxDateTime & AValue)
{
unsigned short Result;
unsigned short H, M, S;
DecodeTime(AValue, H, M, S, Result);
return Result;
}
bool WithinPastYears(const wxDateTime & ANow, const wxDateTime & AThen,
const int & AYears)
{
return YearsBetween(ANow, AThen) <= AYears;
}
bool WithinPastMonths(const wxDateTime & ANow, const wxDateTime & AThen,
const int & AMonths)
{
return MonthsBetween(ANow, AThen) <= AMonths;
}
bool WithinPastWeeks(const wxDateTime & ANow, const wxDateTime & AThen,
const int & AWeeks)
{
return WeeksBetween(ANow, AThen) <= AWeeks;
}
bool WithinPastDays(const wxDateTime & ANow, const wxDateTime & AThen,
const int & ADays)
{
return DaysBetween(ANow, AThen) <= ADays;
}
bool WithinPastHours(const wxDateTime & ANow, const wxDateTime & AThen,
const long & AHours)
{
return HoursBetween(ANow, AThen) <= AHours;
}
bool WithinPastMinutes(const wxDateTime & ANow, const wxDateTime & AThen,
const long & AMinutes)
{
return MinutesBetween(ANow, AThen) <= AMinutes;
}
bool WithinPastSeconds(const wxDateTime & ANow, const wxDateTime & AThen,
const long & ASeconds)
{
return SecondsBetween(ANow, AThen) <= ASeconds;
}
bool WithinPastMilliSeconds(const wxDateTime & ANow, const wxDateTime & AThen,
const long & AMilliSeconds)
{
return MilliSecondsBetween(ANow, AThen) <= AMilliSeconds;
}
int YearsBetween(const wxDateTime & ANow, const wxDateTime & AThen)
{
return (AThen.GetYear() - ANow.GetYear());
}
int MonthsBetween(const wxDateTime & ANow, const wxDateTime & AThen)
{
wxUnusedVar(ANow);
wxUnusedVar(AThen);
// Fixme: Important
THROW_NOT_IMPLEMENTED_ERROR;
/*
wxTimeSpan tsp = AThen.Subtract(ANow);
wxDateSpan Result = wxDateSpan(0,0,0,0);
Result = tsp + Result;
return Result.GetMonths();
*/
}
int WeeksBetween(const wxDateTime & ANow, const wxDateTime & AThen)
{
wxTimeSpan tsp = AThen.Subtract(ANow);
return tsp.GetWeeks();
}
int DaysBetween(const wxDateTime & ANow, const wxDateTime & AThen)
{
wxTimeSpan tsp = AThen.Subtract(ANow);
return tsp.GetDays();
}
long HoursBetween(const wxDateTime & ANow, const wxDateTime & AThen)
{
long Result = (DaysBetween(ANow, AThen) * HoursPerDay);
// Fixme: Check if we need to give the exact hours
// Result = Result - ANow.GetHour() + AThen.GetHour();
return Result;
}
long MinutesBetween(const wxDateTime & ANow, const wxDateTime & AThen)
{
long Result = (DaysBetween(ANow, AThen) * MinsPerDay);
// Fixme: Check if we need to give the exact hours
// Result = Result - ANow.GetHour() + AThen.GetHour();
return Result;
}
long SecondsBetween(const wxDateTime & ANow, const wxDateTime & AThen)
{
long Result = (DaysBetween(ANow, AThen) * SecsPerDay);
// Fixme: Check if we need to give the exact hours
// Result = Result - ANow.GetHour() + AThen.GetHour();
return Result;
}
long MilliSecondsBetween(const wxDateTime & ANow, const wxDateTime & AThen)
{
long Result = (DaysBetween(ANow, AThen) * MSecsPerDay);
// Fixme: Check if we need to give the exact hours
// Result = Result - ANow.GetHour() + AThen.GetHour();
return Result;
}
double YearSpan(const wxDateTime & ANow, const wxDateTime & AThen)
{
return (DaysBetween(ANow, AThen) * 1.0) / ApproxDaysPerYear;
}
double MonthSpan(const wxDateTime & ANow, const wxDateTime & AThen)
{
return (DaysBetween(ANow, AThen) * 1.0) / ApproxDaysPerMonth;
}
double WeekSpan(const wxDateTime & ANow, const wxDateTime & AThen)
{
return (DaysBetween(ANow, AThen) * 1.0) / 7;
}
double DaySpan(const wxDateTime & ANow, const wxDateTime & AThen)
{
return (DaysBetween(ANow, AThen) * 1.0);
}
double HourSpan(const wxDateTime & ANow, const wxDateTime & AThen)
{
return (DaysBetween(ANow, AThen) * HoursPerDay);
}
double MinuteSpan(const wxDateTime & ANow, const wxDateTime & AThen)
{
return (DaysBetween(ANow, AThen) * MinsPerDay);
}
double SecondSpan(const wxDateTime & ANow, const wxDateTime & AThen)
{
return (DaysBetween(ANow, AThen) * SecsPerDay);
}
double MilliSecondSpan(const wxDateTime & ANow, const wxDateTime & AThen)
{
return (DaysBetween(ANow, AThen) * MSecsPerDay);
}
wxDateTime IncYear(const wxDateTime & AValue, const int & ANumberOfYears)
{
wxDateTime Result;
unsigned short Y, M, D, H, N, S, MS;
DecodeDateTime(AValue, Y, M, D, H, N, S, MS);
Y = Y + ANumberOfYears;
if ((M == 2) && (D == 29) && (!IsLeapYear(Y)))
D = 28;
Result = EncodeDateTime(Y, M, D, H, N, S, MS);
return Result;
}
wxDateTime IncYear(const wxDateTime & AValue)
{
return IncYear(AValue, 1);
}
wxDateTime IncWeek(const wxDateTime & AValue, const int & ANumberOfWeeks)
{
return AValue.Add(wxDateSpan(0, 0, ANumberOfWeeks, 0));
}
wxDateTime IncWeek(const wxDateTime & AValue)
{
return IncWeek(AValue, 1);
}
wxDateTime IncDay(const wxDateTime & AValue, const int & ANumberOfDays)
{
return AValue.Add(wxDateSpan(0, 0, 0, ANumberOfDays));
}
wxDateTime IncDay(const wxDateTime & AValue)
{
return IncDay(AValue, 1);
}
wxDateTime IncHour(const wxDateTime & AValue, const long & ANumberOfHours)
{
return AValue.Add(wxTimeSpan(ANumberOfHours, 0, 0, 0));
}
wxDateTime IncHour(const wxDateTime & AValue)
{
return IncHour(AValue, 1);
}
wxDateTime IncMinute(const wxDateTime & AValue, const long & ANumberOfMinutes)
{
return AValue.Add(wxTimeSpan(0, ANumberOfMinutes, 0, 0));
}
wxDateTime IncMinute(const wxDateTime & AValue)
{
return IncMinute(AValue, 1);
}
wxDateTime IncSecond(const wxDateTime & AValue, const long & ANumberOfSeconds)
{
return AValue.Add(wxTimeSpan(0, 0, ANumberOfSeconds, 0));
}
wxDateTime IncSecond(const wxDateTime & AValue)
{
return IncSecond(AValue, 1);
}
wxDateTime IncMilliSecond(const wxDateTime & AValue,
const long & ANumberOfMilliSeconds)
{
return AValue.Add(wxTimeSpan(0, 0, 0, ANumberOfMilliSeconds));
}
wxDateTime IncMilliSecond(const wxDateTime & AValue)
{
return IncMilliSecond(AValue, 1);
}
wxDateTime EncodeDateTime(const unsigned short & AYear,
const unsigned short & AMonth, const unsigned short & ADay,
const unsigned short & AHour, const unsigned short & AMinute,
const unsigned short & ASecond, const unsigned short & AMilliSecond)
{
wxDateTime Result;
if (!TryEncodeDateTime(AYear, AMonth, ADay, AHour, AMinute, ASecond,
AMilliSecond, Result))
InvalidDateTimeError(AYear, AMonth, ADay, AHour, AMinute, ASecond,
AMilliSecond);
return Result;
}
void DecodeDateTime(const wxDateTime & AValue, unsigned short & AYear,
unsigned short & AMonth, unsigned short & ADay, unsigned short & AHour,
unsigned short & AMinute, unsigned short & ASecond,
unsigned short & AMilliSecond)
{
DecodeDate(AValue, AYear, AMonth, ADay);
DecodeTime(AValue, AHour, AMinute, ASecond, AMilliSecond);
}
bool TryEncodeDateTime(const unsigned short & AYear,
const unsigned short & AMonth, const unsigned short & ADay,
const unsigned short & AHour, const unsigned short & AMinute,
const unsigned short & ASecond, const unsigned short & AMilliSecond,
wxDateTime & AValue)
{
wxDateTime dt((wxDateTime::wxDateTime_t) ADay, (wxDateTime::Month) AMonth,
AYear, AHour, AMinute, ASecond, AMilliSecond);
if (dt.IsValid() == false)
return false;
AValue = dt;
return true;
}
wxDateTime EncodeDateWeek(const unsigned short & AYear,
const unsigned short & AWeekOfYear, const unsigned short & ADayOfWeek)
{
wxDateTime Result;
if (!TryEncodeDateWeek(AYear, AWeekOfYear, Result, ADayOfWeek))
InvalidDateWeekError(AYear, AWeekOfYear, ADayOfWeek);
return Result;
}
wxDateTime EncodeDateWeek(const unsigned short & AYear,
const unsigned short & AWeekOfYear)
{
return EncodeDateWeek(AYear, AWeekOfYear, 1);
}
void DecodeDateWeek(const wxDateTime & AValue, unsigned short & AYear,
unsigned short & AWeekOfYear, unsigned short & ADayOfWeek)
{
AYear = AValue.GetYear();
AWeekOfYear = WeekOfTheYear(AValue);
ADayOfWeek = DayOfWeek(AValue);
}
bool TryEncodeDateWeek(const unsigned short & AYear,
const unsigned short & AWeekOfYear, wxDateTime & AValue,
const unsigned short & ADayOfWeek)
{
unsigned short DOW;
int Rest;
wxDateTime dt;
bool Result = IsValidDateWeek(AYear, AWeekOfYear, ADayOfWeek);
if (Result)
{
AValue = EncodeDate(AYear, 1, 1).Add
(wxDateSpan(0, 0, 0, (7 * (AWeekOfYear - 1))));
DOW = DayOfTheWeek(AValue);
Rest = ADayOfWeek - DOW;
if ((DOW > 4))
Rest += 7;
AValue = AValue.Add(wxDateSpan(0, 0, 0, Rest));
}
return Result;
}
bool TryEncodeDateWeek(const unsigned short & AYear,
const unsigned short & AWeekOfYear, wxDateTime & AValue)
{
return TryEncodeDateWeek(AYear, AWeekOfYear, AValue, 1);
}
wxDateTime EncodeDateDay(const unsigned short & AYear,
const unsigned short & ADayOfYear)
{
wxDateTime Result;
if (!TryEncodeDateDay(AYear, ADayOfYear, Result))
InvalidDateDayError(AYear, ADayOfYear);
return Result;
}
void DecodeDateDay(const wxDateTime & AValue, unsigned short & AYear,
unsigned short & ADayOfYear)
{
unsigned short M, D;
DecodeDate(AValue, AYear, M, D);
wxTimeSpan tsp = AValue - EncodeDate(AYear, 1, 1);
ADayOfYear = tsp.GetDays() + 1;
}
bool TryEncodeDateDay(const unsigned short & AYear,
const unsigned short & ADayOfYear, wxDateTime & AValue)
{
bool Result = (ADayOfYear != 0) && (ADayOfYear <=
DaysPerYear(IsLeapYear(AYear)));
if (Result)
AValue = EncodeDate(AYear, 1, 1) + wxDateSpan(0, 0, 0, ADayOfYear - 1);
return Result;
}
wxDateTime EncodeDateMonthWeek(const unsigned short & AYear,
const unsigned short & AMonth, const unsigned short & AWeekOfMonth,
const unsigned short & ADayOfWeek)
{
wxDateTime Result;
if (!TryEncodeDateMonthWeek(AYear, AMonth, AWeekOfMonth, ADayOfWeek,
Result))
InvalidDateMonthWeekError(AYear, AMonth, AWeekOfMonth, ADayOfWeek);
return Result;
}
void DecodeDateMonthWeek(const wxDateTime & AValue, unsigned short & AYear,
unsigned short & AMonth, unsigned short & AWeekOfMonth,
unsigned short & ADayOfWeek)
{
AYear = AValue.GetYear();
AMonth = AValue.GetMonth();
AWeekOfMonth = WeekOfTheMonth(AValue);
ADayOfWeek = DayOfTheWeek(AValue);
}
bool TryEncodeDateMonthWeek(const unsigned short & AYear,
const unsigned short & AMonth, const unsigned short & AWeekOfMonth,
const unsigned short & ADayOfWeek, wxDateTime & AValue)
{
unsigned short S;
int DOM;
bool Result = IsValidDateMonthWeek(AYear, AMonth, AWeekOfMonth, ADayOfWeek);
if (Result)
{
AValue = EncodeDate(AYear, AMonth, 1);
DOM = (AWeekOfMonth - 1) * 7 + ADayOfWeek - 1;
/* Correct for first week in last month. */
S = DayOfTheWeek(AValue);
DOM -= S - 1;
if ((S == DayFriday) || (S == DaySaturday) || (S == DaySunday))
DOM += 7;
AValue = AValue.Add(wxDateSpan(0, 0, 0, DOM));
}
return Result;
}
wxDateTime RecodeYear(const wxDateTime & AValue, const unsigned short & AYear)
{
return wxDateTime(AValue).SetYear(AYear);
}
wxDateTime RecodeMonth(const wxDateTime & AValue, const unsigned short & AMonth)
{
return wxDateTime(AValue).SetMonth((wxDateTime::Month) AMonth);
}
wxDateTime RecodeDay(const wxDateTime & AValue, const unsigned short & ADay)
{
return wxDateTime(AValue).SetDay(ADay);
}
wxDateTime RecodeHour(const wxDateTime & AValue, const unsigned short & AHour)