问题
I am using below code:
using new SQLite.Net.SQLiteConnection(new SQLitePlatformWinRT(), DBPath) to get SQLiteConnection and create table.
Class contain DateTime DataType
class Transaction
{
[SQLite.Net.Attributes.PrimaryKey, SQLite.Net.Attributes.AutoIncrement]
public int QId { get; set; }
public DateTime PurchaseDate { get; set; }
public int Amount {get;set;}
Public string ItemCode {get;set;}
}
Insert Data As follows:
var db = new SQLite.Net.SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), DBPath);
var newItem = new Transaction()
{
PurchaseDate = DateTime.Now,
Amount = 100,
ItemCode = "Abc-C10"
};
db.Insert(newItem);
The date will be stored as Ticks(e.g. 636071680313888433) and this is UTC time.
1) using above DateTime.Now, If my Computer time setting is
1a) in British Time,
will the above code : purchase = DateTime.Now be converted correctly?
1b) in Usa Time,
will the above code : purchase = DateTime.Now be converted correctly?
How to handle this tick in SQL-statement?
How to select all transaction from ,say, a date range ? say , 2016-07-10 to 2016-07-20 ?
Thanks
回答1:
The safest way to work with dates is to use the DateTimeOffset
type instead of DateTime
.
DateTime
does not contain the information about the time zone in which it was created, all it knows is whether it is in UTC or local time, which is not enough if the data is going to be used in different locations.
DateTimeOffset
contains not only the time and date information, but also the time zone, which means the result will always be what you expect.
There are no differences in the way it is used, just change the type:
class Transaction
{
[SQLite.Net.Attributes.PrimaryKey, SQLite.Net.Attributes.AutoIncrement]
public int QId { get; set; }
public DateTimeOffset PurchaseDate { get; set; }
public int Amount {get;set;}
Public string ItemCode {get;set;}
}
For database access:
var db = new SQLite.Net.SQLiteConnection(
new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), DBPath);
var newItem = new Transaction()
{
PurchaseDate = DateTimeOffset.Now, //or use DateTimeOffset.UtcNow for UTC datetime
Amount = 100,
ItemCode = "Abc-C10"
};
db.Insert(newItem);
回答2:
1a) in British Time, will the above code : purchase = DateTime.Now be converted correctly?
1b) in Usa Time, will the above code : purchase = DateTime.Now be converted correctly?
By "convert correctly" if you mean convert to the correct time value, the answer is yes. It will be convert to the UTC time using ticks.
How to handle this tick in SQL-statement?
How to select all transaction from ,say, a date range ? say , 2016-07-10 to 2016-07-20 ?
The convertion from ticks to other time units is like below:
- Ticks per day: 864,000,000,000
- Ticks per hour: 36,000,000,000
- Ticks per minute: 600,000,000
- Ticks per second: 10,000,000
- Ticks per millisecond: 10,000
So you can use the SQL-Statement like below to get the data from 2016-07-10 to 2016-07-20:
SELECT min(PurchaseDate) as 'Date',sum(Qty) as 'Size' from Purchase
group by (PurchaseDate/(864000000000*10)) //864000000000*10 = 1day*10
order by PurchaseDate
Update: If you want to select the Date between 2016-07-10 and 2016-07-20, you can use the following SQL-statement:
select strftime('%Y-%m-%d %H:%M:%S',purchaseDate/10000000 - 62135596800,'unixepoch') as 'date' from purchase where date between '2016-07-10' and '2016-07-20'
来源:https://stackoverflow.com/questions/39177071/how-sqlite-net-pcl-handle-utc-time