timestamp in abap (utclong)

*get/create a current timestamp
    DATA var_utc TYPE utclong.
    var_utc = utclong_current(  ).
    out->write( var_utc ).
    "Expected outcome: 2024-01-10T08:57:07.744744Z

*manipulate a timestamp
    "add 1 day and subtract 1 hour
    var_utc = utclong_add( val = var_utc days = 1 hours = -1 ).
    out->write( var_utc ).
    "Expected outcome: 2024-01-11T07:57:07.744744Z
    "subtract 10 days and add 2 minutes
    var_utc = utclong_add( val = var_utc days = -10 minutes = 2 ).
    out->write( var_utc ).
    "Expected outcome: 2024-01-01T07:59:07.744744Z

*compare timestamps
    DATA difference TYPE i.
    DATA var_utc2 TYPE utclong.
    var_utc = utclong_current(  ).
    var_utc = utclong_add( val = var_utc minutes = 1 ).
    var_utc2 = utclong_current(  ).
    difference = utclong_diff( high = var_utc low = var_utc2 ).
    out->write( difference ).
    "Expected outcome: 60 (seconds)

*splitting timestamp into date and time
    CONVERT UTCLONG var_utc
    TIME ZONE cl_abap_context_info=>get_user_time_zone( )
    INTO DATE DATA(target_date)
    TIME DATA(target_time).
    out->write( target_date ).
    "Expected outcome: 2024-01-10
    out->write( target_time ).
    "Expected outcome: 08:58:07

Leave a Reply

Your email address will not be published. Required fields are marked *