System.Data.SQLite
Check-in [36b4f89052]
Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Completely remove use of DateTime.FromOADate and DateTime.ToOADate methods. Improve comments. Now passes existing tests. More tests are still necessary.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | newJulianDay
Files: files | file ages | folders
SHA1: 36b4f89052e7276ea0a2699f236bcda81576adbb
User & Date: mistachkin 2014-10-31 23:36:10
Context
2014-11-01
00:27
Add more tests. check-in: 5b924895c0 user: mistachkin tags: newJulianDay
2014-10-31
23:36
Completely remove use of DateTime.FromOADate and DateTime.ToOADate methods. Improve comments. Now passes existing tests. More tests are still necessary. check-in: 36b4f89052 user: mistachkin tags: newJulianDay
22:40
Improve handling of out-of-range Julian Day values. check-in: c559792dba user: mistachkin tags: newJulianDay
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to System.Data.SQLite/SQLiteConvert.cs.

37
38
39
40
41
42
43

44
45

46
47

48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
...
213
214
215
216
217
218
219


220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238








































239
240
241
242
243
244
245
246
247
248
249
250
...
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
...
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
...
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
...
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
...
351
352
353
354
355
356
357
358
359
360
361

362
363
364
365
366
367
368
369
370
...
607
608
609
610
611
612
613
614



615
616
617








618
619
620
621
622
623
624
...
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670

    /// <summary>
    /// The value for the Unix epoch (e.g. January 1, 1970 at midnight, in UTC).
    /// </summary>
    protected static readonly DateTime UnixEpoch =
        new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);


    /// <summary>
    /// The value of the OLE Automation epoch represented as a Julian day.

    /// </summary>
    private static readonly double OleAutomationEpochAsJulianDay = 2415018.5;


    /// <summary>
    /// The format string for DateTime values when using the InvariantCulture or CurrentCulture formats.
    /// </summary>
    private const string FullFormat = "yyyy-MM-ddTHH:mm:ss.fffffffK";

    /// <summary>
    /// This is the minimum Julian Day value supported by this library
    /// (148731163200000).
    /// </summary>
    private static readonly long MinimumJD = computeJD(DateTime.MinValue);

    /// <summary>
    /// This is the maximum Julian Day value supported by this library
    /// (464269060799000).
    /// </summary>
    private static readonly long MaximumJD = computeJD(DateTime.MaxValue);

    /// <summary>
    /// An array of ISO-8601 DateTime formats that we support parsing.
    /// </summary>
    private static string[] _datetimeFormats = new string[] {
      "THHmmssK",
      "THHmmK",
................................................................................

      return _utf8.GetString(byteArray, 0, nativestringlen);
    }


    #endregion



    #region DateTime Conversion Functions
    #region New Julian Day Conversion Methods
    /// <summary>
    /// Checks if the specified <see cref="Int64" /> is within the
    /// supported range for a Julian Day value.
    /// </summary>
    /// <param name="jd">
    /// The Julian Day value to check.
    /// </param>
    /// <returns>
    /// Non-zero if the specified Julian Day value is in the supported
    /// range; otherwise, zero.
    /// </returns>
    private static bool isValidJD(
        long jd
        )
    {
        return ((jd >= MinimumJD) && (jd <= MaximumJD));
    }









































    /// <summary>
    /// Converts a Julian Day value to a <see cref="DateTime" />.
    /// This function was copied from the "date.c" file belonging
    /// to the SQLite core library.
    /// </summary>
    /// <param name="jd">
    /// The Julian Day value to convert.
    /// </param>
    /// <param name="badValue">
    /// The <see cref="DateTime" /> value to return in the event that the
    /// Julian Day is out of the supported range.
................................................................................
    /// day values that are closest to the specified Julian Day value.
    /// </returns>
    private static DateTime computeYMD(
        long jd,
        DateTime badValue
        )
    {
        if (!isValidJD(jd))
            return badValue;

        int Z, A, B, C, D, E, X1;

        Z = (int)((jd + 43200000) / 86400000);
        A = (int)((Z - 1867216.25) / 36524.25);
        A = Z + 1 + A - (A / 4);
................................................................................
        }
    }

    ///////////////////////////////////////////////////////////////////////////

    /// <summary>
    /// Converts a Julian Day value to a <see cref="DateTime" />.
    /// This function was copied from the "date.c" file belonging
    /// to the SQLite core library.
    /// </summary>
    /// <param name="jd">
    /// The Julian Day value to convert.
    /// </param>
    /// <param name="badValue">
    /// The <see cref="DateTime" /> value to return in the event that the
    /// Julian Day value is out of the supported range.
................................................................................
    /// second values that are closest to the specified Julian Day value.
    /// </returns>
    private static DateTime computeHMS(
        long jd,
        DateTime badValue
        )
    {
        if (!isValidJD(jd))
            return badValue;

        int s;

        s = (int)((jd + 43200000) % 86400000);

        double seconds;
................................................................................
        s -= hour * 3600;

        int minute;

        minute = s / 60;
        seconds += s - minute * 60;

        int second = (int)Math.Truncate(seconds);
        int millisecond = (int)((seconds - second) * 1000.0);

        try
        {
            DateTime minValue = DateTime.MinValue;

            return new DateTime(
................................................................................
        }
    }

    ///////////////////////////////////////////////////////////////////////////

    /// <summary>
    /// Converts a <see cref="DateTime" /> to a Julian Day value.
    /// This function was copied from the "date.c" file belonging
    /// to the SQLite core library.  Since the range of Julian Day
    /// values supported by this method includes all possible valid
    /// values of a <see cref="DateTime" /> value, it should be

    /// impossible for this method to raise an exception or return
    /// an undefined result.
    /// </summary>
    /// <param name="dateTime">
    /// The <see cref="DateTime" /> value to convert.  This value
    /// will be within the range of <see cref="DateTime.MinValue" />
    /// (00:00:00.0000000, January 1, 0001) to
    /// <see cref="DateTime.MaxValue" /> (23:59:59.9999999, December
    /// 31, 9999).
................................................................................

    /// <summary>
    /// Converts a julianday value into a DateTime
    /// </summary>
    /// <param name="julianDay">The value to convert</param>
    /// <param name="kind">The DateTimeKind to use.</param>
    /// <returns>A .NET DateTime</returns>
    public static DateTime ToDateTime(double julianDay, DateTimeKind kind)



    {
        return DateTime.SpecifyKind(
            DateTime.FromOADate(julianDay - OleAutomationEpochAsJulianDay), kind);








    }

    /// <summary>
    /// Converts the specified number of seconds from the Unix epoch into a
    /// <see cref="DateTime" /> value.
    /// </summary>
    /// <param name="seconds">
................................................................................
    /// <summary>
    /// Converts a DateTime struct to a JulianDay double
    /// </summary>
    /// <param name="value">The DateTime to convert</param>
    /// <returns>The JulianDay value the Datetime represents</returns>
    public static double ToJulianDay(DateTime value)
    {
      return value.ToOADate() + OleAutomationEpochAsJulianDay;
    }

    /// <summary>
    /// Converts a DateTime struct to the whole number of seconds since the
    /// Unix epoch.
    /// </summary>
    /// <param name="value">The DateTime to convert</param>







>

|
>


>










|





|







 







>
>













|



|

>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>



|
|







 







|







 







|
|







 







|







 







|







 







|
|
|
|
>
|
<







 







|
>
>
>

<
<
>
>
>
>
>
>
>
>







 







|







37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
...
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
...
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
...
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
...
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
...
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
...
396
397
398
399
400
401
402
403
404
405
406
407
408

409
410
411
412
413
414
415
...
652
653
654
655
656
657
658
659
660
661
662
663


664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
...
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724

    /// <summary>
    /// The value for the Unix epoch (e.g. January 1, 1970 at midnight, in UTC).
    /// </summary>
    protected static readonly DateTime UnixEpoch =
        new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);

    #pragma warning disable 414
    /// <summary>
    /// The value of the OLE Automation epoch represented as a Julian day.  This
    /// field cannot be removed as the test suite relies upon it.
    /// </summary>
    private static readonly double OleAutomationEpochAsJulianDay = 2415018.5;
    #pragma warning restore 414

    /// <summary>
    /// The format string for DateTime values when using the InvariantCulture or CurrentCulture formats.
    /// </summary>
    private const string FullFormat = "yyyy-MM-ddTHH:mm:ss.fffffffK";

    /// <summary>
    /// This is the minimum Julian Day value supported by this library
    /// (148731163200000).
    /// </summary>
    private static readonly long MinimumJd = computeJD(DateTime.MinValue);

    /// <summary>
    /// This is the maximum Julian Day value supported by this library
    /// (464269060799000).
    /// </summary>
    private static readonly long MaximumJd = computeJD(DateTime.MaxValue);

    /// <summary>
    /// An array of ISO-8601 DateTime formats that we support parsing.
    /// </summary>
    private static string[] _datetimeFormats = new string[] {
      "THHmmssK",
      "THHmmK",
................................................................................

      return _utf8.GetString(byteArray, 0, nativestringlen);
    }


    #endregion

    ///////////////////////////////////////////////////////////////////////////

    #region DateTime Conversion Functions
    #region New Julian Day Conversion Methods
    /// <summary>
    /// Checks if the specified <see cref="Int64" /> is within the
    /// supported range for a Julian Day value.
    /// </summary>
    /// <param name="jd">
    /// The Julian Day value to check.
    /// </param>
    /// <returns>
    /// Non-zero if the specified Julian Day value is in the supported
    /// range; otherwise, zero.
    /// </returns>
    private static bool isValidJd(
        long jd
        )
    {
        return ((jd >= MinimumJd) && (jd <= MaximumJd));
    }

    ///////////////////////////////////////////////////////////////////////////

    /// <summary>
    /// Converts a Julian Day value from a <see cref="Double" /> to an
    /// <see cref="Int64" />.
    /// </summary>
    /// <param name="julianDay">
    /// The Julian Day <see cref="Double" /> value to convert.
    /// </param>
    /// <returns>
    /// The resulting Julian Day <see cref="Int64" /> value.
    /// </returns>
    private static long DoubleToJd(
        double julianDay
        )
    {
        return (long)(julianDay * 86400000.0);
    }

    ///////////////////////////////////////////////////////////////////////////

    /// <summary>
    /// Converts a Julian Day value from an <see cref="Int64" /> to a
    /// <see cref="Double" />.
    /// </summary>
    /// <param name="jd">
    /// The Julian Day <see cref="Int64" /> value to convert.
    /// </param>
    /// <returns>
    /// The resulting Julian Day <see cref="Double" /> value.
    /// </returns>
    private static double JdToDouble(
        long jd
        )
    {
        return (double)(jd / 86400000.0);
    }

    ///////////////////////////////////////////////////////////////////////////

    /// <summary>
    /// Converts a Julian Day value to a <see cref="DateTime" />.
    /// This method was translated from the "computeYMD" function in the
    /// "date.c" file belonging to the SQLite core library.
    /// </summary>
    /// <param name="jd">
    /// The Julian Day value to convert.
    /// </param>
    /// <param name="badValue">
    /// The <see cref="DateTime" /> value to return in the event that the
    /// Julian Day is out of the supported range.
................................................................................
    /// day values that are closest to the specified Julian Day value.
    /// </returns>
    private static DateTime computeYMD(
        long jd,
        DateTime badValue
        )
    {
        if (!isValidJd(jd))
            return badValue;

        int Z, A, B, C, D, E, X1;

        Z = (int)((jd + 43200000) / 86400000);
        A = (int)((Z - 1867216.25) / 36524.25);
        A = Z + 1 + A - (A / 4);
................................................................................
        }
    }

    ///////////////////////////////////////////////////////////////////////////

    /// <summary>
    /// Converts a Julian Day value to a <see cref="DateTime" />.
    /// This method was translated from the "computeHMS" function in the
    /// "date.c" file belonging to the SQLite core library.
    /// </summary>
    /// <param name="jd">
    /// The Julian Day value to convert.
    /// </param>
    /// <param name="badValue">
    /// The <see cref="DateTime" /> value to return in the event that the
    /// Julian Day value is out of the supported range.
................................................................................
    /// second values that are closest to the specified Julian Day value.
    /// </returns>
    private static DateTime computeHMS(
        long jd,
        DateTime badValue
        )
    {
        if (!isValidJd(jd))
            return badValue;

        int s;

        s = (int)((jd + 43200000) % 86400000);

        double seconds;
................................................................................
        s -= hour * 3600;

        int minute;

        minute = s / 60;
        seconds += s - minute * 60;

        int second = (int)seconds;
        int millisecond = (int)((seconds - second) * 1000.0);

        try
        {
            DateTime minValue = DateTime.MinValue;

            return new DateTime(
................................................................................
        }
    }

    ///////////////////////////////////////////////////////////////////////////

    /// <summary>
    /// Converts a <see cref="DateTime" /> to a Julian Day value.
    /// This method was translated from the "computeJD" function in
    /// the "date.c" file belonging to the SQLite core library.
    /// Since the range of Julian Day values supported by this method
    /// includes all possible (valid) values of a <see cref="DateTime" />
    /// value, it should be extremely difficult for this method to
    /// raise an exception or return an undefined result.

    /// </summary>
    /// <param name="dateTime">
    /// The <see cref="DateTime" /> value to convert.  This value
    /// will be within the range of <see cref="DateTime.MinValue" />
    /// (00:00:00.0000000, January 1, 0001) to
    /// <see cref="DateTime.MaxValue" /> (23:59:59.9999999, December
    /// 31, 9999).
................................................................................

    /// <summary>
    /// Converts a julianday value into a DateTime
    /// </summary>
    /// <param name="julianDay">The value to convert</param>
    /// <param name="kind">The DateTimeKind to use.</param>
    /// <returns>A .NET DateTime</returns>
    public static DateTime ToDateTime(
        double julianDay,
        DateTimeKind kind
        )
    {


        long jd = DoubleToJd(julianDay);
        DateTime dateTimeYMD = computeYMD(jd, DateTime.MinValue);
        DateTime dateTimeHMS = computeHMS(jd, DateTime.MinValue);

        return new DateTime(
            dateTimeYMD.Year, dateTimeYMD.Month, dateTimeYMD.Day,
            dateTimeHMS.Hour, dateTimeHMS.Minute, dateTimeHMS.Second,
            dateTimeHMS.Millisecond, kind);
    }

    /// <summary>
    /// Converts the specified number of seconds from the Unix epoch into a
    /// <see cref="DateTime" /> value.
    /// </summary>
    /// <param name="seconds">
................................................................................
    /// <summary>
    /// Converts a DateTime struct to a JulianDay double
    /// </summary>
    /// <param name="value">The DateTime to convert</param>
    /// <returns>The JulianDay value the Datetime represents</returns>
    public static double ToJulianDay(DateTime value)
    {
        return JdToDouble(computeJD(value));
    }

    /// <summary>
    /// Converts a DateTime struct to the whole number of seconds since the
    /// Unix epoch.
    /// </summary>
    /// <param name="value">The DateTime to convert</param>