View previous topic :: View next topic
|
Author |
Message |
ruhdolf
New User
Joined: 21 Jan 2021 Posts: 7 Location: Österreich
|
|
|
|
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 |
|
|
sergeyken
Senior Member
Joined: 29 Apr 2008 Posts: 2022 Location: USA
|
|
|
|
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 |
|
|
Nic Clouston
Global Moderator
Joined: 10 May 2007 Posts: 2455 Location: Hampshire, UK
|
|
|
|
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 |
|
|
ruhdolf
New User
Joined: 21 Jan 2021 Posts: 7 Location: Österreich
|
|
|
|
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 |
|
|
steve-myers
Active Member
Joined: 30 Nov 2013 Posts: 917 Location: The Universe
|
|
|
|
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 |
|
|
ruhdolf
New User
Joined: 21 Jan 2021 Posts: 7 Location: Österreich
|
|
|
|
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 |
|
|
Nic Clouston
Global Moderator
Joined: 10 May 2007 Posts: 2455 Location: Hampshire, UK
|
|
|
|
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 |
|
|
ruhdolf
New User
Joined: 21 Jan 2021 Posts: 7 Location: Österreich
|
|
|
|
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 |
|
|
Garry Carroll
Senior Member
Joined: 08 May 2006 Posts: 1193 Location: Dublin, Ireland
|
|
|
|
[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 |
|
|
ruhdolf
New User
Joined: 21 Jan 2021 Posts: 7 Location: Österreich
|
|
|
|
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 |
|
|
sergeyken
Senior Member
Joined: 29 Apr 2008 Posts: 2022 Location: USA
|
|
|
|
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 |
|
|
Nic Clouston
Global Moderator
Joined: 10 May 2007 Posts: 2455 Location: Hampshire, UK
|
|
|
|
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 |
|
|
ruhdolf
New User
Joined: 21 Jan 2021 Posts: 7 Location: Österreich
|
|
|
|
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 |
|
|
ruhdolf
New User
Joined: 21 Jan 2021 Posts: 7 Location: Österreich
|
|
|
|
ok it's an overflow |
|
Back to top |
|
|
Nic Clouston
Global Moderator
Joined: 10 May 2007 Posts: 2455 Location: Hampshire, UK
|
|
|
|
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 |
|
|
sergeyken
Senior Member
Joined: 29 Apr 2008 Posts: 2022 Location: USA
|
|
|
|
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 |
|
|
prino
Senior Member
Joined: 07 Feb 2009 Posts: 1306 Location: Vilnius, Lithuania
|
|
|
|
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 |
|
|
sergeyken
Senior Member
Joined: 29 Apr 2008 Posts: 2022 Location: USA
|
|
|
|
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 |
|
|
prino
Senior Member
Joined: 07 Feb 2009 Posts: 1306 Location: Vilnius, Lithuania
|
|
|
|
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 |
|
|
prino
Senior Member
Joined: 07 Feb 2009 Posts: 1306 Location: Vilnius, Lithuania
|
|
|
|
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 |
|
|
|