//#define TEST_ARRAYS
//#define TEST_LOG
//#define TEST_STRINGS
-#define TEST_THREADS
-//#define TEST_TIME
+//#define TEST_THREADS
+#define TEST_TIME
//#define TEST_LONGLONG
// ============================================================================
wxDateTime now = wxDateTime::Now();
printf("Current GMT time:\t%s\n", now.ToGMT().Format().c_str());
- //TODO printf("Unix epoch (GMT):\t%s\n", wxDateTime((time_t)0).MakeGMT().Format().c_str());
+ printf("Unix epoch (GMT):\t%s\n", wxDateTime((time_t)0).MakeGMT().Format().c_str());
printf("Current time in Paris:\t%s\n", now.ToTimezone(wxDateTime::CET).Format().c_str());
printf(" Moscow:\t%s\n", now.ToTimezone(wxDateTime::MSK).Format().c_str());
printf(" New York:\t%s\n", now.ToTimezone(wxDateTime::EST).Format().c_str());
{
puts("\n*** wxDateTime out-of-standard-range dates test ***");
+ printf("Unix epoch:\t%s\n",
+ wxDateTime(2440587.5).Format().c_str());
+ printf("Feb 29, 0: \t%s\n",
+ wxDateTime(29, wxDateTime::Feb, 0).Format().c_str());
printf("JDN 0: \t%s\n",
wxDateTime(0.0).Format().c_str());
printf("Jan 1, 1AD:\t%s\n",
}
// test conversions to JDN &c
-static void TestTimeJulian()
+static void TestTimeJDN()
{
puts("\n*** wxDateTime to JDN test ***");
- printf("JDN of current time:\t%f\n", wxDateTime::Now().GetJulianDayNumber());
- printf("JDN of Jan 1, 1900: \t%f\n",
- wxDateTime(1, wxDateTime::Jan, 1900).GetJulianDayNumber());
- printf("JDN of Jan 1, 1BC: \t%f\n",
- wxDateTime(1, wxDateTime::Jan, 0).GetJulianDayNumber());
- printf("JDN 0: \t%f\n",
- wxDateTime(24, wxDateTime::Nov, -4713, 12, 0, 0).GetJulianDayNumber());
+ struct Date
+ {
+ wxDateTime::wxDateTime_t day;
+ wxDateTime::Month month;
+ int year;
+ double jdn;
+ };
+
+ static const Date testDates[] =
+ {
+ { 21, wxDateTime::Jan, 2222, 2532648.5 },
+ { 29, wxDateTime::May, 1976, 2442927.5 },
+ { 1, wxDateTime::Jan, 1970, 2440587.5 },
+ { 1, wxDateTime::Jan, 1900, 2415020.5 },
+ { 15, wxDateTime::Oct, 1582, 2299160.5 },
+ { 4, wxDateTime::Oct, 1582, 2299149.5 },
+ { 1, wxDateTime::Mar, 1, 1721484.5 },
+ { 1, wxDateTime::Jan, 1, 1721425.5 },
+ { 31, wxDateTime::Dec, 0, 1721424.5 },
+ { 1, wxDateTime::Jan, 0, 1721059.5 },
+ { 12, wxDateTime::Aug, -1234, 1270573.5 },
+ { 12, wxDateTime::Aug, -4000, 260313.5 },
+ { 24, wxDateTime::Nov, -4713, -0.5 },
+ };
+
+ for ( size_t n = 0; n < WXSIZEOF(testDates); n++ )
+ {
+ const Date& d = testDates[n];
+ wxDateTime dt(d.day, d.month, d.year);
+ double jdn = dt.GetJulianDayNumber();
+
+ printf("JDN of %s %02d, %4d%s is:\t%f",
+ wxDateTime::GetMonthName(d.month).c_str(),
+ d.day,
+ wxDateTime::ConvertYearToBC(d.year),
+ d.year > 0 ? "AD" : "BC",
+ jdn);
+ if ( jdn == d.jdn )
+ {
+ puts(" (ok)");
+ }
+ else
+ {
+ printf(" (ERROR: should be %f, delta = %f)\n",
+ d.jdn, jdn - d.jdn);
+ }
+ }
}
#endif // TEST_TIME
TestTimeSet();
TestTimeZones();
TestTimeRange();
- TestTimeJulian();
+ TestTimeJDN();
#endif // TEST_TIME
wxUninitialize();
-/////////////////////////////////////////////////////////////////////////////
+///////////////////////////////////////////////////////////////////////////////
// Name: wx/datetime.h
// Purpose: implementation of time/date related classes
// Author: Vadim Zeitlin
// Modified by:
// Created: 11.05.99
// RCS-ID: $Id$
-// Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
+// Copyright: (c) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
+// parts of code taken from sndcal library by Scott E. Lee:
+//
+// Copyright 1993-1995, Scott E. Lee, all rights reserved.
+// Permission granted to use, copy, modify, distribute and sell
+// so long as the above copyright and this permission statement
+// are retained in all copies.
+//
// Licence: wxWindows license
-/////////////////////////////////////////////////////////////////////////////
+///////////////////////////////////////////////////////////////////////////////
+
+/*
+ * Implementation notes:
+ *
+ * 1. the time is stored as a 64bit integer containing the signed number of
+ * milliseconds since Jan 1. 1970 (the Unix Epoch)
+ *
+ * 2. the range is thus something about 580 million years, but due to current
+ * algorithms limitations, only dates from Nov 24, 4714BC are handled
+ *
+ * 3. standard ANSI C functions are used to do time calculations whenever
+ * possible, i.e. when the date is in the range Jan 1, 1970 to 2038
+ *
+ * 4. otherwise, the calculations are done by converting the date to/from JDN
+ * first (the range limitation mentioned above comes from here: the
+ * algorithm used by Scott E. Lee's code only works for positive JDNs, more
+ * or less)
+ *
+ */
// ============================================================================
// declarations
// (i.e. JDN(Jan 1, 1970) = 2440587.5)
static const int EPOCH_JDN = 2440587;
+// the date of JDN -0.5 (as we don't work with fractional parts, this is the
+// reference date for us) is Nov 24, 4714BC
+static const int JDN_0_YEAR = -4713;
+static const int JDN_0_MONTH = wxDateTime::Nov;
+static const int JDN_0_DAY = 24;
+
+// the constants used for JDN calculations
+static const int JDN_OFFSET = 32046;
+static const int DAYS_PER_5_MONTHS = 153;
+static const int DAYS_PER_4_YEARS = 1461;
+static const int DAYS_PER_400_YEARS = 146097;
+
// ----------------------------------------------------------------------------
// globals
// ----------------------------------------------------------------------------
if ( !s_timezoneSet )
{
- // just call localtime() instead of figurin out whether this system
+ // just call localtime() instead of figuring out whether this system
// supports tzset(), _tzset() or something else
time_t t;
(void)localtime(&t);
}
// return the integral part of the JDN for the midnight of the given date (to
-// get the real JDN you need to add 0.5, this is, in fact, JDN of the noon of
-// the previous day)
+// get the real JDN you need to add 0.5, this is, in fact, JDN of the
+// noon of the previous day)
static long GetTruncatedJDN(wxDateTime::wxDateTime_t day,
wxDateTime::Month mon,
int year)
{
- // CREDIT: the algorithm was taken from Peter Baum's home page
-
- // the algorithm assumes Jan == 1
- int month = mon + 1;
-
- // we want the leap day (Feb 29) be at the end of the year, so we count
- // March as the first month
- if ( month < wxDateTime::Mar + 1 )
+ // CREDIT: code below is by Scott E. Lee (but bugs are mine)
+
+ // check the date validity
+ wxASSERT_MSG(
+ (year > JDN_0_YEAR) ||
+ ((year == JDN_0_YEAR) && (mon > JDN_0_MONTH)) ||
+ ((year == JDN_0_YEAR) && (mon == JDN_0_MONTH) && (day >= JDN_0_DAY)),
+ _T("date out of range - can't convert to JDN")
+ );
+
+ // make the year positive to avoid problems with negative numbers division
+ year += 4800;
+
+ // months are counted from March here
+ int month;
+ if ( mon >= wxDateTime::Mar )
{
- month += MONTHS_IN_YEAR;
- year--;
+ month = mon - 2;
}
-
- // this table contains the number of the days before the 1st of the each
- // month (in a non leap year) with the third value corresponding to March
- // (and the last one to February)
- static const int monthOffsets[14] =
+ else
{
- 0, 31, 61, 92, 122, 153, 184, 214, 245, 275, 306
- };
+ month = mon + 10;
+ year--;
+ }
- // and now add contributions of all terms together to get the result (you'd
- // better see the Web page for the description if you want to understand
- // why it works (if it does :-))
- return day +
- // linear approximation for months
- monthOffsets[month - (wxDateTime::Mar + 1)] +
- // the year contribution
- 365*year + year/4 - year/100 + year/400 +
- // 1721119.5 is the JDN of the midnight of Mar 1, year 0
- 1721118;
+ // now we can simply add all the contributions together
+ return ((year / 100) * DAYS_PER_400_YEARS) / 4
+ + ((year % 100) * DAYS_PER_4_YEARS) / 4
+ + (month * DAYS_PER_5_MONTHS + 2) / 5
+ + day
+ - JDN_OFFSET;
}
// this function is a wrapper around strftime(3)
// take some arbitrary Sunday
tm tm = { 0, 0, 0, 28, Nov, 99 };
- // and offset it by the number of days needed to get
+ // and offset it by the number of days needed to get the correct wday
tm.tm_mday += wday;
return CallStrftime(abbr ? _T("%a") : _T("%A"), &tm);
wxASSERT_MSG( IsValid(), _T("invalid wxDateTime") );
tm tm2(tm1);
+
+ // we want the time in GMT, mktime() takes the local time, so use timegm()
+ // if it's available
+#ifdef HAVE_TIMEGM
+ time_t timet = timegm(&tm2);
+#else // !HAVE_TIMEGM
+ // FIXME this almost surely doesn't work
+ tm2.tm_sec -= GetTimeZone();
+
time_t timet = mktime(&tm2);
+
+ if ( tm2.tm_isdst )
+ {
+ tm2.tm_hour += 1;
+
+ timet = mktime(&tm2);
+ }
+#endif // HAVE_TIMEGM/!HAVE_TIMEGM
+
if ( timet == (time_t)(-1) )
{
wxFAIL_MSG(_T("Invalid time"));
// get the current date from system
time_t timet = GetTimeNow();
- struct tm *tm = localtime(&timet);
+ struct tm *tm = gmtime(&timet);
// adjust the time
tm->tm_hour = hour;
ReplaceDefaultYearMonthWithCurrent(&year, &month);
- wxCHECK_MSG( day <= GetNumberOfDays(month, year), ms_InvDateTime,
+ wxCHECK_MSG( (0 < day) && (day <= GetNumberOfDays(month, year)),
+ ms_InvDateTime,
_T("Invalid date in wxDateTime::Set()") );
// the range of time_t type (inclusive)
tm.tm_hour = hour;
tm.tm_min = minute;
tm.tm_sec = second;
+ tm.tm_isdst = -1; // mktime() will guess it (wrongly, probably)
(void)Set(tm);
wxDateTime& wxDateTime::Set(double jdn)
{
- m_time = (jdn - 0.5 - EPOCH_JDN) * TIME_T_FACTOR;
+ // so that m_time will be 0 for the midnight of Jan 1, 1970 which is jdn
+ // EPOCH_JDN + 0.5
+ jdn -= EPOCH_JDN + 0.5;
+
+ m_time = jdn;
+ m_time *= MILLISECONDS_PER_DAY;
return *this;
}
if ( time != (time_t)-1 )
{
// use C RTL functions
- tm *tm = localtime(&time);
+ tm *tm = gmtime(&time);
// should never happen
- wxCHECK_MSG( tm, Tm(), _T("localtime() failed") );
+ wxCHECK_MSG( tm, Tm(), _T("gmtime() failed") );
return Tm(*tm);
}
else
{
- // CREDIT: the algorithm was taken from Peter Baum's home page
+ // remember the time and do the calculations with the date only - this
+ // eliminates rounding errors of the floating point arithmetics
- // calculate the Gregorian date from JDN for the midnight of our date
wxLongLong timeMidnight = m_time;
- long timeOnly = (m_time % MILLISECONDS_PER_DAY).GetLo();
+
+ long timeOnly = (m_time % MILLISECONDS_PER_DAY).ToLong();
+ if ( timeOnly < 0 )
+ {
+ timeOnly = MILLISECONDS_PER_DAY - timeOnly;
+ }
+
timeMidnight -= timeOnly;
- // TODO this probably could be optimised somehow...
-
- double jdn = (timeMidnight / MILLISECONDS_PER_DAY).GetLo();
- jdn += EPOCH_JDN + 0.5;
- long z = jdn - 1721118.5;
- double r = jdn - 1721118.5 - z;
- double g = z - 0.25;
- long a = g/36524.25; // number of days per year
- long b = a - a / 4;
- int year = (b + g) / 365.25;
- long c = b + z - 365.25*year;
- int month = (5*c + 456)/153;
- int day = c - (153*month - 457)/5 + (r < 0.5 ? 0 : 1);
- if ( month > 12 )
+ // calculate the Gregorian date from JDN for the midnight of our date:
+ // this will yield day, month (in 1..12 range) and year
+
+ // actually, this is the JDN for the noon of the previous day
+ long jdn = (timeMidnight / MILLISECONDS_PER_DAY).ToLong() + EPOCH_JDN;
+
+ // CREDIT: code below is by Scott E. Lee (but bugs are mine)
+
+ wxASSERT_MSG( jdn > -2, _T("JDN out of range") );
+
+ // calculate the century
+ int temp = (jdn + JDN_OFFSET) * 4 - 1;
+ int century = temp / DAYS_PER_400_YEARS;
+
+ // then the year and day of year (1 <= dayOfYear <= 366)
+ temp = ((temp % DAYS_PER_400_YEARS) / 4) * 4 + 3;
+ int year = (century * 100) + (temp / DAYS_PER_4_YEARS);
+ int dayOfYear = (temp % DAYS_PER_4_YEARS) / 4 + 1;
+
+ // and finally the month and day of the month
+ temp = dayOfYear * 5 - 3;
+ int month = temp / DAYS_PER_5_MONTHS;
+ int day = (temp % DAYS_PER_5_MONTHS) / 5 + 1;
+
+ // month is counted from March - convert to normal
+ if ( month < 10 )
{
- year++;
- month -= 12;
+ month += 3;
}
+ else
+ {
+ year += 1;
+ month -= 9;
+ }
+
+ // year is offset by 4800
+ year -= 4800;
+
+ // check that the algorithm gave us something reasonable
+ wxASSERT_MSG( (0 < month) && (month <= 12), _T("invalid month") );
+ wxASSERT_MSG( (1 <= day) && (day < 32), _T("invalid day") );
+ wxASSERT_MSG( (INT_MIN <= year) && (year <= INT_MAX),
+ _T("year range overflow") );
+ // construct Tm from these values
Tm tm;
- tm.year = year;
+ tm.year = (int)year;
tm.mon = (Month)(month - 1); // algorithm yields 1 for January, not 0
- tm.mday = day;
+ tm.mday = (wxDateTime_t)day;
tm.msec = timeOnly % 1000;
timeOnly -= tm.msec;
timeOnly /= 1000; // now we have time in seconds
if ( time != (time_t)-1 )
{
// use strftime()
- tm *tm = localtime(&time);
+ tm *tm = gmtime(&time);
// should never happen
- wxCHECK_MSG( tm, _T(""), _T("localtime() failed") );
+ wxCHECK_MSG( tm, _T(""), _T("gmtime() failed") );
return CallStrftime(format, tm);
}