What is an XDate struct?

From ZedGraphWiki

Jump to: navigation, search

XDate is a struct used in ZedGraph to handle date conversions to and from a variety of formats, and also to store a date value. XDate stores the date internally as a double value, so it requires only 8 bytes for storage. The double value represents the number of days since December 30, 1899 at 0:00 hours. Note that this date is the same as an OLE Automation date (and almost the same as the Microsoft Excel date format) -- see the DateTime.ToOADate() method.

Contents

Valid Range

The XDate has a valid range from January 1st, 4713 B.C. to December 31st, 9999. Note that these dates are extrapolations of the Gregorian calendar, that is, the Gregorian calendar is really only valid from October 15, 1582 forward. The countries that adopted the Gregorian calendar first did so on October 4, 1582, so that the next day was October 15, 1582. Prior to that time the Julian Calendar was used. However, Prior to March 1, 4 AD the treatment of leap years was inconsistent, and prior to 45 BC the Julian Calendar did not exist. The XDate struct projects only Gregorian dates backwards and does not deal with Julian calendar dates at all. For dates prior to year 1, the XDate.ToString method will just append a "(BC)" notation to the end of any date.

Type Casting

XDate includes implicit conversion to and from the double type which allows you to set an XDate to a double or to set a double to an XDate without an explicit cast (this works in C# anyway). For example:

XDate myXDate = new XDate( 2006, 3, 16 );
// Implicit conversion from XDate to double here
Double myDouble = myXDate;
// Implicit conversion from double to XDate here
XDate anotherXDate = myDouble;

Alternatively, you can use an explicit conversion (this works for Visual Basic):

Double myDouble = myXDate.XLDate;
XDate anotherXDate = new XDate( myDouble );

Format Conversion

The XDate struct includes conversion functions to and from:

  • Gregorian calendar date
  • Julian day number
  • OLE Automation date
  • DateTime format
  • Decimal year format


ToString conversion XDate supports conversion to a wide variety of date string formats -- this is implemented via the DateTime.ToString() method, using the DateTimeFormatInfo class.

A Note About Excel Dates

The Excel date format is very similar to the XDate, except that Excel errantly assumes that the calendar year 1900 is a leap year (it's not). Therefore, prior to March 1st, 1900 (XDate values below 60), MS Excel dates are off by 1 day. The OLE Automation date format does not share this limitation.