首页 > 解决方案 > RPG 4:将有符号整数转换为日期和时间

问题描述

我需要将时间戳(在 Signed Int 中)转换为日期和时间。我该怎么做?

主意:

D  stampint                     10I 0     //- value: '1455554435' - input parm 
D  Varprint                     10A      
D  Varprint2                     8A 0     

 /free
   Varprint  = %char(%date( (<cast to timestamp> stampint) :*MDY));
   Varprint2 = %char(%time( (<cast to timestamp> stampint) :*HMS));
   dsply Varprint;
   dsply Varprint;
   inlr = *on;
   return ;
 /End-free

 Objective result:
   DSPLY 02/15/2016
   DSPLY 16:40:35

谢谢。

更新:对不起,我提出的问题太快了,以至于我没有正确报告。

目标是能够报告日期和时间,而不管变量的类型如何。Var1 可以是 * CHAR 或数字(打包)。Varprint 和 Varprint2 是一个例子,但我想知道如何从类型 I 转换为类型 Z。

我曾尝试使用:

D timestmp          Z inz('1970-01-01-00.00.00.000000')
D result            Z inz('0001-01-01-00.00.00.000000')
... code..
 /free
   result = timestmp + %seconds(stampint);
   dsply result;
 /end-free

但结果是错误的。

我也试过:

Varprint  = %char(%date( %editc(stampint:'Z') :*MDY));

但是它给出了编译错误: *RNF7416- 左右手边的类型不进行 EVAL 操作。

标签: rpgle

解决方案


您需要使用 %timestamp(),除了 10 位整数对于实际时间戳来说不够大......日期为 6 位,时间为 6 位(包括秒)

最好的办法是使用%editc(sampint:'X')前导零转换为字符串,然后 %subst() 打破日期,我假设一段时间内只有 'HH:MI' ......

如果要转换为实际时间类型,则需要连接 ':00' 秒才能使用 %time();


推荐阅读