Index: System.Data.SQLite/SQLiteConvert.cs ================================================================== --- System.Data.SQLite/SQLiteConvert.cs +++ System.Data.SQLite/SQLiteConvert.cs @@ -74,10 +74,22 @@ "yyyy-MM-dd", "yyyyMMdd", "yy-MM-dd" }; + /// + /// The internal default format for UTC DateTime values when converting + /// to a string. + /// + private static readonly string _datetimeFormatUtc = _datetimeFormats[5]; + + /// + /// The internal default format for local DateTime values when converting + /// to a string. + /// + private static readonly string _datetimeFormatLocal = _datetimeFormats[19]; + /// /// An UTF-8 Encoding instance, so we can convert strings to and from UTF-8 /// private static Encoding _utf8 = new UTF8Encoding(); /// @@ -347,10 +359,23 @@ /// The JulianDay value the Datetime represents public static double ToJulianDay(DateTime value) { return value.ToOADate() + OleAutomationEpochAsJulianDay; } + + /// + /// Returns the default DateTime format string to use for the specified + /// DateTimeKind. + /// + /// The DateTimeKind to use. + /// + /// The default DateTime format string to use for the specified DateTimeKind. + /// + private static string GetDateTimeKindFormat(DateTimeKind kind) + { + return (kind == DateTimeKind.Utc) ? _datetimeFormatUtc : _datetimeFormatLocal; + } /// /// Converts a DateTime to a string value, using the current DateTimeFormat specified for the connection when it was opened. /// /// The DateTime value to convert @@ -357,27 +382,28 @@ /// Either a string containing the long integer number of 100-nanosecond units since System.DateTime.MinValue, a /// Julian day double, an integer number of seconds since the Unix epoch, a culture-independent formatted date and time /// string, a formatted date and time string in the current culture, or an ISO8601-format date/time string. public string ToString(DateTime dateValue) { - switch (_datetimeFormat) - { - case SQLiteDateFormats.Ticks: - return dateValue.Ticks.ToString(CultureInfo.InvariantCulture); - case SQLiteDateFormats.JulianDay: - return ToJulianDay(dateValue).ToString(CultureInfo.InvariantCulture); - case SQLiteDateFormats.UnixEpoch: - return ((long)(dateValue.Subtract(UnixEpoch).Ticks / TimeSpan.TicksPerSecond)).ToString(); - case SQLiteDateFormats.InvariantCulture: - return dateValue.ToString(FullFormat, CultureInfo.InvariantCulture); - case SQLiteDateFormats.CurrentCulture: - return dateValue.ToString(FullFormat, CultureInfo.CurrentCulture); - default: - return (dateValue.Kind == DateTimeKind.Utc) ? - dateValue.ToString(_datetimeFormats[5], CultureInfo.InvariantCulture) : // include "Z" - dateValue.ToString(_datetimeFormats[19], CultureInfo.InvariantCulture); - } + switch (_datetimeFormat) + { + case SQLiteDateFormats.Ticks: + return dateValue.Ticks.ToString(CultureInfo.InvariantCulture); + case SQLiteDateFormats.JulianDay: + return ToJulianDay(dateValue).ToString(CultureInfo.InvariantCulture); + case SQLiteDateFormats.UnixEpoch: + return ((long)(dateValue.Subtract(UnixEpoch).Ticks / TimeSpan.TicksPerSecond)).ToString(); + case SQLiteDateFormats.InvariantCulture: + return dateValue.ToString(FullFormat, CultureInfo.InvariantCulture); + case SQLiteDateFormats.CurrentCulture: + return dateValue.ToString(FullFormat, CultureInfo.CurrentCulture); + default: + return (dateValue.Kind == DateTimeKind.Unspecified) ? + DateTime.SpecifyKind(dateValue, _datetimeKind).ToString( + GetDateTimeKindFormat(_datetimeKind), CultureInfo.InvariantCulture) : + dateValue.ToString(GetDateTimeKindFormat(dateValue.Kind), CultureInfo.InvariantCulture); + } } /// /// Internal function to convert a UTF-8 encoded IntPtr of the specified length to a DateTime. ///