使用sqlite進行時間的插入的時候,使用了
DateTime.ToString("s") //s: 2008-06-15T21:15:07
插入到數據庫之后,發現時間被加了8個小時
找了半天資料,才找到原因
This profile defines two ways of handling time zone offsets:
有兩種方式處理時區的時差
Times are expressed in UTC (Coordinated Universal Time), with a special UTC designator ("Z").
如果是UTC時間的話,后面需要加Z表示。1994-11-05T13:15:30Z
Times are expressed in local time, together with a time zone offset in hours and minutes.
A time zone offset of "+hh:mm" indicates that the date/time uses a local time zone which is "hh" hours and "mm" minutes ahead of UTC.
A time zone offset of "-hh:mm" indicates that the date/time uses a local time zone which is "hh" hours and "mm" minutes behind UTC.
其它的時間,在年月日時分秒后面,通過+-時差來表示。
如果時間比UTC時間快的話,就用+hh:mm表示,后面的時間表示相差的時間。
如果時間比UTC時間慢的話,就用-hh:mm表示,后面的時間表示相差的時間。
+hh:mm和-hh:mm實際上表示的是時區,+08:00表示的是東八區,-05:00表示的是西五區
有兩種方式來說明時間
1994-11-05T08:15:30-05:00 corresponds to November 5, 1994, 8:15:30 am, US Eastern Standard Time.
//時間是1994-11-05 08:15:30 表示的是西五區的時間,比UTC時間慢5個小時 換算成UTC時間的話,需要加上5個小時,就是1994-11-05 13:15:30
1994-11-05T16:15:30+08:00 表示的是東八區的時間,比UTC時間快8個小時 換算成UTC時間的話,需要減去8個小時,就是1994-11-05 13:15:30
1994-11-05T13:15:30Z corresponds to the same instant.//很明顯是UTC時間
總結:
DateTime.ToString("s") //s: 2008-06-15T21:15:07
這個時間在存入SQLite數據庫的時候,被當做UTC時間了,因為我的電腦里面設置的時區,是東八區的北京時間,所以被加上了8個小時存儲
解決方法:
DateTime.ToString("yyyy-MM-dd HH:mm:ss") 使用這個,來存儲就沒有問題。
或者可以考慮設置SQLiteConnecttion.DateTimeKind
參考
Date and Time Formats
http://www.w3.org/TR/NOTE-datetime
DateTime.ToString Method (String)
http://msdn.microsoft.com/en-us/library/zdtaw1bw(v=vs.110).aspx