wxString pgsDateTimeGen::random() { // Get a random number representing seconds MAPM result = pgsMapm::pgs_str_mapm(m_randomizer->random()), quot, rem; // Use hours and seconds for avoiding overflows of seconds result.integer_div_rem(3600, quot, rem); long hours, seconds; pgsMapm::pgs_mapm_str(quot, true).ToLong(&hours); pgsMapm::pgs_mapm_str(rem, true).ToLong(&seconds); wxTimeSpan time_span(hours, 0, seconds, 0); // Add the TimeSpan to the MinDate wxDateTime aux_min(m_min); aux_min.Add(time_span); // Post conditions wxASSERT(aux_min.IsLaterThan(m_min) || aux_min.IsEqualTo(m_min)); wxASSERT(aux_min.IsEarlierThan(m_max) || aux_min.IsEqualTo(m_max)); return aux_min.Format(wxT("%Y-%m-%d %H:%M:%S")); }
void DateUtils::convertAbsolute2DMY(MAPM absolute, MAPM& day, MAPM& month, MAPM& year) { absolute += 1; bool bc = absolute.sign() <= 0; bool fix = false; MAPM div, rem; absolute.integer_div_rem(days_in_400_years, div, rem); year = div * 400; absolute = rem; absolute.integer_div_rem(days_in_100_years, div, rem); if(div <= -4) fix = true; if(div >= 4) { div -= 1; rem += days_in_100_years; } year += div * 100; absolute = rem; absolute.integer_div_rem(days_in_4_years, div, rem); year += div * 4; absolute = rem; absolute.integer_div_rem(days_in_1_years, div, rem); if(div <= -4) fix = true; if(div >= 4) { div -= 1; rem += days_in_1_years; } year += div; absolute = rem; if(bc) { if(fix && absolute.sign() == 0) { // Correct off by one error in year calculations // due to negative leap years absolute += 1; } else { --year; absolute += daysInYear(year); } } else { if(absolute.sign() != 0) { ++year; } } month = 12; day = 31; int *days = isLeapYear(year) ? days_before_month_leap : days_before_month; for(int i = 11; i >= 0; --i) { if(absolute > days[i]) { month = i + 1; day = absolute - days[i]; break; } } }