【问题标题】:Decimal value to date and time in Progress 4gl进行中的日期和时间的十进制值 4gl
【发布时间】:2020-07-09 12:31:17
【问题描述】:

我想知道如何将十进制值转换为日期和时间。

将变量 tt_decimal 定义为 DECIMAL INIT "2,459,040.7355"。

将变量 tt_date 定义为字符。 将变量 tt_time 定义为字符。

如何将上面的 tt_decimal 转换为 tt_date 和 tt_time。

【问题讨论】:

  • tt_decimal 值从何而来? .7355 部分是什么意思? ABL 中日期/时间的大多数数字表示形式都是整数。

标签: openedge progress-4gl


【解决方案1】:

没有直接转换。

一些早于 DATETIME 数据类型的旧应用程序使用各种约定来构建自己的。 (这都是 20 多年前的事了——现代代码使用内置的 DATETIME 数据类型。)有时他们将日期填充到整数侧,将时间填充到小数侧(通常以秒为单位除以 100,000)。但无法保证您的数据已采用这种方法。实际上,您的“示例代码”强烈建议您可能在字符串中包含 DATETIME,因为您已将初始值显示为字符串而不是小数。您需要确定您的数据到底做了什么。

如果您要构建新的东西,而不是与现有的遗留代码集成,您应该使用 DATETIME 或 DATETIME-TZ。不是十进制或字符。

如果您有使用 DECIMAL 数据类型的旧代码,并且如上所述,日期是整数部分,时间是小数部分,那么您可以像这样转换它:

function decimal2datetime return datetime ( input decimalDT as decimal ):

  define variable d as date    no-undo.
  define variable t as integer no-undo.

  define variable i as integer no-undo.
  define variable x as decimal no-undo.

  i = truncate( decimalDT, 0 ).    /* we will presume that the integer portion is a date */
  x = ( decimalDT - i ).           /* and that the decimal portion is time in seconds   */

  d = date( i ).                   /* convert an integer to a date using the default Progress epoch                           */
  t = 100000 * x.                  /* assuming that the time in seconds was converted to a decimal via division by 100,000 */

  return datetime( d, t * 1000 ).  /* the time parameter to datetime() is specified in milliseconds.                              */

end.


display decimal2datetime( 2459040.7355 ).

【讨论】:

  • 感谢汤姆的意见。我创建了 2 个参考上述函数的函数(decimalt2date 和 decimal2time)。效果很好。
猜你喜欢
  • 2021-04-08
  • 1970-01-01
  • 2018-08-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-09-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多