IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

Calculate with timestamp


IBM Mainframe Forums -> PL/I & Assembler
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
ruhdolf

New User


Joined: 21 Jan 2021
Posts: 7
Location: Österreich

PostPosted: Thu Jan 21, 2021 2:57 pm
Reply with quote

Hello, i'm new here.

i have no knowledge in PL1 and i want to subtract 2 timestamps.
the result of timst1 - timst2 should be in this form --> 00.01.13.385771

DCL ZW_TIMST CHAR(26);
timst1 = timestamp();
….
timst2 = timestamp();

i get (examp) 2021-01-21-06.34.59.359528 and want to convert in an integer, that i can calculate
HH*60*60*1000000 + MM*60*1000000 + SS*1000000 + mills - (HH2*60*60*1000000 + MM2*60*1000000 + SS2*1000000 + mills2)
and then i calculate hours, minutes, seconds and millionth of seconds

can you help me how to convert from char to integer? which datatype should i usw?
i should be performant

thank you!
Back to top
View user's profile Send private message
sergeyken

Senior Member


Joined: 29 Apr 2008
Posts: 2022
Location: USA

PostPosted: Thu Jan 21, 2021 7:09 pm
Reply with quote

1. Use functions SUBSTR to extract digital substrings from timestamp string.
2. Convert extracted CHAR substrings to FIXED BINARY values.
3. Perform all arithmetics with FIXED BINARY.
4. Convert final results to formatted CHAR string.

You can find these examples in any PL/I Manual For Beginners.
Back to top
View user's profile Send private message
Nic Clouston

Global Moderator


Joined: 10 May 2007
Posts: 2455
Location: Hampshire, UK

PostPosted: Thu Jan 21, 2021 7:27 pm
Reply with quote

I would define a structure over your CHAR(26) variable with the numeric portions defined as PIC 9s, You COULD use those directly in you calculations but I would copy them to FIXED DEC variables.

You also need to be aware of time stamps running across midnight.
Back to top
View user's profile Send private message
ruhdolf

New User


Joined: 21 Jan 2021
Posts: 7
Location: Österreich

PostPosted: Fri Jan 22, 2021 11:27 am
Reply with quote

Nic Clouston wrote:
I would define a structure over your CHAR(26) variable with the numeric portions defined as PIC 9s, You COULD use those directly in you calculations but I would copy them to FIXED DEC variables.

You also need to be aware of time stamps running across midnight.


Can you give me en example how to redefine?

In Cobol i would do it like...

05 timst.
10 timst-yyyy pic 9(4).
10 filler pic x(1).
10 timst-mm pic 9(2).
10 filler pic x(1).
10 timst-dd pic 9(2).
……

but in pl1?
Back to top
View user's profile Send private message
steve-myers

Active Member


Joined: 30 Nov 2013
Posts: 917
Location: The Universe

PostPosted: Fri Jan 22, 2021 1:35 pm
Reply with quote

Forget using the character string this timestamp function returns. I'm almost certain it is a function your installation wrote. The character string it returns appears to me to be a character representation of the hex digits returned by the IBM TIME macro.

I got news for you: the TIME macro has many other options. One of them is to return a 64-bit binary value that represents the current time of day clock translated to microseconds, which is exactly what you want.

Unless you want to delve into writing LE compliant Assembler, I'd get out the Language Environment manuals. I'd bet there is an LE function that can provide what you need in a format that is more readily usable to you.
Back to top
View user's profile Send private message
ruhdolf

New User


Joined: 21 Jan 2021
Posts: 7
Location: Österreich

PostPosted: Fri Jan 22, 2021 2:24 pm
Reply with quote

TIME-Macro?

I did it like that

DCL PNT_T1 POINTER STATIC;
DCL ZW_TIMST1_X CHAR (26) BASED (PNT_T1);
DCL 1 ZW_TIMST1 BASED (PNT_T1),
2 ZW_TIMST1_YYYY PIC '9999',
2 ZW_TIMST1_X1 CHAR (1),
2 ZW_TIMST1_MM PIC '99',
2 ZW_TIMST1_X2 CHAR (1),
2 ZW_TIMST1_DD PIC '99',
2 ZW_TIMST1_X3 CHAR (1),
2 ZW_TIMST1_HH PIC '99',
2 ZW_TIMST1_X4 CHAR (1),
2 ZW_TIMST1_MI PIC '99',
2 ZW_TIMST1_X5 CHAR (1),
2 ZW_TIMST1_SS PIC '99',
2 ZW_TIMST1_X6 CHAR (1),
2 ZW_TIMST1_MIS PIC '999999';

ZW_TIMST1_X = TIMESTAMP();

But it doesn't work. i got an error at the instruction
Back to top
View user's profile Send private message
Nic Clouston

Global Moderator


Joined: 10 May 2007
Posts: 2455
Location: Hampshire, UK

PostPosted: Fri Jan 22, 2021 2:59 pm
Reply with quote

What error? You do not show the allocation of ZW_TIMST1_X. You could code your declaration thus:
Code:

DCL ZW_TIMST1_X CHAR (26);
DCL 1 ZW_TIMST1 DEF ZW_TIMST1_X POS(1),
          2 ZW_TIMST1_YYYY PIC '9999',
          2 ZW_TIMST1_X1 CHAR (1),
          2 ZW_TIMST1_MM PIC '99',
          2 ZW_TIMST1_X2 CHAR (1),
          2 ZW_TIMST1_DD PIC '99',
          :

Note the use of code tags to present the code. Please do so in future.
Back to top
View user's profile Send private message
ruhdolf

New User


Joined: 21 Jan 2021
Posts: 7
Location: Österreich

PostPosted: Fri Jan 22, 2021 3:27 pm
Reply with quote

i didn't know that i had to allocate.
in Cobol i only redefine.
i didn't usw pointer in the past

thanks, now after allocation it works.

i didn't understand POS(1) in your last code
Back to top
View user's profile Send private message
Garry Carroll

Senior Member


Joined: 08 May 2006
Posts: 1193
Location: Dublin, Ireland

PostPosted: Fri Jan 22, 2021 4:29 pm
Reply with quote

[quote]
Code:
Code:

DCL ZW_TIMST1_X CHAR (26);
DCL 1 ZW_TIMST1 DEF ZW_TIMST1_X POS(1),
          2 ZW_TIMST1_YYYY PIC '9999',
          2 ZW_TIMST1_X1 CHAR (1),
          2 ZW_TIMST1_MM PIC '99',
          2 ZW_TIMST1_X2 CHAR (1),
          2 ZW_TIMST1_DD PIC '99',
          :[/quote]


DEFining a field as starting a POSition(1) is similar to Cobol's REDEFINES. Personally, I prefer to base on an address or on a pointer that addresses the underlying field.

Garry.
Back to top
View user's profile Send private message
ruhdolf

New User


Joined: 21 Jan 2021
Posts: 7
Location: Österreich

PostPosted: Fri Jan 22, 2021 4:56 pm
Reply with quote

Code:

     DCL PNT_T1      POINTER STATIC;
     DCL ZW_TIMST1_X    CHAR (26)  BASED (PNT_T1);
     DCL 1 ZW_TIMST1 BASED (PNT_T1),
          2 ZW_TIMST1_YYYY        PIC '9999',
          2 ZW_TIMST1_X1          CHAR (1),
          2 ZW_TIMST1_MM          PIC '99',
          2 ZW_TIMST1_X2          CHAR (1),
          2 ZW_TIMST1_DD          PIC '99',
          2 ZW_TIMST1_X3          CHAR (1),
          2 ZW_TIMST1_HH          PIC '99',
          2 ZW_TIMST1_X4          CHAR (1),
          2 ZW_TIMST1_MI          PIC '99',
          2 ZW_TIMST1_X5          CHAR (1),
          2 ZW_TIMST1_SS          PIC '99',
          2 ZW_TIMST1_X6          CHAR (1),
          2 ZW_TIMST1_MIS         PIC '999999';
     DCL ZW_TIMST1_N        BIN FIXED(31);

     ZW_TIMST1_X = TIMESTAMP();

/* variant 1 */
     ZW_TIMST1_N = ZW_TIMST1_HH * 1000000 * 60 * 60;

/* variant 2       */
     ZW_TIMST1_N = ZW_TIMST1_HH;
     ZW_TIMST1_N = ZW_TIMST1_N  * 1000000 * 60 * 60;


Why does variant 1 doesn't work?
variant 2 works fine. why is it necessary to move the ZW_TIMST1_HH to ZW_TIMST1_N. only after that it's possible to multiplacate
Back to top
View user's profile Send private message
sergeyken

Senior Member


Joined: 29 Apr 2008
Posts: 2022
Location: USA

PostPosted: Fri Jan 22, 2021 6:20 pm
Reply with quote

ruhdolf wrote:

Why does variant 1 doesn't work?

The term “doesn’t work” is meaningless.
If you need help, you must present here meaningful information, such as:
1) compilation error?
2) execution error? error code, or message?
3) program ABEND? abend code?
4) incorrect results? what exactly is wrong?
5) the computer got fire, or exploded?
Etc, etc, etc.
Back to top
View user's profile Send private message
Nic Clouston

Global Moderator


Joined: 10 May 2007
Posts: 2455
Location: Hampshire, UK

PostPosted: Fri Jan 22, 2021 6:30 pm
Reply with quote

Without knowing what the error is no one can tell. One possibility is that one of the system assigned intermediate fields is too small. A reference to the PL/1 Programmers Guide manual may help.
If you do not know what POS(1) is refer to the PL/1 Language Reference manual.
In PL/1 if you are using controlled storage you have to allocate. In COBOL you probably were not using controlled storage hence the REDEFINEs works.
Back to top
View user's profile Send private message
ruhdolf

New User


Joined: 21 Jan 2021
Posts: 7
Location: Österreich

PostPosted: Fri Jan 22, 2021 7:28 pm
Reply with quote

It's an execution error

Code:
IBM0301S ONCODE=320  The ZERODIVIDE condition was raised.
         From entry point MBV223 at statement 6516 at compile unit offset +0000
         address 20DDC9E2.
ERROR condition was raised
Back to top
View user's profile Send private message
ruhdolf

New User


Joined: 21 Jan 2021
Posts: 7
Location: Österreich

PostPosted: Fri Jan 22, 2021 7:47 pm
Reply with quote

ok it's an overflow
Back to top
View user's profile Send private message
Nic Clouston

Global Moderator


Joined: 10 May 2007
Posts: 2455
Location: Hampshire, UK

PostPosted: Fri Jan 22, 2021 8:03 pm
Reply with quote

Well, it is a zero divide not an overflow. But as you are not doing a divide then it must be in the generated code - probably caused by an overflow.
For performance reasons you should convert the PIC variable to FIXED DEC before converting to FIXED BIN. Doing PIC -> FIXED BIN directly means calling a subroutine - there should be a compiler message regarding this.
Back to top
View user's profile Send private message
sergeyken

Senior Member


Joined: 29 Apr 2008
Posts: 2022
Location: USA

PostPosted: Fri Jan 22, 2021 8:11 pm
Reply with quote

The first step you need to do in case of such error: print all involved fields as they are, before any other manipulations.

(Next step might be: print some intermediate results, step by step.)

This is a very standard approach for any professional who has had any deal with at least one single program code in any language during his (professional) life.
Back to top
View user's profile Send private message
prino

Senior Member


Joined: 07 Feb 2009
Posts: 1306
Location: Vilnius, Lithuania

PostPosted: Fri Jan 22, 2021 9:28 pm
Reply with quote

What you, and everyone else in this thread, really need to do is to RTFM!

Enterprise PL/I has a ton of builtin functions to work with dates and times!
Back to top
View user's profile Send private message
sergeyken

Senior Member


Joined: 29 Apr 2008
Posts: 2022
Location: USA

PostPosted: Sat Jan 23, 2021 1:38 am
Reply with quote

prino wrote:
What you, and everyone else in this thread, really need to do is to RTFM!

Enterprise PL/I has a ton of builtin functions to work with dates and times!

No builtin function, or whatever else would help when one has not a minor idea about programming.
Back to top
View user's profile Send private message
prino

Senior Member


Joined: 07 Feb 2009
Posts: 1306
Location: Vilnius, Lithuania

PostPosted: Sat Jan 23, 2021 4:36 pm
Reply with quote

steve-myers wrote:
Forget using the character string this timestamp function returns. I'm almost certain it is a function your installation wrote.


Nope, it's a standard PL/I builtin function.
Back to top
View user's profile Send private message
prino

Senior Member


Joined: 07 Feb 2009
Posts: 1306
Location: Vilnius, Lithuania

PostPosted: Sat Jan 23, 2021 8:37 pm
Reply with quote

Nic Clouston wrote:
I would define a structure over your CHAR(26) variable with the numeric portions defined as PIC 9s, You COULD use those directly in you calculations but I would copy them to FIXED DEC variables.

Which is what the compiler would do anyway, so it's not really necessary to do it yourself, unless you want a specific precision.
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> PL/I & Assembler

 


Similar Topics
Topic Forum Replies
No new posts To get the count of rows for every 1 ... DB2 3
No new posts how to calculate SUM value for VB fil... DFSORT/ICETOOL 1
No new posts how to calculate SUM for VB file usin... JCL & VSAM 1
No new posts Insert system time/date (timestamp) u... DFSORT/ICETOOL 5
No new posts Timestamp difference and its average ... DB2 11
Search our Forums:

Back to Top