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

查看:133
本文介绍了基于任意格式将字符串转换为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?

我正在处理一个作业处理程序,它接受来自各种工作站的任务。这些任务有一系列参数,其中一些是日期,但是(不幸的是,我的控制之外)它们作为字符串传递。由于作业可能来自不同的工作站,因此用于将日期格式化为字符串的实际日期时间格式可能(当然也是实际的 )不同。

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.

Googling,我发现唯一的快速解决方案是偷偷地更改 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.

请问,并提前感谢

Willem

更新

更简洁地说:

我需要像 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%的倒数,只是因为可以把eg时间令牌在格式字符串中两次,而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。 / li>
  • trailing characters are ignored.
  • no support for Eastern Asian formatting characters since they are windows only.
  • no MBCS support.

扩展程序


  • #9吃一个空格。

  • 模式结尾的空格是可选的。

  • 匹配任何char。

  • 引用上述字符来真正匹配char。

  • #9 eats whitespace.
  • whitespace at the end of a pattern is optional.
  • ? matches any char.
  • Quote the above chars to really match the char.

(我相信这些评论稍微过时了,一些亚洲支持后来加了一些,但我不太清楚)

(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天全站免登陆