c# – SQLite日期和时间数据类型

我正在尝试构建一个漂亮的小型数据库,以便在移动应用程序上运行(Windows Mobile 5,如果你很好奇).

SQLite Documentation中,日期和时间数据类型定义如下:

1.2 Date and Time Datatype

SQLite does not have a storage class set aside for storing dates
and/or times. Instead, the built-in Date And Time Functions of SQLite
are capable of storing dates and times as TEXT, REAL, or INTEGER
values:

  • TEXT as ISO8601 strings (“YYYY-MM-DD HH:MM:SS.SSS”).
  • REAL as Julian day numbers, the number of days since noon in Greenwich on November 24, 4714 B.C. according to the proleptic
    Gregorian calendar.
  • INTEGER as Unix Time, the number of seconds since 1970-01-01 00:00:00 UTC.

Applications can chose to store dates and times in any of these
formats and freely convert between formats using the built-in date and
time functions.

因此,将我的DateTime值保存为REAL(float)或INTEGER的大小相同.

TEXT格式怎么样? YYYY-MM-DD HH:MM:SS.SSS中有23个字符.每个字符是8个字节吗?如果是这样,那就是以文本格式存储的巨大空间(这正是我目前所做的).

REAL格式怎么样?我是否定义基准日期为公元前4714年11月24日? (我甚至不确定Visual Studio 2008是否允许我这样做.我从未尝试过.)然后在我想要的基准日期和日期之间获取TimeSpan,提取天数并存储它?

// is this how to declare this date?
private static readonly DateTime nov24_4714bc = new DateTime(-4714, 11, 24);

public static double GetRealDate(DateTime dateTime) {
  // FYI: subtracting dates in .NET returns a time span object
  return (dateTime - nov24_4714bc).TotalDays;
}

INTEGER格式怎么样?我会定义1970-01-01 00:00:00 UTC的基准日期(请告诉我该怎么做!),然后在基准日期和输入日期之间获取TimeSpan,提取秒数,并存储?

// is this a UTC date?
private static readonly DateTime utc1970_01_01 = new DateTime(1970, 1, 1);

public static double GetIntDate(DateTime dateTime) {
  // FYI: subtracting dates in .NET returns a time span object
  return (dateTime - nov24_4714bc).TotalSeconds;
}

对此有何帮助?我有点困惑几点.

解决方法:

>如果“人类可读性”很重要,请使用TEXT格式.
>如果节省空间很重要,请使用其中一种数字格式.

如果您不需要毫秒精度,则可以通过仅包含您需要的部分来节省TEXT格式的空间. SQLite日期/时间函数接受3种较短格式:

> YYYY-MM-DD HH:MM:SS(19个字符)
> YYYY-MM-DD HH:MM(16个字符)
> YYYY-MM-DD(10个字符)

(绝对不要使用MM / DD / YYYY;它不受支持,并且排序不正确.)

Would I define a base date of November 24, 4714 B.C.? (I am not even
sure if Visual Studio 2008 will let me do that. I’ve never tried.)

你不能:System.DateTime只支持1到9999年.你需要选择一个不同的基准日期,然后执行(dateTime – baseDate).TotalDays baseDateJD,其中baseDateJD是基准日期的Julian日期.一些合理的选择是:

> 0001-01-01 = JD 1721425.5
> 1970-01-01 = JD 2440587.5
> 2000-01-01 = JD 2451544.5

相关文章

SQLite架构简单,又有Json计算能力,有时会承担Json文件/RES...
使用Python操作内置数据库SQLite以及MySQL数据库。
破解微信数据库密码,用python导出微信聊天记录
(Unity)SQLite 是一个软件库,实现了自给自足的、无服务器...
安卓开发,利用SQLite实现登陆注册功能