System.Data.SQLite

Check-in [c559792dba]
Login

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

Overview
Comment:Improve handling of out-of-range Julian Day values.
Downloads: Tarball | ZIP archive
Timelines: family | ancestors | descendants | both | newJulianDay
Files: files | file ages | folders
SHA1: c559792dba4cff18e8af99127eb0a12e57c02b72
User & Date: mistachkin 2014-10-31 22:40:47.088
Context
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
21:36
Add experimental port of the Julian Day handling functions from the SQLite core library. Still needs tests. Pursuant to [3e783eecbe]. check-in: fe17cded54 user: mistachkin tags: newJulianDay
Changes
Unified Diff Ignore Whitespace Patch
Changes to System.Data.SQLite/SQLiteConvert.cs.
47
48
49
50
51
52
53












54
55
56
57
58
59
60
    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>
    /// An array of ISO-8601 DateTime formats that we support parsing.
    /// </summary>
    private static string[] _datetimeFormats = new string[] {
      "THHmmssK",
      "THHmmK",
      "HH:mm:ss.FFFFFFFK",







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







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
    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",
      "HH:mm:ss.FFFFFFFK",
204
205
206
207
208
209
210


















211
212
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
251
252
253
254




255
256
257
258
259
260
261

262
263



264
265
266
267
268
269
270


    #endregion

    #region DateTime Conversion Functions
    #region New Julian Day Conversion Methods
    /// <summary>


















    /// Converts a Julian Day number 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 number to convert.
    /// </param>




    /// <returns>
    /// A <see cref="DateTime" /> value that contains the year, month, and day
    /// values that are closest to the specified Julian Day value.
    /// </returns>
    private static DateTime computeYMD(
        long jd

        )
    {



        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);
        B = A + 1524;
        C = (int)((B - 122.1) / 365.25);
        D = (36525 * C) / 100;
        E = (int)((B - D) / 30.6001);
        X1 = (int)(30.6001 * E);

        int day, month, year;

        day = B - D - X1;
        month = E < 14 ? E - 1 : E - 13;
        year = month > 2 ? C - 4716 : C - 4715;



        return new DateTime(year, month, day);





    }

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

    /// <summary>
    /// Converts a Julian Day number 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 number to convert.




    /// </param>
    /// <returns>
    /// A <see cref="DateTime" /> value that contains the hour, minute, and
    /// second values that are closest to the specified Julian Day value.
    /// </returns>
    private static DateTime computeHMS(
        long jd

        )
    {



        int s;

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

        double seconds;

        seconds = s / 1000.0;







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|




|

>
>
>
>

|
|


|
>


>
>
>

















>
>
|
>
>
>
>
>





|




|
>
>
>
>






|
>


>
>
>







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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323


    #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.
    /// </param>
    /// <returns>
    /// A <see cref="DateTime" /> value that contains the year, month, and
    /// 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);
        B = A + 1524;
        C = (int)((B - 122.1) / 365.25);
        D = (36525 * C) / 100;
        E = (int)((B - D) / 30.6001);
        X1 = (int)(30.6001 * E);

        int day, month, year;

        day = B - D - X1;
        month = E < 14 ? E - 1 : E - 13;
        year = month > 2 ? C - 4716 : C - 4715;

        try
        {
            return new DateTime(year, month, day);
        }
        catch
        {
            return badValue;
        }
    }

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

    /// <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.
    /// </param>
    /// <returns>
    /// A <see cref="DateTime" /> value that contains the hour, minute, and
    /// 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;

        seconds = s / 1000.0;
280
281
282
283
284
285
286


287
288
289
290
291





292
293
294
295
296
297
298
299




300
301
302




303
304
305
306
307
308
309
310
311
312

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

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



        DateTime minValue = DateTime.MinValue;

        return new DateTime(
            minValue.Year, minValue.Month, minValue.Day,
            hour, minute, second, millisecond);





    }

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

    /// <summary>
    /// Converts a <see cref="DateTime" /> to a Julian Day number.
    /// This function was copied from the "date.c" file belonging
    /// to the SQLite core library.




    /// </summary>
    /// <param name="dateTime">
    /// The <see cref="DateTime" /> value to convert.




    /// </param>
    /// <returns>
    /// The nearest Julian Day number corresponding to the specified
    /// <see cref="DateTime" /> value.
    /// </returns>
    private static long computeJD(
        DateTime dateTime
        )
    {
        int Y, M, D;







>
>
|

|
|
|
>
>
>
>
>





|

|
>
>
>
>


|
>
>
>
>


|







333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380

        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(
                minValue.Year, minValue.Month, minValue.Day,
                hour, minute, second, millisecond);
        }
        catch
        {
            return badValue;
        }
    }

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

    /// <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).
    /// </param>
    /// <returns>
    /// The nearest Julian Day value corresponding to the specified
    /// <see cref="DateTime" /> value.
    /// </returns>
    private static long computeJD(
        DateTime dateTime
        )
    {
        int Y, M, D;