Skip to content

Add support of negetive OLE Automation Date values - #807

Merged
swharden merged 2 commits into
ScottPlot:masterfrom
StendProg:OLEDateFix
Feb 20, 2021
Merged

swharden merged 2 commits into
ScottPlot:masterfrom
StendProg:OLEDateFix

Conversation

@StendProg

Copy link
Copy Markdown
Contributor

Purpose:
OLE Automation date support negative values, and it's minimum value is midnight, 1 January 0100.##806
This PR allow to use negative values and expand avalable range.

New Functionality:
image

@swharden
swharden merged commit 783a3ee into ScottPlot:master Feb 20, 2021
@audun

audun commented Mar 17, 2021

Copy link
Copy Markdown

Note that this doesn't work correctly for the date 1899-12-30. OLE Automation dates have surprising conversions between -1.0 and 1.0:

DateTime.FromOADate(-1.0).ToString("s") "1899-12-29T00:00:00"
DateTime.FromOADate(-0.9).ToString("s") "1899-12-30T21:36:00"
DateTime.FromOADate( 0.9).ToString("s") "1899-12-30T21:36:00"
DateTime.FromOADate( 1.0).ToString("s") "1899-12-31T00:00:00"

@StendProg

Copy link
Copy Markdown
Contributor Author

Hi @audun,
Thanks for the fun fact, really as you wrote.
But I think it's worth leaving everything as it is, it is unlikely that many will honor 30 December 1899 with an attentive look.

@audun

audun commented Mar 20, 2021

Copy link
Copy Markdown

True, and it works okayish.

image

However, it appears that there are more issues with negative OADates. Note that the two first time ticks above are reversed. It becomes very obvious if you change the date generation in the TicksDateTime recipe to show a few data points per date in a monotonously increasing fashion, like: myDates[i] = new DateTime(1899, 12, 20, 1, 0, 0).AddDays(i / 4.0);

image

If I change it to use the "Excel" conversion, which is very close to OADate, it looks like what you'd expect, and is much easier to reason about.

image

        // From OxyPlot.Axes.DateTimeAxis
        private static readonly DateTime TimeOrigin = new DateTime(1899, 12, 31, 0, 0, 0, DateTimeKind.Utc);
        public static double ToDouble(DateTime value)
        {
            var span = value - TimeOrigin;
            return span.TotalDays + 1;
        }

        public void ExecuteRecipe(Plot plt)
        {
            // create data sample data
            DateTime[] myDates = new DateTime[100];
            for (int i = 0; i < myDates.Length; i++) 
                myDates[i] = new DateTime(1899, 12, 20, 1, 0, 0).AddDays(i / 4.0);

            // Convert DateTime[] to double[] before plotting
            //double[] xs = myDates.Select(x => x.ToOADate()).ToArray();
            double[] xs = myDates.Select(x => ToDouble(x)).ToArray();
            double[] ys = DataGen.RandomWalk(myDates.Length);
            plt.AddScatter(xs, ys);

            // Then tell the axis to display tick labels using a time format
            plt.XAxis.DateTimeFormat(true);
        }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants