Midrange News for the IBM i Community


Posted by: Deepak Deshpande
AS/400 Freelance Programmer/Analyst
India
Built-in function %dec example
has no ratings.
Published: 29 Jun 2012
Revised: 23 Jan 2013 - 4111 days ago
Last viewed on: 26 Apr 2024 (6383 views) 

Using IBM i? Need to create Excel, CSV, HTML, JSON, PDF, SPOOL reports? Learn more about the fastest and least expensive tool for the job: SQL iQuery.

Built-in function %dec example Published by: Deepak Deshpande on 29 Jun 2012 view comments(6)

Hi

I am trying to get only MM/DD from system date and HH:MM from system time.

Can anyone point out where I am going wrong?

 

d                 DS

d xxDTTM                1      8  0
d  xxRVWDT              1      4  0
d  xxRVWTM              5      8  0
 

eval      xxRVWDT = %dec(%char(%date():*usa):4:0)

eval      xxRVWTM = %dec(%char(%time():*hms):4:0)

 

Thanks,

Deepak

Return to midrangenews.com home page.
Sort Ascend | Descend

COMMENTS

(Sign in to Post a Comment)
Posted by: TFisher
Premium member *
Comment on: Built-in function %dec example
Posted: 11 years 9 months 28 days 1 hours 16 minutes ago

You cannot put a date or time into a 4-digit number since the result of your operation is 6-digits.

 

Here is what you need to do:

D                 Ds                                      
d xxDtTm                         8  0                     
d xxRVWDT                        4  0 Overlay(xxDtTm:1)   
d xxRVWTM                        4  0 Overlay(xxDtTm:5)   

  xxRvwdt = %dec(%Subst(%char(%date:*Usa0):1:4):4:0);
  xxRvwtm = %dec(%Subst(%char(%time:*hms0):1:4):4:0);

 

Or, if you do not like the way this code looks you can do it this way:

 

D                 Ds                                   
d xxDtTm                         8  0                  
d xxMM                           2  0 Overlay(xxDtTm:1)
d xxDD                           2  0 Overlay(xxDtTm:3)
d xxHR                           2  0 Overlay(xxDtTm:5)
d xxMN                           2  0 Overlay(xxDtTm:7)

 

  xxMM = %Subdt(%date:*Months);   
  xxDD = %Subdt(%date:*days);     
  xxHR = %Subdt(%time:*hours);    
  xxMN = %Subdt(%time:*minutes); 

 

Posted by: neilrh
Premium member *
Jackson, MI
Comment on: Built-in function %dec example
Posted: 11 years 9 months 28 days 41 minutes ago

How about: 

MMDD = %dec(%date():*MDY) / 100;
HHMM = %dec(%time()) / 100;

 We have a 14s0 "timestamp" audit field in many of our files which I populate using %dec(%timestamp():*iso) / 1000000

Posted by: bobcozzi
Site Admin ****
Chicagoland
Comment on: Built-in function %dec example
Posted: 11 years 9 months 27 days 23 hours 44 minutes ago

The best solution is to use the Extract opcode (i.e., the %SUBDT built-in function) as Fish as indicated. However, just to point out that even if the assignment operations in your code would have worked, which they won't, you would have needed to omit the separators by adding a zero to the end of the date and time format specifiers. *USA0 and *HMS0 otherwise you would get 07/2 in your result instead of 0729.

In addition, the %CHAR built-in function always zero-suppresses leading zeros so your end result would have been 72912 and your "4:0" length probably would have produced 7291 in your MMDD result. So again, the %SUBDT is the best solution. But remember that %SUBDT always returns the Year Portion in YYYY format regardless of the date format.

 

Posted by: sarge
Premium member *
United States
Comment on: Built-in function %dec example
Posted: 11 years 9 months 27 days 16 hours 17 minutes ago

never (unless you absolutely have to) use multiplication or division to convert dates. you are essentially putting the processor into a DO loop, which is quite taxing.

using the old DATE MULT 10000.01 to reverse a date means the machine is actually executing a loop process ten thousand times that ADDS the the date to the result each "loop".

dividing is merely multiplication-in-opposite-direction. i believe the technique used by neil loops 100 times to divide the date.

using the %SUBST or even a MOVEL with a MOVE statement are by far the least processor intensive methods.

QUANTIFY: i base this on the older operating systems of the S/38, S/36 and S/34...S/32 and S/3 for those of you who remember :0)  it is always possible that while i've been off to afghanistan (again) that the processor has been modified so that it does not "loop" when doing multiplication and/or division...but i doubt it.

-sarge

Posted by: DeepakD
Premium member *
India
Comment on: Built-in function %dec example
Posted: 11 years 9 months 16 days 3 hours 8 minutes ago

Thanks everybody.

-Deepak

Posted by: clbirk
Premium member *
Comment on: Built-in function %dec example
Posted: 11 years 9 months 15 days 18 hours 56 minutes ago

sarge:

I don't buy what you are saying at all. I started out prior to the s/3 on another platform, but had a s/3, had many s/36's and the multiply command did NOT add 10,000 times.  There was a machine instruction to do multiplication and to do division.

 

Yes division was slower than mulitplication as it was faster to multiply by .5 than divide by 2.