gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 12:33:46 27 4
gpt4 key购买 nike

我正在尝试构建一个漂亮的小型数据库以在移动应用程序(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 是否允许我这样做。我从未尝试过。)然后获取 base dateTimeSpan > 和我想要的日期,提取天数并存储?

// 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 base date 和我的输入日期之间,提取秒数并存储?

// 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

关于c# - SQLite 日期和时间数据类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8569894/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com