@@ -639,26 +639,22 @@ static const int64_t SecondsFrom1900To2001 = INT64_C(3187296000);
639
639
640
640
static const int64_t NtTo1900OffsetInterval = INT64_C(0x014F373BFDE04000 );
641
641
642
- static int count_leap_years (int yearsSince1900)
642
+ static int count_leap_years (const int yearsSince1900)
643
643
{
644
- int result = 0 ;
645
- if (yearsSince1900 > 101 )
646
- {
647
- result += 25 ;
648
- yearsSince1900 -= 101 ;
649
- }
644
+ int tmpYears = yearsSince1900 + 299 ; // shift into 1601, the first 400 year cycle including 1900
650
645
651
- int year400 = yearsSince1900 / 400 ;
652
- yearsSince1900 -= year400 * 400 ;
653
- result + = year400 * 97 ;
646
+ int year400 = tmpYears / 400 ;
647
+ tmpYears -= year400 * 400 ;
648
+ int result = year400 * 97 ;
654
649
655
- int year100 = yearsSince1900 / 100 ;
656
- yearsSince1900 -= year100 * 100 ;
650
+ int year100 = tmpYears / 100 ;
651
+ tmpYears -= year100 * 100 ;
657
652
result += year100 * 24 ;
658
653
659
- int year4 = yearsSince1900 / 4 ;
660
- yearsSince1900 -= year4 * 4 ;
661
- result += year4;
654
+ result += tmpYears / 4 ;
655
+
656
+ // subtract off leap years from 1601
657
+ result -= 72 ;
662
658
663
659
return result;
664
660
}
@@ -724,16 +720,11 @@ struct compute_year_result
724
720
int secondsLeftThisYear;
725
721
};
726
722
723
+ static const int64_t secondsFrom1601To1900 = INT64_C(9435484800 );
724
+
727
725
static compute_year_result compute_year (int64_t secondsSince1900)
728
726
{
729
- int year = 0 ;
730
- int64_t secondsLeft = secondsSince1900;
731
- if (secondsSince1900 >= SecondsFrom1900To2001)
732
- {
733
- // After year 2001, shift there and start normal 400 year cycle
734
- year += 101 ;
735
- secondsLeft -= SecondsFrom1900To2001;
736
- }
727
+ int64_t secondsLeft = secondsSince1900 + secondsFrom1601To1900; // shift to start of this 400 year cycle
737
728
738
729
int year400 = static_cast <int >(secondsLeft / SecondsIn400Years);
739
730
secondsLeft -= year400 * SecondsIn400Years;
@@ -747,8 +738,8 @@ static compute_year_result compute_year(int64_t secondsSince1900)
747
738
int year1 = secondsInt / SecondsInYear;
748
739
secondsInt -= year1 * SecondsInYear;
749
740
750
- year += year400 * 400 + year100 * 100 + year4 * 4 + year1;
751
- return {year , secondsInt};
741
+ // shift back to 1900 base from 1601:
742
+ return {year400 * 400 + year100 * 100 + year4 * 4 + year1 - 299 , secondsInt};
752
743
}
753
744
754
745
utility::string_t datetime::to_string (date_format format) const
@@ -779,7 +770,7 @@ utility::string_t datetime::to_string(date_format format) const
779
770
}
780
771
781
772
const auto monthDay = yearDay - monthTable[month] + 1 ;
782
- const auto weekday = static_cast <int >((secondsSince1900 / SecondsInDay + 3 ) % 7 );
773
+ const auto weekday = static_cast <int >((secondsSince1900 / SecondsInDay + 1 ) % 7 );
783
774
784
775
char outBuffer[38 ]; // Thu, 01 Jan 1970 00:00:00 GMT\0
785
776
// 1970-01-01T00:00:00.1234567Z\0
0 commit comments