基于任意格式将字符串转换为 TDateTime [英] Converting a string to TDateTime based on an arbitrary format

查看:34
本文介绍了基于任意格式将字符串转换为 TDateTime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Delphi 5 中是否有任何方法可以将字符串转换为 TDateTime,您可以在其中指定要使用的实际格式?

Is there any way in Delphi 5 to convert a string to a TDateTime where you can specify the actual format to use?

我正在开发一个作业处理器,它接受来自各种工作站的任务.任务有一系列参数,其中一些是日期,但是(不幸的是,我无法控制)它们作为字符串传递.由于作业可能来自不同的工作站,用于将日期格式化为字符串的实际日期时间格式可能(当然,实际do)不同.

I'm working on a jobprocessor, which accepts tasks from various workstations. The tasks have a range of parameters, some of which are dates, but (unfortunately, and out of my control) they're passed as strings. Since the jobs can come from different workstations, the actual datetime format used to format the dates as a string might (and, of course, actual do) differ.

四处搜索,我找到的唯一快速解决方案是偷偷更改 ShortDateFormat 变量,然后将其恢复为原始值.由于 ShortDateFormat 是一个全局变量,而且我在线程环境中工作,因此唯一可行的方法是同步对它的每次访问,这是完全不可接受的(并且可以撤消的).

Googling around, the only quick solutions I found was to sneakily change the ShortDateFormat variable, and restore it to its original value afterwards. Since ShortDateFormat is a global variable, and I'm working in a threaded environment the only way this would work is by synchronizing every access to it, which is completely unacceptable (and undoable).

我可以将 SysUtils 单元中的库代码复制到我自己的方法中,并调整它们以使用指定的格式而不是全局变量,但我只是想知道是否还有更多内容足够了,我错过了.

I could copy the library code from the SysUtils unit into my own methods, and tweak them to work with a specified format instead of the global variables, but I'm just wondering if there's something more adequate out there that I missed.

更新

更简洁地说:

我需要诸如 StrToDate(或 StrToDateTime)之类的东西,并添加了指定用于将字符串转换为 TDateTime 的确切格式的选项.

I need something like StrToDate (or StrToDateTime), with the added option of specifying the exact format it should use to convert the string to a TDateTime.

推荐答案

我为 FreePascal 的 dateutils 单元创建了这样的例程,如果需要移植,它应该很容易移植.

I created such routine for FreePascal's dateutils unit, and it should easy to port, if porting is needed at all.

代码:

http://svn.freepascal.org/cgi-bin/viewvc.cgi/trunk/packages/rtl-objpas/src/inc/dateutil.inc?revision=30628&view=co

(代码是文件末尾的最后一个(巨大的)过程)

(code is the last (huge) procedure at the end of the file)

文档:

http://www.freepascal.org/docs-html/rtl/dateutils/scandatetime.html

请注意,它不是 formatdatetime 的完全倒数,它有一些扩展:

Note that it is not a complete inverse of formatdatetime, and it has some extensions:

  • FormatDateTime 的倒数不是 100% 的倒数,仅仅因为可以把例如格式字符串中的时间标记两次,并且 scandatetime 不知道选择哪个时间.

  • An inverse of FormatDateTime is not 100% an inverse, simply because one can put e.g. time tokens twice in the format string,and scandatetime wouldn't know which time to pick.

像 hn 这样的字符串不能安全地反转.例如.1:2(1 后 2 分钟)传递 12,解析为 12:00,然后错过了n"部分的字符.

Strings like hn can't be reversed safely. E.g. 1:2 (2 minutes after 1) delivers 12 which is parsed as 12:00 and then misses chars for the "n" part.

  • 忽略尾随字符.
  • 不支持东亚格式字符,因为它们只是窗口.
  • 不支持 MBCS.

扩展

  • #9 吃空格.
  • 模式末尾的空格是可选的.
  • ?匹配任何字符.
  • 引用上述字符以真正匹配字符.

(我相信这些评论有点过时了,因为后来添加了一些亚洲支持,但我不确定)

(I believe these comments are slightly outdated in the sense hat some Asian support was added later but I am not sure)

这篇关于基于任意格式将字符串转换为 TDateTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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