System.Data.SQLite
Check-in [dbdf2561a4]
Not logged in

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

Overview
Comment:When converting a DateTime instance of an 'Unspecified' kind to a string, use the same kind as the connection, if available. Also, cleanup handling of the default formats for UTC and local time.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | logBind
Files: files | file ages | folders
SHA1: dbdf2561a44fabd61459f0d656decf62a4bea5d6
User & Date: mistachkin 2012-02-19 13:22:08
Context
2012-02-19
13:31
Merge fix for ticket [8a426d12eb] along with logging and DateTime handling enhancements to trunk. check-in: c3bb6c2582 user: mistachkin tags: trunk
13:22
When converting a DateTime instance of an 'Unspecified' kind to a string, use the same kind as the connection, if available. Also, cleanup handling of the default formats for UTC and local time. Closed-Leaf check-in: dbdf2561a4 user: mistachkin tags: logBind
08:34
Refactor previous commit to remove the new delegates as they are not strictly required. Also, remove superfluous newData local variable in the StepCallback method. check-in: c43fca0ae6 user: mistachkin tags: logBind
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

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

    72     72         "yyyyMMddHHmm",
    73     73         "yyyyMMddTHHmmssFFFFFFF",
    74     74         "yyyy-MM-dd",
    75     75         "yyyyMMdd",
    76     76         "yy-MM-dd"
    77     77       };
    78     78   
           79  +    /// <summary>
           80  +    /// The internal default format for UTC DateTime values when converting
           81  +    /// to a string.
           82  +    /// </summary>
           83  +    private static readonly string _datetimeFormatUtc = _datetimeFormats[5];
           84  +
           85  +    /// <summary>
           86  +    /// The internal default format for local DateTime values when converting
           87  +    /// to a string.
           88  +    /// </summary>
           89  +    private static readonly string _datetimeFormatLocal = _datetimeFormats[19];
           90  +
    79     91       /// <summary>
    80     92       /// An UTF-8 Encoding instance, so we can convert strings to and from UTF-8
    81     93       /// </summary>
    82     94       private static Encoding _utf8 = new UTF8Encoding();
    83     95       /// <summary>
    84     96       /// The default DateTime format for this instance
    85     97       /// </summary>
................................................................................
   345    357       /// </summary>
   346    358       /// <param name="value">The DateTime to convert</param>
   347    359       /// <returns>The JulianDay value the Datetime represents</returns>
   348    360       public static double ToJulianDay(DateTime value)
   349    361       {
   350    362         return value.ToOADate() + OleAutomationEpochAsJulianDay;
   351    363       }
          364  +
          365  +    /// <summary>
          366  +    /// Returns the default DateTime format string to use for the specified
          367  +    /// DateTimeKind.
          368  +    /// </summary>
          369  +    /// <param name="kind">The DateTimeKind to use.</param>
          370  +    /// <returns>
          371  +    /// The default DateTime format string to use for the specified DateTimeKind.
          372  +    /// </returns>
          373  +    private static string GetDateTimeKindFormat(DateTimeKind kind)
          374  +    {
          375  +        return (kind == DateTimeKind.Utc) ? _datetimeFormatUtc : _datetimeFormatLocal;
          376  +    }
   352    377   
   353    378       /// <summary>
   354    379       /// Converts a DateTime to a string value, using the current DateTimeFormat specified for the connection when it was opened.
   355    380       /// </summary>
   356    381       /// <param name="dateValue">The DateTime value to convert</param>
   357    382       /// <returns>Either a string containing the long integer number of 100-nanosecond units since System.DateTime.MinValue, a
   358    383       /// Julian day double, an integer number of seconds since the Unix epoch, a culture-independent formatted date and time
   359    384       /// string, a formatted date and time string in the current culture, or an ISO8601-format date/time string.</returns>
   360    385       public string ToString(DateTime dateValue)
   361    386       {
   362         -      switch (_datetimeFormat)
   363         -      {
   364         -        case SQLiteDateFormats.Ticks:
   365         -          return dateValue.Ticks.ToString(CultureInfo.InvariantCulture);
   366         -        case SQLiteDateFormats.JulianDay:
   367         -          return ToJulianDay(dateValue).ToString(CultureInfo.InvariantCulture);
   368         -        case SQLiteDateFormats.UnixEpoch:
   369         -          return ((long)(dateValue.Subtract(UnixEpoch).Ticks / TimeSpan.TicksPerSecond)).ToString();
   370         -        case SQLiteDateFormats.InvariantCulture:
   371         -          return dateValue.ToString(FullFormat, CultureInfo.InvariantCulture);
   372         -        case SQLiteDateFormats.CurrentCulture:
   373         -          return dateValue.ToString(FullFormat, CultureInfo.CurrentCulture);
   374         -        default:
   375         -          return (dateValue.Kind == DateTimeKind.Utc) ?
   376         -              dateValue.ToString(_datetimeFormats[5], CultureInfo.InvariantCulture) : // include "Z"
   377         -              dateValue.ToString(_datetimeFormats[19], CultureInfo.InvariantCulture);
   378         -      }
          387  +        switch (_datetimeFormat)
          388  +        {
          389  +            case SQLiteDateFormats.Ticks:
          390  +                return dateValue.Ticks.ToString(CultureInfo.InvariantCulture);
          391  +            case SQLiteDateFormats.JulianDay:
          392  +                return ToJulianDay(dateValue).ToString(CultureInfo.InvariantCulture);
          393  +            case SQLiteDateFormats.UnixEpoch:
          394  +                return ((long)(dateValue.Subtract(UnixEpoch).Ticks / TimeSpan.TicksPerSecond)).ToString();
          395  +            case SQLiteDateFormats.InvariantCulture:
          396  +                return dateValue.ToString(FullFormat, CultureInfo.InvariantCulture);
          397  +            case SQLiteDateFormats.CurrentCulture:
          398  +                return dateValue.ToString(FullFormat, CultureInfo.CurrentCulture);
          399  +            default:
          400  +                return (dateValue.Kind == DateTimeKind.Unspecified) ?
          401  +                    DateTime.SpecifyKind(dateValue, _datetimeKind).ToString(
          402  +                        GetDateTimeKindFormat(_datetimeKind), CultureInfo.InvariantCulture) :
          403  +                    dateValue.ToString(GetDateTimeKindFormat(dateValue.Kind), CultureInfo.InvariantCulture);
          404  +        }
   379    405       }
   380    406   
   381    407       /// <summary>
   382    408       /// Internal function to convert a UTF-8 encoded IntPtr of the specified length to a DateTime.
   383    409       /// </summary>
   384    410       /// <remarks>
   385    411       /// This is a convenience function, which first calls ToString() on the IntPtr to convert it to a string, then calls