使用 CreateODBCDateTime() 创建的对象的 return 类型是什么?
What is the return type of an object created with CreateODBCDateTime()?
假设我在 cfscript 中有以下函数...return 类型是什么?
[return type goes here] function GetODBCDateTime () {
return CreateODBCDateTime (Now ());
}
提示:这不是 ODBCDateTime
。
来自 documentation,看起来像一个以 ODBC 时间戳格式构造的 DateTime。
正确答案似乎是 string
。我想重要的是要记住字符串在 ColdFusion(和 Java)中被视为对象。从文档中,CreateODBCDateTime()
的 return 类型是:
A date-time object, in ODBC timestamp format.
在本例中,它可以是 ODBC 时间戳格式的 字符串。
简答: 看看 documentation for cffunction,它显示了可用的 return 类型。对于日期和时间对象,使用 date
、 而不是 字符串。尽管除非有特定原因需要 CreateODBCDateTime,否则您可以简单地使用 Now()
。对于 大多数 目的,两者是等价的。
更长的答案:
A date-time object, in ODBC timestamp format
which really means: a string in ODBC timestamp format.
不完全是。尽管当表示 为字符串时它确实具有非常特定的格式,但 CreateODBCDateTime 仍然 return 是一个 date/time 对象。如果转储 class 信息,可以看到它实际上是 java.util.Date 的子 class,而不是 java.lang.String.
// string representation
writeDump(GetODBCDateTime().toString());
// base class
writeDump(GetODBCDateTime().getClass().name);
// superclass
writeDump(GetODBCDateTime().getClass().getSuperClass().name);
For example, I can call getClass().getSuperClass().name on the returned object and get java.util.Date, if the return type is either date or string!
你是对的,但 IMO 不应该发生这种情况。我不知道 CF 如何在内部处理 return 类型,但是......这听起来像是由于 CF 通常的 automagic-type-conversion 规则在工作 - 或者 - 也许 CF 只是使用 return类型以验证对象 可以 转换为指定类型,如果可以,则对象 returned "as is"。但是,您仍应使用类型 date
以确保函数 始终 return 是一个日期对象。否则,它可以 return 它想要的任何东西,比如字符串 "apple" 而不是日期。
Dumping either returned object always gives a plain text string.
现在 是 我所期望的。当您 cfdump 任何对象时,CF 通常会调用该对象的 toString()
method. As the name implies, it returns a string representation of the object. With CreateODBCDateTime, toString() is designed to return the underlying date value in a very specific format of {ts 'yyyy-mm-dd HH:nn:ss'}
. However, internally the object is still a date. Yet when dumped, it is visually identical to a date string with the same value. (Dumps are great for debugging, but they favor presentation over accuracy,因此不要总是相信您所看到的 ;-)。
假设我在 cfscript 中有以下函数...return 类型是什么?
[return type goes here] function GetODBCDateTime () {
return CreateODBCDateTime (Now ());
}
提示:这不是 ODBCDateTime
。
来自 documentation,看起来像一个以 ODBC 时间戳格式构造的 DateTime。
正确答案似乎是 string
。我想重要的是要记住字符串在 ColdFusion(和 Java)中被视为对象。从文档中,CreateODBCDateTime()
的 return 类型是:
A date-time object, in ODBC timestamp format.
在本例中,它可以是 ODBC 时间戳格式的 字符串。
简答: 看看 documentation for cffunction,它显示了可用的 return 类型。对于日期和时间对象,使用 date
、 而不是 字符串。尽管除非有特定原因需要 CreateODBCDateTime,否则您可以简单地使用 Now()
。对于 大多数 目的,两者是等价的。
更长的答案:
A date-time object, in ODBC timestamp format
which really means: a string in ODBC timestamp format.
不完全是。尽管当表示 为字符串时它确实具有非常特定的格式,但 CreateODBCDateTime 仍然 return 是一个 date/time 对象。如果转储 class 信息,可以看到它实际上是 java.util.Date 的子 class,而不是 java.lang.String.
// string representation
writeDump(GetODBCDateTime().toString());
// base class
writeDump(GetODBCDateTime().getClass().name);
// superclass
writeDump(GetODBCDateTime().getClass().getSuperClass().name);
For example, I can call getClass().getSuperClass().name on the returned object and get java.util.Date, if the return type is either date or string!
你是对的,但 IMO 不应该发生这种情况。我不知道 CF 如何在内部处理 return 类型,但是......这听起来像是由于 CF 通常的 automagic-type-conversion 规则在工作 - 或者 - 也许 CF 只是使用 return类型以验证对象 可以 转换为指定类型,如果可以,则对象 returned "as is"。但是,您仍应使用类型 date
以确保函数 始终 return 是一个日期对象。否则,它可以 return 它想要的任何东西,比如字符串 "apple" 而不是日期。
Dumping either returned object always gives a plain text string.
现在 是 我所期望的。当您 cfdump 任何对象时,CF 通常会调用该对象的 toString()
method. As the name implies, it returns a string representation of the object. With CreateODBCDateTime, toString() is designed to return the underlying date value in a very specific format of {ts 'yyyy-mm-dd HH:nn:ss'}
. However, internally the object is still a date. Yet when dumped, it is visually identical to a date string with the same value. (Dumps are great for debugging, but they favor presentation over accuracy,因此不要总是相信您所看到的 ;-)。