-// this test miscellaneous static wxDateTime functions
-static void TestTimeStatic()
-{
- puts("\n*** wxDateTime static methods test ***");
-
- // some info about the current date
- int year = wxDateTime::GetCurrentYear();
- printf("Current year %d is %sa leap one and has %d days.\n",
- year,
- wxDateTime::IsLeapYear(year) ? "" : "not ",
- wxDateTime::GetNumberOfDays(year));
-
- wxDateTime::Month month = wxDateTime::GetCurrentMonth();
- printf("Current month is '%s' ('%s') and it has %d days\n",
- wxDateTime::GetMonthName(month, wxDateTime::Name_Abbr).c_str(),
- wxDateTime::GetMonthName(month).c_str(),
- wxDateTime::GetNumberOfDays(month));
-
- // leap year logic
- static const size_t nYears = 5;
- static const size_t years[2][nYears] =
- {
- // first line: the years to test
- { 1990, 1976, 2000, 2030, 1984, },
-
- // second line: TRUE if leap, FALSE otherwise
- { FALSE, TRUE, TRUE, FALSE, TRUE }
- };
-
- for ( size_t n = 0; n < nYears; n++ )
- {
- int year = years[0][n];
- bool should = years[1][n] != 0,
- is = wxDateTime::IsLeapYear(year);
-
- printf("Year %d is %sa leap year (%s)\n",
- year,
- is ? "" : "not ",
- should == is ? "ok" : "ERROR");
-
- wxASSERT( should == wxDateTime::IsLeapYear(year) );
- }
-}
-
-// test constructing wxDateTime objects
-static void TestTimeSet()
-{
- puts("\n*** wxDateTime construction test ***");
-
- for ( size_t n = 0; n < WXSIZEOF(testDates); n++ )
- {
- const Date& d1 = testDates[n];
- wxDateTime dt = d1.DT();
-
- Date d2;
- d2.Init(dt.GetTm());
-
- wxString s1 = d1.Format(),
- s2 = d2.Format();
-
- printf("Date: %s == %s (%s)\n",
- s1.c_str(), s2.c_str(),
- s1 == s2 ? "ok" : "ERROR");
- }
-}
-
-// test time zones stuff
-static void TestTimeZones()
-{
- puts("\n*** wxDateTime timezone test ***");
-
- wxDateTime now = wxDateTime::Now();
-
- printf("Current GMT time:\t%s\n", now.Format("%c", wxDateTime::GMT0).c_str());
- printf("Unix epoch (GMT):\t%s\n", wxDateTime((time_t)0).Format("%c", wxDateTime::GMT0).c_str());
- printf("Unix epoch (EST):\t%s\n", wxDateTime((time_t)0).Format("%c", wxDateTime::EST).c_str());
- printf("Current time in Paris:\t%s\n", now.Format("%c", wxDateTime::CET).c_str());
- printf(" Moscow:\t%s\n", now.Format("%c", wxDateTime::MSK).c_str());
- printf(" New York:\t%s\n", now.Format("%c", wxDateTime::EST).c_str());
-
- wxDateTime::Tm tm = now.GetTm();
- if ( wxDateTime(tm) != now )
- {
- printf("ERROR: got %s instead of %s\n",
- wxDateTime(tm).Format().c_str(), now.Format().c_str());
- }
-}
-
-// test some minimal support for the dates outside the standard range
-static void TestTimeRange()
-{
- puts("\n*** wxDateTime out-of-standard-range dates test ***");
-
- static const char *fmt = "%d-%b-%Y %H:%M:%S";
-
- printf("Unix epoch:\t%s\n",
- wxDateTime(2440587.5).Format(fmt).c_str());
- printf("Feb 29, 0: \t%s\n",
- wxDateTime(29, wxDateTime::Feb, 0).Format(fmt).c_str());
- printf("JDN 0: \t%s\n",
- wxDateTime(0.0).Format(fmt).c_str());
- printf("Jan 1, 1AD:\t%s\n",
- wxDateTime(1, wxDateTime::Jan, 1).Format(fmt).c_str());
- printf("May 29, 2099:\t%s\n",
- wxDateTime(29, wxDateTime::May, 2099).Format(fmt).c_str());
-}
-
-static void TestTimeTicks()
-{
- puts("\n*** wxDateTime ticks test ***");
-
- for ( size_t n = 0; n < WXSIZEOF(testDates); n++ )
- {
- const Date& d = testDates[n];
- if ( d.ticks == -1 )
- continue;
-
- wxDateTime dt = d.DT();
- long ticks = (dt.GetValue() / 1000).ToLong();
- printf("Ticks of %s:\t% 10ld", d.Format().c_str(), ticks);
- if ( ticks == d.ticks )
- {
- puts(" (ok)");
- }
- else
- {
- printf(" (ERROR: should be %ld, delta = %ld)\n",
- d.ticks, ticks - d.ticks);
- }
-
- dt = d.DT().ToTimezone(wxDateTime::GMT0);
- ticks = (dt.GetValue() / 1000).ToLong();
- printf("GMtks of %s:\t% 10ld", d.Format().c_str(), ticks);
- if ( ticks == d.gmticks )
- {
- puts(" (ok)");
- }
- else
- {
- printf(" (ERROR: should be %ld, delta = %ld)\n",
- d.gmticks, ticks - d.gmticks);
- }
- }
-
- puts("");
-}
-
-// test conversions to JDN &c
-static void TestTimeJDN()
-{
- puts("\n*** wxDateTime to JDN test ***");
-
- for ( size_t n = 0; n < WXSIZEOF(testDates); n++ )
- {
- const Date& d = testDates[n];
- wxDateTime dt(d.day, d.month, d.year, d.hour, d.min, d.sec);
- double jdn = dt.GetJulianDayNumber();
-
- printf("JDN of %s is:\t% 15.6f", d.Format().c_str(), jdn);
- if ( jdn == d.jdn )
- {
- puts(" (ok)");
- }
- else
- {
- printf(" (ERROR: should be %f, delta = %f)\n",
- d.jdn, jdn - d.jdn);
- }
- }
-}
-
-// test week days computation
-static void TestTimeWDays()
-{
- puts("\n*** wxDateTime weekday test ***");
-
- // test GetWeekDay()
- size_t n;
- for ( n = 0; n < WXSIZEOF(testDates); n++ )
- {
- const Date& d = testDates[n];
- wxDateTime dt(d.day, d.month, d.year, d.hour, d.min, d.sec);
-
- wxDateTime::WeekDay wday = dt.GetWeekDay();
- printf("%s is: %s",
- d.Format().c_str(),
- wxDateTime::GetWeekDayName(wday).c_str());
- if ( wday == d.wday )
- {
- puts(" (ok)");
- }
- else
- {
- printf(" (ERROR: should be %s)\n",
- wxDateTime::GetWeekDayName(d.wday).c_str());
- }
- }
-
- puts("");
-
- // test SetToWeekDay()
- struct WeekDateTestData
- {
- Date date; // the real date (precomputed)
- int nWeek; // its week index in the month
- wxDateTime::WeekDay wday; // the weekday
- wxDateTime::Month month; // the month
- int year; // and the year
-
- wxString Format() const
- {
- wxString s, which;
- switch ( nWeek < -1 ? -nWeek : nWeek )
- {
- case 1: which = "first"; break;
- case 2: which = "second"; break;
- case 3: which = "third"; break;
- case 4: which = "fourth"; break;
- case 5: which = "fifth"; break;
-
- case -1: which = "last"; break;
- }
-
- if ( nWeek < -1 )
- {
- which += " from end";
- }
-
- s.Printf("The %s %s of %s in %d",
- which.c_str(),
- wxDateTime::GetWeekDayName(wday).c_str(),
- wxDateTime::GetMonthName(month).c_str(),
- year);
-
- return s;
- }
- };
-
- // the array data was generated by the following python program
- /*
-from DateTime import *
-from whrandom import *
-from string import *
-
-monthNames = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]
-wdayNames = [ 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun' ]
-
-week = DateTimeDelta(7)
-
-for n in range(20):
- year = randint(1900, 2100)
- month = randint(1, 12)
- day = randint(1, 28)
- dt = DateTime(year, month, day)
- wday = dt.day_of_week
-
- countFromEnd = choice([-1, 1])
- weekNum = 0;
-
- while dt.month is month:
- dt = dt - countFromEnd * week
- weekNum = weekNum + countFromEnd