Hi,
I am currently working on a .NET version of a Mac application which utilizes SQLite as the data store. CoreData saves dates in the SQLite db as unix timestamps with 1/1/2001 GMT as the epoch instead of 1/1/1970.
In this table I have a table like this:
CREATE TABLE ZTABLE ( Z_ENT INTEGER, Z_PK INTEGER PRIMARY KEY, Z_OPT INTEGER, ZTYPE INTEGER, ZACTIVE INTEGER, ZNOTE VARCHAR, ZDATE TIMESTAMP);
Any idea how I would proceed with reading the timestamp value from the ZDATE column?
EDIT: I solved this by modifying the provider to support a third datetimeformat. In case anyone is interested, here is the code I added/modified:
In SQLiteConvert.cs:
static
DateTime ConvertFromCoreDataTimestamp(string timestamp)
{
CultureInfo cultureInfo = new CultureInfo("en-US"); // Use the correct decimal delimiter ( . ) when parsing to/from string
DateTime origin = new DateTime(2001, 1, 1, 0, 0, 0, DateTimeKind.Utc);
return origin.AddSeconds(double.Parse(timestamp, cultureInfo)).ToLocalTime();
}static string ConvertToCoreDataTimestamp(DateTime date)
{
DateTime origin = new DateTime(2001, 1, 1, 0, 0, 0, DateTimeKind.Utc);
TimeSpan diff = date.ToUniversalTime() - origin;
double doubleFormat = diff.TotalSeconds;
CultureInfo cultureInfo = new CultureInfo("en-US"); // Use the correct decimal delimiter ( . ) when parsing to/from string
return doubleFormat.ToString(cultureInfo);
}
public
DateTime ToDateTime(string dateText)
{
switch (_datetimeFormat)
{
case SQLiteDateFormats.Ticks:
return new DateTime(Convert.ToInt64(dateText, CultureInfo.InvariantCulture));
case SQLiteDateFormats.CoreData:
return ConvertFromCoreDataTimestamp(dateText);
default:
return DateTime.ParseExact(dateText, _datetimeFormats, DateTimeFormatInfo.InvariantInfo, DateTimeStyles.None);
}
}
public
string ToString(DateTime dateValue)
{
switch (_datetimeFormat)
{
case SQLiteDateFormats.Ticks:
return dateValue.Ticks.ToString(CultureInfo.InvariantCulture);
case SQLiteDateFormats.CoreData:
return ConvertToCoreDataTimestamp(dateValue);
default:
return dateValue.ToString(_datetimeFormats[0], CultureInfo.InvariantCulture);
}
}
In SQLiteConnection.cs:
Changed:
SQLiteDateFormats dateFormat = String.Compare(FindKey(opts, "DateTimeFormat", "ISO8601"), "ticks", true, CultureInfo.InvariantCulture) == 0 ? SQLiteDateFormats.Ticks : SQLiteDateFormats.ISO8601;
To:
SQLiteDateFormats
dateFormat;
string dFormat = FindKey(opts, "DateTimeFormat", "ISO8601");
switch (dFormat)
{
case "ticks":
dateFormat = SQLiteDateFormats.Ticks;
break;
case "CoreData":
dateFormat = SQLiteDateFormats.CoreData;
break;
default:
dateFormat = SQLiteDateFormats.ISO8601;
break;
}