以字符串格式存储日期值的最佳方法是什么?
我必须以字符串格式存储日期值 (TDateTime
)。最好的方法是什么?我考虑了以下方法:
FloatToStr
:失去精度,取决于区域设置
`FloatToStr' 和格式设置:失去精度
DateTimeToStr
:取决于区域设置
DateTimeToStr 与格式设置: ?
还有其他选择吗? 方面进行比较
- 它们如何在内存大小、
- 区域设置独立性
- 、精度
I have to store date values (TDateTime
) in a string format. What is the best way to do this? I considered the following approaches:
FloatToStr
: looses precision, depends on locale settings
`FloatToStr' with format settings : looses precision
DateTimeToStr
: depends on locale settings
DateTimeToStr
with format settings : ?
Are there any other alternatives? How do they compare in terms of
- Size in memory
- Independence of locale settings
- Precision
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用 ISO-8601 格式,详见 http://en.wikipedia.org/wiki/ISO_8601
如果需要节省存储空间,可以使用“紧凑”布局,例如“20090621T054523”。
您可以使用例如
FormatDateTime('yyyymmddThhnnss',aDateTime)
来生成它。关于时区和本地化(来自维基百科):
因此,您最好将时间转换为 UTC,然后在时间戳末尾附加“Z”。或者根据您当地的时区使用+hh/-hh。 :“18:30Z”、“22:30+04”、“1130-0700”和“15:00-03:30”。
以下时间均指同一时刻 为了获得更好的分辨率,您可以通过在逗号或点字符后添加分数来添加亚秒计时:例如,要表示“14 小时、30 分钟、10 秒和 500 毫秒”,请将其表示为“14:30:10, 5”、“143010,5”、“14:30:10.5”或“143010.5”。您可以添加几位小数以提高分辨率。
如果您需要快速 Iso8601 转换例程(处理 UTF-8 内容),请查看 SynCommons.pas。它比默认的 SysUtils 函数快得多。
PS:
如果您的目的只是将 TDateTime 作为文本存储在纯 Delphi 应用程序中,您可以使用非标准但速度很快:
使用
Int64
TDateTime/double 内存结构的二进制布局将比任何其他浮点相关转换更快。Use ISO-8601 format, as detailed in http://en.wikipedia.org/wiki/ISO_8601
If you need to save storage space, you can use the "compact" layout, e.g. '20090621T054523'.
You can use e.g.
FormatDateTime('yyyymmddThhnnss',aDateTime)
to produce it.About time zone and localisation (from wikipedia):
So you should better convert the time into UTC, then append 'Z' at the end of the timestamp. Or use +hh/-hh according to your local time zone. The following times all refer to the same moment: "18:30Z", "22:30+04", "1130-0700", and "15:00-03:30".
For a better resolution, you can add sub-second timing by adding a fraction after either a comma or a dot character: e.g. to denote "14 hours, 30 minutes, 10 seconds and 500 ms", represent it as "14:30:10,5", "143010,5", "14:30:10.5", or "143010.5". You can add several decimals to increase resolution.
If you need fast Iso8601 conversion routines (working with UTF-8 content), take a look at the corresponding part in SynCommons.pas. It's much faster than the default
SysUtils
functions.PS:
If your purpose is just to store TDateTime as text in a pure Delphi application, you can use a not standard but fast:
Using the
Int64
binary layout ofTDateTime/double
memory structure will be faster than any other floating-point related conversion.一般来说,我建议将 ISO 格式的日期时间存储为字符串: yyyy-mm-dd hh:nn:ss.mmmm
编辑:如果您想最小化空间,您可以省略所有分隔符并将其格式化为: yyyymmddhhnnssmmmm
Generally I would recommend to store datetimes in ISO format as string: yyyy-mm-dd hh:nn:ss.mmmm
EDIT: if you want to minimize space, you can leave out all the separators and format it like: yyyymmddhhnnssmmmm
FormatDateTime('yyyymmddhhnnss.zzz', 现在)
FormatDateTime('yyyymmddhhnnss.zzz', Now)
您需要多少精度?例如,您可以获取自 1970 年 1 月 1 日以来的毫秒数,然后将其存储为转换为字符串的数字。如果空间真的很紧张,您可以对该值进行 Base64 处理。缺点是两者都不是人类可读的。
How much precision do you need? You could take, say, the number of milliseconds since, say, 1/1/1970, and just store that as a number converted to a string. If space is really tight, you could base64 that value. The downside would be that neither would be human readable.