在 Delphi 中将 TDateTime 声明为 Const
据我所知,没有办法做到这一点,但我会问以防万一其他人知道如何做到这一点。 在 Delphi 中如何将日期声明为常量?
我找到的唯一解决方案是使用数字等价物,维护起来有点痛苦,因为它不是人类可读的。
const
Expire : TDateTime = 39895; // Is actually 3/23/2009
我希望能够做的是这样的事情:
const
Expire : TDateTime = TDateTime ('3/23/2009');
或者
const
Expire : TDateTime = StrToDate('3/23/2009');
所以让我知道这是否是一个功能请求或者我是否只是错过了如何做到这一点(是的,我知道这似乎是一件奇怪的事情...... .)
As far as I know there is no way to do this, but I am going to ask just in case someone else knows how to do this. How can I declare a date as a const in Delphi?
The only solution I have found is to use the numeric equivalent, which is kind of a pain to maintain because it is not human readable.
const
Expire : TDateTime = 39895; // Is actually 3/23/2009
What I would like to be able to do is something like this:
const
Expire : TDateTime = TDateTime ('3/23/2009');
or
const
Expire : TDateTime = StrToDate('3/23/2009');
So let me know if this is a feature request or if I just missed how to do this (yeah, I know it seems like an odd thing to want . . . .)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
好吧,我的反应有点晚了,但这里有一个针对较新的德尔福的解决方案。
它使用隐式类重载器,以便可以像使用 TDateTime 变量一样使用此类型的记录。
实现:
现在你可以像这样声明你的日期:
要查看它是否有效,请执行以下命令:
如果你真的想用它替换所有 TdateTime 变量,你可能还需要重载一些其他运算符(加、减、显式、 ...)。
Ok, my reaction is a bit late, but here's a solution for the newer Delphi's.
It uses implicit class overloaders so that records of this type can be used as if they are TDateTime variables.
Implementation:
Now you can declare your dates like this:
To see if it works, execute the following:
If you really want to replace all your TdateTime variables with this, you probably need to overload some other operators too (Add, subtract, explicit, ...).
唯一的? 可能的方式,但可能不是您正在寻找的:
The only? possible way, but probably not what you are looking for:
我倾向于用函数模拟 const 日期。 从技术上讲,它们比“伪常量”可分配类型const更常量。
注意使用
EncodeDate
而不是StrToDate
。StrToDate
受区域设置的影响,这意味着不能保证字符串会按预期进行解释。例如,您是否知道有一群奇怪的人认为将日期部分“洗牌”成不一致的重要性顺序是有意义的? 他们使用中间部分,然后是最不重要的部分,然后是最重要的部分(例如“3/23/2009”)<厚脸皮的笑容>。 逻辑唯一有意义的时候是当你年满 102 岁时 - 那么你就可以声称你的年龄是 021 岁。
I tend to simulate const dates with a function. Technically they're a little more constant than the "pseudo-constant" assignable typed const's.
NOTE the use of
EncodeDate
rather thanStrToDate
.StrToDate
is affected by regional settings meaning there's no guarantee a string will be interpreted as would be expected.For example, did you know that there's a strange a group of people who think it makes sense to "shuffle" date parts into an inconsistent order of significance? They use middle, then least, then most significant part (e.g. '3/23/2009') <cheeky grin>. The only time that logic makes sense is when you turn 102 years old - then you can claim your age is 021.
没有办法做到这一点,因为解释日期文字本身并不是确定性的,它取决于您遵循的约定/区域设置。
例如,对于任何法国人来说,“1/4/2009”都不是一月份,并且将编译器翻译为 1 月 4 日将使其成为傻瓜编译器;-)
除非编译器实现一些(有据可查的)“神奇”双射函数来配对日期值和显示表示......无论如何,地球的一半人不会喜欢它。
我现在看到的唯一明确的方式是提供价值,即使它看起来很痛苦......
...我的 0.02 美元
There is no way to do this because interpreting a date litteral in itself is not deterministic, it depends on the convention/locale you follow.
'1/4/2009' is not in January for any French person for instance, and having the compiler translating as January 4th would make it a fool's compiler ;-)
Unless the compiler implements some (well documented) "magic" bijective function for pairing a date value and a display representation... And anyway, half of the planet would not like it.
The only non ambiguous way I see now is to provide the value even if it looks like a pain...
... my $0.02
不,Delphi 不支持这一点。
您的第一个想法是请求与普通浮点文字不同的日期时间文字。 我找到了 QC 72000,它是关于显示
TDateTime< /code> 值作为调试器中的日期,但与您的特定请求无关。 不过,以前也没有人提过这个问题。 这是新闻组中常年出现的话题。 我只是在 QC 中找不到任何相关内容。
您的第二个想法需要
StrToDate
在编译时可评估。 我在 QC 中也没有看到任何有关它的条目,但就其价值而言,C++ 正在为具有必要品质的函数提供这样的功能。 但是,StrToDate
无法满足这些要求,因为它对当前区域设置的日期设置很敏感。No, Delphi doesn't support that.
Your first idea would be a request for date-time literals distinct from ordinary floating-point literals. I found QC 72000, which is about displaying
TDateTime
values as dates in the debugger, but nothing about your particular request. It's not like nobody's ever mentioned it before, though. It's a perennial topic on the newsgroups; I just can't find anything in QC about it.Your second idea would require
StrToDate
to be evaluable at compile time. I don't see any entries in QC about it either, but for what it's worth, C++ is getting such a feature for functions that are shown to have the necessary qualities.StrToDate
wouldn't meet those requirements, though, because it's sensitive to the current locale's date settings.Rob Kennedy 的回答表明,StrToDate 解决方案本质上是不可能的,因为如果代码是在欧洲编译的,您不希望代码被破坏!
我确实同意应该有某种方法来执行 EncodeDate 但没有。
就我而言,编译器应该简单地编译并运行它在常量赋值中找到的任何代码,并将结果存储到常量中。 我将其留给程序员来确保代码对其环境不敏感。
Rob Kennedy's answer shows that the StrToDate solution is inherently out of the question as you don't want your code to break if it's compiled in Europe!
I do agree there should be some way to do EncodeDate but there isn't.
As far as I'm concerned the compiler should simply compile and run any code it finds in a constant assignment and store the result into the constant. I'd leave it up to the programmer to ensure the code isn't sensitive to it's environment.
一种解决方案是创建一个多年来的常量列表,另一个用于月份偏移量的列表,然后动态构建它。 您必须自己处理闰年,将每个结果常量加 1。 下面只是一些帮助您开始...:)
如果您有闰年,那么您必须为该年 2 月之后的任何内容加 1。
编辑
为了清楚起见,又添加了几年,并添加了 Leap_Day 常量。
One solution would be to create a list of constants for years, another for month offsets and then build it on the fly. You would have to take care of leap years yourself by adding 1 to each resulting constant. Just a few below to get you started... :)
If you have a leap year then you have to add 1 to anything beyond february of that year.
EDIT
Added a few more years for clarity, and added a Leap_Day constant.
Delphi 日期是自 1899 年 12 月 30 日以来的天数。 因此,您可能会想出一个复杂的数学公式来将日期表示为常量。 然后你可以非常奇怪地格式化它,以强调人类可读的部分。 我最好的尝试如下,但它非常不完整; 一方面,它假设所有月份都有 30 天。
不过,我的例子主要是为了好玩。 实际上,这是相当荒谬的。
A Delphi date is the # of days since Dec 30, 1899. So you could probably come up with an elaborate mathematical formula to express a date as a const. Then you could format it very oddly, to emphasize the human-readable parts. My best attempt is below, but it is very much incomplete; for one thing, it assumes that all months have 30 days.
My example is mostly for fun though. In practice, this is pretty ridiculous.
我认为您可用的最佳解决方案是声明:
并接受它。
我的尝试No.1
我的尝试 N°2
因此,即使它们是常量且确定的(即不依赖于任何区域设置信息),它仍然不起作用。
i think the best solution available to you is to declare:
and just accept it.
My attempt Nº1
My attempt Nº2
So even though they're constant, and deterministic (i.e. do not depend on any locale information), it still doesn't work.
类型“TDateTime”=类型“Double”。
算法:
2.const
DTZiro: TDateTime = 2.04237268518519;
The type "TDateTime" = type "Double".
Algorithm:
2.const
DTZiro: TDateTime = 2.04237268518519;
System.DateUtils 对于每个时间部分都有常量。
System.DateUtils has constants for each part of time.