## UTC format which is basically the number of milliseconds since Jan 1 1970 convert in RPG

5 pts.
Tags:
AS/400
RPG ILE
Is there a straightforward way using rpg ile to convert the UTC date into ymd format

Thanks. We'll let you know when a new response is added.

This will convert 1074528964 milliseconds to 04/01/19

D unixts s 10U 0 inz(1074528964)
D epoch s z inz(z’1970-01-01-00.00.00′)
D ts s z
DYmd s D DATFMT(*YMD)

c epoch adddur unixts:*S ts
c move ts Ymd
c dsply Ymd
c eval *inlr = *on

## Discuss This Question: 6 Replies

There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.
• There's something "funny" going on here. The Unix epoch is based on the number of seconds since Jan 1 1970 and not the number of milliseconds. The provided answer then correctly shows how to use seconds since the epoch, but is worded within the context of milliseconds (as is the original question). If you really want milliseconds since Jan 1 1970 you should use the *MS duration code on the ADDDUR rather than *S. But I suspect that the use of milliseconds in the question and answer should really be rephrased to seconds.
report
• Oops! *MS is microseconds, not milliseconds. For a millisecond input you would need to divide the millisecond value by 1000 to get seconds and then add that duration to Epoch:
```dSeconds          s             10u 0

c                   eval      Seconds = MS/1000
c     Epoch         adddur    Seconds:*S    TS
```
or alternatively multiply the millisecond input and then add a *MS duration.
report
• for seconds d date1 s 8a d UTC s 10i 0 inz(1074528964) d UTCBase s z inz(z'1970-01-01-00.00.00.000000') c eval date1 = %char( c %date(UTCBase + %seconds(UTC)) c :*ymd) for milliseconds d date1 s 8a d UTC s 10i 0 inz(1074528964) d UTCBase s z inz(z'1970-01-01-00.00.00.000000') c eval date1 = %char( c %date(UTCBase + %seconds(UTC/1000)) c :*ymd)
report
• ```d  date1          s              8a
d  UTC            s             10i 0 inz(1074528964)
d  UTCBase        s               z   inz(z'1970-01-01-00.00.00.000000')

c                   eval      date1 = %char(
c                                     %date(UTCBase + %seconds(UTC))
c                                     :*ymd)                              ```
report
• OK, just had to make some procedures for this assuming seconds since 1/1/70
```     h dftactgrp(*no) actgrp(*caller) indent('|') option(*srcstmt:*nodebugio)

d  date1          s               d
d  UTC1           s             10i 0

d UTC2Date        pr              d
d  inUTC                        10i 0 const
d Date2UTC        pr            10i 0
d  inDate                         d   const

c                   eval      date1 = UTC2Date(1074528964)
c                   eval      UTC1 = Date2UTC(%date('2004-01-19'))
c                   dump(a)
c                   eval      *inlr = *on

p UTC2Date        b
d UTC2Date        pi              d
d  inUTC                        10i 0 const
c                   return    %date(%timestamp(
c                             '1970-01-01-00.00.00.000000')
c                             + %seconds(inUTC))
p UTC2Date        e

p Date2UTC        b
d Date2UTC        pi            10i 0
d  inDate                         d   const
c                   return    %diff(inDate:%date('1970-01-01'):*days)
c                             * 24 * 60 * 60
p Date2UTC        e
```