# Calculate with timestamp

Author Message
ruhdolf

New User

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

 Posted: Thu Jan 21, 2021 2:57 pm 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!
sergeyken
Warnings : 2

Senior Member

Joined: 29 Apr 2008
Posts: 1139

 Posted: Thu Jan 21, 2021 7:09 pm 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.
Nic Clouston

Global Moderator

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

 Posted: Thu Jan 21, 2021 7:27 pm 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.
ruhdolf

New User

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

Posted: Fri Jan 22, 2021 11:27 am

 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?
steve-myers

Active Member

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

 Posted: Fri Jan 22, 2021 1:35 pm 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.
ruhdolf

New User

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

 Posted: Fri Jan 22, 2021 2:24 pm 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
Nic Clouston

Global Moderator

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

Posted: Fri Jan 22, 2021 2:59 pm

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.
ruhdolf

New User

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

 Posted: Fri Jan 22, 2021 3:27 pm 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
Garry Carroll

Senior Member

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

Posted: Fri Jan 22, 2021 4:29 pm

[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.
ruhdolf

New User

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

Posted: Fri Jan 22, 2021 4:56 pm

 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
sergeyken
Warnings : 2

Senior Member

Joined: 29 Apr 2008
Posts: 1139

Posted: Fri Jan 22, 2021 6:20 pm

 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.
Nic Clouston

Global Moderator

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

 Posted: Fri Jan 22, 2021 6:30 pm 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.
ruhdolf

New User

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

Posted: Fri Jan 22, 2021 7:28 pm

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
ruhdolf

New User

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

 Posted: Fri Jan 22, 2021 7:47 pm ok it's an overflow
Nic Clouston

Global Moderator

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

 Posted: Fri Jan 22, 2021 8:03 pm 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.
sergeyken
Warnings : 2

Senior Member

Joined: 29 Apr 2008
Posts: 1139

 Posted: Fri Jan 22, 2021 8:11 pm 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.
prino
Warnings : 1

Senior Member

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

 Posted: Fri Jan 22, 2021 9:28 pm 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!
sergeyken
Warnings : 2

Senior Member

Joined: 29 Apr 2008
Posts: 1139

Posted: Sat Jan 23, 2021 1:38 am

 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.
prino
Warnings : 1

Senior Member

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

Posted: Sat Jan 23, 2021 4:36 pm

 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.
prino
Warnings : 1

Senior Member

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

Posted: Sat Jan 23, 2021 8:37 pm

 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.
 View Bookmarks All times are GMT + 6 Hours

 Topic Forum Replies Similar Topics To get previous month from current ti... SYNCSORT 7 Timestamp in TCP/IP for Z/VSE JCL JCL & VSAM 0 how to add date & timestamp to th... DFSORT/ICETOOL 5 calculate the record count and based ... DFSORT/ICETOOL 1 DB2 Timestamp field weird behaviour. DB2 8
Search our Forums: