SAP Tutorials Blog


 

 


DATA(ts_ref1) =  xco_cp_time=>moment( iv_year   = '2024'

                                      iv_month  = '01'

                                      iv_day    = '01'

                                      iv_hour   = '11'

                                      iv_minute = '37'

                                      iv_second = '54' ).


DATA(ts_ref2) =  xco_cp_time=>moment( iv_year   = '2024'

                                      iv_month  = '02'

                                      iv_day    = '02'

                                      iv_hour   = '10'

                                      iv_minute = '28'

                                      iv_second = '59' ).


"Additions; various optional parameters are available

"2027-03-02T11:37:54

DATA(ts18) = ts_ref1->add( iv_day = 1 iv_month = 2 iv_year = 3

                          )->as( xco_cp_time=>format->iso_8601_extended

                          )->value.


"2024-02-02T11:31:03 (note the minute value as a consequence of the value

"passed for second)

DATA(ts19) = ts_ref2->add( iv_hour = 1 iv_minute = 2 iv_second = 4

                          )->as( xco_cp_time=>format->iso_8601_extended

                          )->value.


"Adding an hour to the current time stamp

DATA(ts20) = xco_cp=>sy->moment( )->add( iv_hour = 1

                                  )->as( xco_cp_time=>format->iso_8601_extended

                                  )->value.


"Substractions

"2020-10-31T11:37:54

DATA(ts21) = ts_ref1->subtract( iv_day = 1 iv_month = 2 iv_year = 3

                              )->as( xco_cp_time=>format->iso_8601_extended

                              )->value.


"2024-02-02T09:26:55

DATA(ts22) = ts_ref2->subtract( iv_hour = 1 iv_minute = 2 iv_second = 4

                              )->as( xco_cp_time=>format->iso_8601_extended

                              )->value.


"Substracting 1 year/month/day/hour/minute/second from the curernt time stamp

DATA(ts23) = xco_cp=>sy->moment( )->subtract( iv_day = 1 iv_month = 1 iv_year = 1

                                              iv_hour = 1 iv_minute = 1 iv_second = 1

                                 )->as( xco_cp_time=>format->iso_8601_extended

                                 )->value.


"---- Excursion: Defining a time interval and checking if ----

"---- a time stamp is within that time interval --------------


"In the following example, a time interval is defined from the current time stamp

"retrieved with XCO to a specified one. Using the 'contains' method and providing

"another time stamp reference, it is checked whether that time stamp is contained

"in the time interval. The return value is of type abap_bool. Using the lower_bound

"and upper_bound attributes, the low and high values are retrieved.

DATA(ts_ref3) =  xco_cp_time=>moment( iv_year   = '2030'

                                      iv_month  = '01'

                                      iv_day    = '01'

                                      iv_hour   = '10'

                                      iv_minute = '00'

                                      iv_second = '00' ).


DATA(ts_ref4) =  xco_cp_time=>moment( iv_year   = '2028'

                                      iv_month  = '01'

                                      iv_day    = '01'

                                      iv_hour   = '11'

                                      iv_minute = '00'

                                      iv_second = '00' ).


DATA(ts_interval) = xco_cp=>sy->moment( )->interval_to( ts_ref3 ).

DATA(ts_in) = ts_interval->contains( ts_ref4 ). "If it is contained, the value is 'X'.

DATA(ts_interval_low) = ts_interval->lower_bound->as( xco_cp_time=>format->iso_8601_extended )->value.

DATA(ts_interval_high) = ts_interval->upper_bound->as( xco_cp_time=>format->iso_8601_extended )->value.


Serkan AKKAVAK

Computer Engineer BSc

Head of SAP & Software Department

Contact : serkurumsal@yandex.com