c# - Why does strftime() not work in Sqlite when dates are stored as ticks? -
i have following code:
[test] public void sqlitetest() { var insertquery = @"insert metricdata (date) values (@date)"; var selectquery = @"select strftime('%y',date) 'year', date metricdata"; var connectionstring = "data source=:memory:;version=3;datetimeformat=ticks;"; using (var db = new sqliteconnection(connectionstring)) { db.open(); databaseschemabuilder.initializeschema(db); var testdate = datetime.now; using (var cmd = new sqlitecommand(insertquery, db)) { cmd.parameters.addwithvalue("@date", testdate); cmd.executenonquery(); } using (var cmd = new sqlitecommand(selectquery, db)) { using (var reader = cmd.executereader()) { reader.read(); var querieddate = reader.getdatetime(1); var year = reader.getstring(0); assert.areequal(testdate, querieddate); assert.areequal("2013", year); } } } }
when run code, first assertion passes second 1 fails, year equalling 1968 (other format values incorrect, month comes 19 example).
when looking @ raw query output, year result 1968
, date 635120225608999130
.
why strftime()
not working in instance?
edit: add, date column have defined date integer
. also, if not include datetimeformat=ticks
in connection string assertions pass.
date
integer
in sqlite
uses unix time
allows oldest date of 1970-01-01 00:00:00 utc
. 1968 year value error , not pass assert.areequal("2013",year)
.
see documentation
and removing datetimeformat=ticks;
in connection string means going use default iso8601
allow date prior 1970-01-01
, reason assert passes.
see reference
Comments
Post a Comment