Skip to Content
avatar image
Former Member

Converting time into integer

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.

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

8 Answers

  • avatar image
    Former Member
    Nov 28, 2005 at 06:41 PM

    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

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member

      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.

  • Nov 28, 2005 at 06:47 PM

    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

    Add comment
    10|10000 characters needed characters exceeded

  • Nov 28, 2005 at 06:47 PM

    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

    Add comment
    10|10000 characters needed characters exceeded

    • 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

  • Nov 28, 2005 at 07:06 PM

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

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Nov 28, 2005 at 07:24 PM

    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

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Nov 28, 2005 at 07:26 PM

    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

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Nov 28, 2005 at 07:41 PM

    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.

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Nov 28, 2005 at 09:07 PM

    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

    Add comment
    10|10000 characters needed characters exceeded

    • 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