日期时间字符串到毫秒数 [英] Datetime string to number of milliseconds

查看:65
本文介绍了日期时间字符串到毫秒数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从网络资源中提取数据,并以"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!!

推荐答案

我猜想它与SQLite有关

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.

似乎答案是以下之一:

无需切换数据库,并且如果您永远不需要亚毫秒级的精度,那么我至少会尝试手动计算+单整数列"解决方案,看看它是否有效.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆