日期时间字符串到毫秒数 [英] Datetime string to number of milliseconds
问题描述
我从网络资源中提取数据,并以"YYYY-MM-DDTHH:MM:SS.SSSZ"格式返回时间.由于我将在本专栏做很多比较,因此我认为最好将它们存储为自纪元以来的毫秒数(我需要毫秒精度!).将传入的日期时间字符串转换为毫秒数的最佳方法是什么?
I pull data from a websource and it returns time in the format 'YYYY-MM-DDTHH:MM:SS.SSSZ'. Since I would be doing a lot of comparisons on this column, I thought it would be best to store them as the number of millliseconds since epoch (I need millisecond precision!!). What would be the best way to convert the incoming datetime string to number of milliseconds?
我尝试使用
strftime('%s',datetimeString)
-返回从纪元到datetimeString的秒数
strftime('%s', datetimeString)
- gives back the number of seconds since epoch till datetimeString
strftime('%f',datetimeString)
-仅返回SS.SSS部分!
strftime('%f', datetimeString)
- gives back only the SS.SSS part!!
推荐答案
I'm guessing it has something to do with SQLite not having actual Date or Datetime types (section 1.2). Instead it formats them as one of:
- ISO8601字符串(
TEXT
类型) - 浮点天数(
REAL
类型) - 自纪元时间以来的整数秒数(
INTEGER
类型)
- an ISO8601 string (
TEXT
type) - a floating point number of days (
REAL
type) - an integer number of seconds since the epoch time (
INTEGER
type)
REAL
数据类型的精度可能不足以在单个字段中存储日期,时间和小数秒.
The REAL
data type may not have enough precision to store a date, time, and fractional seconds in a single field.
似乎答案是以下之一:
- 将它们存储为两个单独的字段,日期时间存储在一个字段中,小数秒存储在另一个字段中
- 自己计算整数毫秒数,并将整数结果存储在单列中,只要所需的小数秒精度的最大水平适合8字节或更少的有符号整数(SQLite3中的
INTEGER
类型) - 将数据库切换到支持实际日期/日期时间类型的DBMS
无需切换数据库,并且如果您永远不需要亚毫秒级的精度,那么我至少会尝试手动计算+单整数列"解决方案,看看它是否有效.
Without switching your DB, and if you never need sub-millisecond precision, then I'd at least try the "manual calculation + single integer column" solution and see if it works.
这篇关于日期时间字符串到毫秒数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!