Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Converting time into integer

Former Member
0 Kudos

Hi ,

I performing time claculations ,Is there FM to convert a field of type T into type Integer ?

i have 3 fields

tot begtime endtime

0.98 08:59 09:58

tot is of type Curr with 2 decimals,betime,endtime are of

T

i am getting problem when i am performing calculations like

tot = beg - end.

Regards.

23 REPLIES 23

Former Member
0 Kudos

Hi Aima,

Have two char and two integer varaibles.

1. Move the Start Time and End time to char variables.

2. Move the char values to integer variables.

3. Now subtract Endtime from Start time to get total.

Regards,

Rajasekhar

0 Kudos

Aima,

Rich's example is perfect for time differences within the same day.

If the time can span multiple days, use Func Module : SD_DATETIME_DIFFERENCE

It allows you to enter dates and times.

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos

One question, in what unit is the integar value to be. You have two times, do you need to see the difference in hours/minutes/seconds? If not, what could you possibly be trying to do?

As you can see in this sample program, the difference between the times is 59 mintues.

report zrich_0001.


data: time1 type sy-uzeit,
      time2 type sy-uzeit,
      time_diff type sy-uzeit.

   time1 = '095800'.
   time2 = '085900'.


   time_diff = time1 - time2.

   write:/ time1,
         / time2,
         / time_diff.

Regards,

Rich Heilman

Former Member
0 Kudos

What are you trying to get, difference between the times in seconds, or minutes, or hours or all? If you declare the tot as type i(integer), then you will get the difference as the number of seconds, or if you define tot as another time field, then you will get it time format HHMMSS.

Srinivas

0 Kudos

Alternatively, you can use DURATION_DETERMINE function module, give today's date to both start and end dates and then give your times to start time and end time. It will give you the difference in the unit you give to the export parameter UNIT of this function module.

0 Kudos

Hi Srinivas,

i have 2 different things,

i have 2 fields given as Type t and 1 field of type Curr with 2 decimal values,

Now wat i have to do is perform calculation based on certain condition i have to break the total time into 2 different times based on begtime and time:

for tat i am doing

WA_SUBRTN_IFLEX-GSMTZT = WA_SUBRTN_IFLEX-GEHZT - WA_SUBRTN_IFLEX-BEGZT.

but a dump occurs with field overflow.

could u plz define this ?

0 Kudos

Again, I think you need to be using all time field.




report zrich_0001.

data: time1 type sy-uzeit,
      time2 type sy-uzeit,
      time_diff type sy-uzeit.

time1 = '095800'.
time2 = '085900'.


time_diff = time1 - time2.



write:/ time1,
      / time2,
      / time_diff.


Using all time fields will give you the difference in time HHMMSS. Now you can do what ever you need to do with this. For example, your requirement is to find the difference, then split it and add it to each time value. This is just an example.

report zrich_0001.

data: time1 type sy-uzeit,
      time2 type sy-uzeit,
      time_diff type sy-uzeit.

time1 = '095800'.
time2 = '085900'.


time_diff = ( time1 - time2 ) / 2.


write:/ time1,
      / time2,
      / time_diff.

Skip 5.

time1 =  time1 + time_diff.
time2 =  time2 + time_diff.

write:/ time1,
     / time2,
     / time_diff.

Regards,

Rich Heilman

0 Kudos

Can you post the relevant code where you do this and also the exact data definition part of the code?

Srinivas

0 Kudos

Hi Srinivas,

As i said i am getting a **** .txt file and have to work accordingly hence i am getting the data as

0.98 08:59 09:58

1.73 10:14 11:58

i gave explanation in my earlier post regarding adding a company required functionality in SAP which communicates with HR and WM.

Regards.

0 Kudos

If you are getting these values on a .txt file, how can you say that types are these? When you upload this file, aren't you going to define your own fields into which these values go? I wanted to see what that definition is in your code. You mentioned, you got a overflow error. You will get this error if your field length is not sufficient for the difference.

Also, if you are calculating difference between two times, why is your "tot" field defined as CURR field? Shouldn't that be either a time field or an integer?

Another thing is, you say you have to calculate "tot" based on "beg" and "end", but your example shows that you are getting some value for "tot" also. I am confused as to what you are getting on that file and what condition you are trying to implement.

Please clarify.

Srinivas

0 Kudos

hi Srinivas,

<b> how can you say that types are these? .</b>

According to the specificaion i have defined begzt as Type T,endzt as Type t tot time as currency with decimals, but i have the flexibility to change also !

<b>Also, if you are calculating difference between two times, why is your "tot" field defined as CURR field? Shouldn't that be either a time field or an integer?</b>

The problem i am facing here is when i am trying to get the time difference i have to store in this format <b>0.98 </b>which is not of type T and integer will truncate this value.

Plz suggest me wat data type should i take to store this in a flexible way and perform operations

A<b>nother thing is, you say you have to calculate "tot" based on "beg" and "end", but your example shows that you are getting some value for "tot" also.

</b>

yes i am getting the tot time also, hence when begtime and endtime differs for a shift i have to spilit the total time also .

0 Kudos

What does <b>0.98</b> represent. It is the difference between begin time and end time? In what, seconds, hours, minutes, What? I think, hours, Yes?

Regards,

Rich Heilman

0 Kudos

Hi Rich

U r correct .

0 Kudos

Ok, I assume that .98 means the difference is .98 of an hour. If that is correct. Then you would do something like this to convert it to a time. Here CONVERT_TO_TIME is a time field that will have the difference in HHMMSS format. Then store this in your db.



report zrich_0001.

data: time_diff type p decimals 2 value '.98'.

data: convert_to_time type sy-uzeit.

time_diff = time_diff * 3600.

convert_to_time = time_diff.

write convert_to_time.


Regards,

Rich Heilman

0 Kudos

Ok, that is making some sense now. Let us now concentrate on what should be the data type for "tot". When you have a value of 0.98 what does that mean? Is it 0.98 hours or what? You can define your "tot" as type p decimals 2. But I don't see how you can have a '.98' difference when you are doing the subtraction on the time fields.

Difference between two time fields is always an integer value.

More clarification is needed on this aspect.

Thanks,

Srinivas

thomas_jung
Developer Advocate
Developer Advocate

I prefer to use TimeStamps for this sort of thing, especially if you must cross days.

The following is an example from 640 that uses the new cl_abap_tstmp class.

 cl_abap_tstmp=>systemtstmp_syst2utc(
        exporting
          syst_date = <wa_joblist>-strtdate
          syst_time = <wa_joblist>-strttime
        importing
          utc_tstmp = start_stamp ).

      cl_abap_tstmp=>systemtstmp_syst2utc(
        exporting
          syst_date = sy-datum
          syst_time = sy-uzeit
        importing
          utc_tstmp = now_stamp ).

      seconds = cl_abap_tstmp=>subtract(
          tstmp1 = now_stamp
          tstmp2 = start_stamp ).

Former Member
0 Kudos

From the help on 'COMPUTE':


DATA: SECONDS TYPE I, 
      TIME_FROM TYPE T VALUE '200000', 
      TIME_TO   TYPE T VALUE '020000'. 
SECONDS = ( TIME_TO - TIME_FROM ) MOD 86400. 

SECONDS has the value 21600 (6 hours). The "MOD 86400" operation in this example ensures that the result is not negative, even if the time period spans midnight.

Rob

Former Member
0 Kudos

Hi Aima,

How to determine of the times(Beg,End) are on the same date? Ignore my earlier post.

As srinivas suggested, post the code, how you are getting the beg and end times...plz

Regards,

Raj

Former Member
0 Kudos

Use Function Module SD_CALC_DURATION_FROM_DATETIME

It returns a DEC 11 value. It also returns a parameter tells you which of the 2 dates is greater.

Former Member
0 Kudos

Hi Rich

<b>.98 means the difference is .98 of an hour.</b>

From my understanding it is .98 Hours.

<b>@Srinivas</b>

<b>Difference between two time fields is always an integer value.</b>

Yes , if u declare as resultant variable as type i then it is,but for me i have received as 0.98 hours .

for the difference of 08:59 and 09:59.

0.98 08:59 09:58

0 Kudos

Again, here is how you would convert the .98 of an hour to a time field. You will want to use the time field for adding to the begin time or end time or whatever it is that you want to do.



report zrich_0001.
data: time_diff type p decimals 2 value '.98'.
data: convert_to_time type sy-uzeit.
time_diff = time_diff * 3600.
convert_to_time = time_diff.
write convert_to_time.


Regards,

Rich Heilman

0 Kudos

Hi Rich ,

Thanks for carrying me for till here , i am working on ur code nd will be back as soon as i get on with a solution.

<b>Rewarding point to all u guys is not just appreiciation but also defining how professional u all r .</b>

Regards.

0 Kudos

OK let us try this.

data: v_time_diff type i,

v_input_time_diff type p decimals 2,

time_beg type t,

time_end type t.

v_time_diff = time_end - time_beg.

v_input_time_diff = '.98'.

v_input_time_diff = v_input_time_diff * 3600.

After this you can do whatever you want with the two fields.

Srinivas