如何将 SQLite 中的当前时间戳存储为刻度?

发布于 2024-11-06 03:25:19 字数 766 浏览 5 评论 0原文

我有一个 SQLite 数据库,我将日期存储为刻度。我没有使用默认的 ISO8601 格式。假设我有一个定义如下的表:

CREATE TABLE TestDate (LastModifiedTime DATETIME)

我希望使用 SQL 插入当前日期和时间。如果我执行以下任何语句,我最终都会将日期和时间存储为字符串而不是刻度。

INSERT INTO TestDate (LastModifiedTime) VALUES(CURRENT_TIMESTAMP)
INSERT INTO TestDate (LastModifiedTime) VALUES(DateTime('now'))

我查看了 SQLite 文档,但我似乎没有找到任何选项来获取当前时间戳(以刻度为单位)。

我当然可以在 C# 中定义一个参数并将该值存储为 System.DateTime。这确实会导致日期时间以刻度为单位存储到数据库中。

我想做的是能够直接从 SQL 语句中插入和更新当前时间戳。我该怎么做?

编辑:

我希望将数据作为刻度存储在数据库中的原因是,日期的存储格式与 ADO.Net 数据提供程序存储的格式相同,因此当数据也使用 ADO.Net 提供程序进行查询时,它会作为 System.DataTime .Net 类型正确检索。

I have a SQLite database where I store the dates as ticks. I am not using the default ISO8601 format. Let's say I have a table defined as follows:

CREATE TABLE TestDate (LastModifiedTime DATETIME)

Using SQL, I wish to insert the current date and time. If I execute any of the below statements, I end up getting the date and time stored as a string and not in ticks.

INSERT INTO TestDate (LastModifiedTime) VALUES(CURRENT_TIMESTAMP)
INSERT INTO TestDate (LastModifiedTime) VALUES(DateTime('now'))

I have looked at the SQLite documenation, but I do not seem to find any option to obtain the current timestamp in ticks.

I can of course define a parameter in C# and store the value as a System.DateTime. This does result in the datetime getting stored to the database in ticks.

What I would like to do is be able to insert and update the current timestamp directly from within the SQL statement. How would I do this?

Edit:

The reason I want the data stored as ticks in the database, is that the dates are stored in the same format as stored by the ADO.Net data provider, and so that when the data is also queried using the ADO.Net provider it is correctly retrieved as a System.DataTime .Net type.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

玻璃人 2024-11-13 03:25:19

SQLite 的这一特殊之处给我带来了很大的痛苦。

简单的方法 - 作为常规时间戳存储和检索

create table TestDate (
        LastModifiedTime datetime
);
insert into TestDate (LastModifiedTime) values (datetime('now'));
select datetime(LastModifiedTime), strftime('%s.%f', LastModifiedTime)  from TestDate;

输出: 2011-05-10 21:34:46|1305063286.46.000

痛苦的方法 - 作为 UNIX 时间戳存储和检索

您可以使用 strftime 检索刻度值。此外,要存储 UNIX 时间戳(大致相当于刻度),您可以将秒数括在单引号中。

 insert into TestDate (LastModifiedTime) values ('1305061354');

SQLite 会在内部将其存储为非 UNIX 时间戳的其他值。在检索时,您需要显式告诉 SQLite 将其作为 UNIX 时间戳进行检索。

 select datetime(LastModifiedTime, 'unixepoch') FROM TestDate;

要存储当前日期和时间,请使用 strftime('%s', 'now')。

 insert into TestDate (LastModifiedTime) VALUES (strftime('%s', 'now'));

完整示例:

create table TestDate (
        LastModifiedTime datetime
);      
insert into TestDate (LastModifiedTime) values (strftime('%s', 'now'));
select datetime(LastModifiedTime, 'unixepoch') from TestDate;

当由 sqlite3 执行时,此脚本带有打印:

2011-05-10 21:02:34 (or your current time)

This particular oddity of SQLite caused me much anguish.

Easy way - store and retrieve as regular timestamp

create table TestDate (
        LastModifiedTime datetime
);
insert into TestDate (LastModifiedTime) values (datetime('now'));
select datetime(LastModifiedTime), strftime('%s.%f', LastModifiedTime)  from TestDate;

Output: 2011-05-10 21:34:46|1305063286.46.000

Painful way - store and retrieve as a UNIX timestamp

You can use strftime to retrieve the value in ticks. Additionally, to store a UNIX timestamp (roughly equivalent to ticks), you can can surround the number of seconds in single-quotes.

 insert into TestDate (LastModifiedTime) values ('1305061354');

SQLite will store this internally as some other value that is not a UNIX timestamp. On retrieval, you need to explicitly tell SQLite to retrieve it as a UNIX timestamp.

 select datetime(LastModifiedTime, 'unixepoch') FROM TestDate;

To store the current date and time, use strftime('%s', 'now').

 insert into TestDate (LastModifiedTime) VALUES (strftime('%s', 'now'));

Full example:

create table TestDate (
        LastModifiedTime datetime
);      
insert into TestDate (LastModifiedTime) values (strftime('%s', 'now'));
select datetime(LastModifiedTime, 'unixepoch') from TestDate;

When executed by sqlite3, this script with print:

2011-05-10 21:02:34 (or your current time)
韬韬不绝 2024-11-13 03:25:19

经过进一步研究 SQLite 文档 和其他有关日期数字转换的信息,我想出了下面的公式,它似乎产生了正确的结果:

INSERT INTO TestDate(LastModifiedTime)
    VALUES(CAST((((JulianDay('now', 'localtime') - 2440587.5)*86400.0) + 62135596800) * 10000000 AS BIGINT))

似乎是一种痛苦的方式来产生我希望可以作为内置日期时间格式使用的东西,特别是数据库支持以刻度形式存储日期时间值。希望这对其他人也有用。

更新:

对于夏令时,上述公式并不完美。请参阅 SQLite 文档中有关本地时间计算的注意事项和错误部分。

After further study of the SQLite documentation and other information found on date number conversions, I have come up with the following formula, which appears to produce correct results:

INSERT INTO TestDate(LastModifiedTime)
    VALUES(CAST((((JulianDay('now', 'localtime') - 2440587.5)*86400.0) + 62135596800) * 10000000 AS BIGINT))

Seems like a painful way to produce something that I would expect to be available as a built-in datetime format, especially that the database supports the storing of datetime values in ticks. Hopefully, this becomes useful for others too.

Update:

The above formula is not perfect when it comes to daylight savings. See section Caveats And Bugs in SQLite docs regarding local time calculation.

土豪我们做朋友吧 2024-11-13 03:25:19

以下将返回自 UNIX 纪元以来的毫秒数:

SELECT (strftime('%s', 'now') - strftime('%S', 'now') + strftime('%f', 'now')) * 1000 AS ticks

它的工作原理是获取自 UNIX 纪元 (%s) 以来的秒数,减去当前时间的秒数 ( %S),加上带小数位的秒数 (%f),并将结果乘以 1000,将秒转换为毫秒。

减法和加法是为了增加值的精度而不扭曲结果。正如 SQLite 文档中所述,同一步骤中所有使用 'now' 将返回相同的值。

The following will return the number of milliseconds since the UNIX Epoch:

SELECT (strftime('%s', 'now') - strftime('%S', 'now') + strftime('%f', 'now')) * 1000 AS ticks

It works by grabbing the number of seconds since the Unix Epoch (%s), subtracting the number of seconds in the current time (%S), adding the number of seconds with decimal places (%f), and multiplying the result by 1000 to convert from seconds to milliseconds.

The subtraction and addition are to add precision to the value without skewing the result. As stated in the SQLite Documentation, all uses of 'now' within the same step will return the same value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文