进行中的日期和时间的十进制值 4gl

Decimal value to date and time in Progress 4gl

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

定义变量tt_decimal作为小数初始化“2,459,040.7355”。

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

如何将上面的tt_decimal转换成tt_date和tt_time.

没有直接转换。

一些早于 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 ).