2016-08-27 30 views
0

我使用下面的代码:如何SQLite.Net-PCL手柄UTC时间

使用新SQLite.Net.SQLiteConnection(新SQLitePlatformWinRT(),DBPATH),以获得SQLiteConnection 和创建表。

类包含日期时间数据类型

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;}  
    } 

插入数据如下:

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); 

日期将被(636071680313888433 e.g)存储为蜱,这是UTC时间。使用上述DateTime.Now,如果我的电脑时间设置为

1A)在英国时间

1),

将上面的代码:购买= DateTime.Now正确转换?

1B)在美国时间,

将上面的代码:购买= DateTime.Now正确转换?

如何在SQL语句中处理此刻表?

如何从日期范围中选择所有交易?说,2016-07-10至2016-07-20?

感谢

+1

你是什么意思“正确转换”?最正确的行为是没有转换。 –

回答

1

与日期,安全的做法是改用DateTimeOffsetDateTime

DateTime没有包含创建时区的信息,只知道它是UTC还是本地时间,如果数据要在不同的位置使用,这是不够的。

DateTimeOffset不仅包含时间和日期信息,还包含时区,这意味着结果将始终如您所愿。

有它的使用方式没有差异,只是改变类型:

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;}  
} 

对于数据库的访问:在英国时间

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); 
+0

如何选择Sqlite包含UTC时间的日期范围。可以告诉我这是如何完成的。 – MilkBottle

0

1A),将上面的代码:purchase = DateTime.Now要正确转换吗?

1b)在美国时间, 将上面的代码:purchase = DateTime.Now是否能正确转换?

通过“正确地转换”如果您的意思是转换为正确的时间值,答案是肯定的。它将使用刻度转换为UTC时间。

如何处理这种蜱在SQL语句?

如何从日期范围中选择所有交易?说,2016-07-10至2016-07-20?

从蜱到其他时间单位的皈依是象下面这样:6亿每

  • 蜱:360亿种每分钟
  • 蜱:8640亿种每小时
  • 蜱:

    每天
    • 蜱第二:10,000,000
    • 蜱每毫秒:10,000

    所以,你可以使用的SQL语句像下面从2016年7月10日至2016年7月20日获得的数据:

    SELECT min(PurchaseDate) as 'Date',sum(Qty) as 'Size' from Purchase 
    group by (PurchaseDate/(864000000000*10)) //864000000000*10 = 1day*10 
    order by PurchaseDate 
    

    更新:如果你想选择的日期2016-07-10和2016-07-20,你可以使用下面的SQL语句:

    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' 
    
  • +0

    @Xia:如何选择2016-07-10和2016-07-20之间的日期?选择min(PurchaseDate)作为'Date',从“”和“”之间购买金额(金额)。如何在双引号“”内表示日期? – MilkBottle

    +0

    你不能在SQLite中的双引号内表示日期。 2016-07-10和2016-07-20之间进行选择,请参阅我的更新。 –

    +0

    @夏:我很快就会回来。非常感谢你。我将努力在这个项目上展示基于微软UWP的应用程序。我希望我能为这个SQLite数据表示使用一些很酷的控件和UI。 – MilkBottle