All files / src/git zcl_abapgit_git_time.clas.abap

94.87% Statements 148/156
20% Branches 1/5
100% Functions 1/1
94.87% Lines 148/156

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 1571x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x     1x 1x 1x 1x 1x 1x     1x 1x     1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x     1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
CLASS zcl_abapgit_git_time DEFINITION
  PUBLIC
  FINAL
  CREATE PUBLIC .
 
  PUBLIC SECTION.
 
    TYPES:
      ty_unixtime TYPE zif_abapgit_git_definitions=>ty_unixtime .
 
    CLASS-METHODS get_unix
      RETURNING
        VALUE(rv_time) TYPE ty_unixtime
      RAISING
        zcx_abapgit_exception .
 
    CLASS-METHODS get_unix_days_ago
      IMPORTING
        !iv_days       TYPE i
      RETURNING
        VALUE(rv_time) TYPE i
      RAISING
        zcx_abapgit_exception .
 
    CLASS-METHODS get_unix_from_local
      IMPORTING
        !iv_date       TYPE d
        !iv_time       TYPE t
      RETURNING
        VALUE(rv_time) TYPE ty_unixtime
      RAISING
        zcx_abapgit_exception .
 
    CLASS-METHODS get_utc
      IMPORTING
        !iv_unix TYPE ty_unixtime
      EXPORTING
        !ev_date TYPE sy-datum
        !ev_time TYPE sy-uzeit .
  PROTECTED SECTION.
  PRIVATE SECTION.
ENDCLASS.
 
 
 
CLASS zcl_abapgit_git_time IMPLEMENTATION.
 
 
  METHOD get_unix_days_ago.
* https://www.epochconverter.com
    CONSTANTS lc_seconds_per_day TYPE i VALUE 86400.
    CONSTANTS lc_epoch TYPE timestamp VALUE '19700101000000'.
    DATA lv_time TYPE timestamp.
 
    GET TIME STAMP FIELD lv_time.
 
    rv_time = cl_abap_tstmp=>subtract(
      tstmp1 = lv_time
      tstmp2 = lc_epoch ).
 
    IF iv_days <= 0 OR iv_days > 24855.
      zcx_abapgit_exception=>raise( |Invalid iv_days: { iv_days }| ).
    ENDIF.
 
    rv_time = rv_time - iv_days * lc_seconds_per_day.
  ENDMETHOD.
 
 
  METHOD get_unix.
* returns seconds since Unix epoch, including timezone indicator
 
    CONSTANTS lc_epoch TYPE timestamp VALUE '19700101000000'.
    DATA lv_time TYPE timestamp.
    DATA lv_seconds TYPE i.
 
    GET TIME STAMP FIELD lv_time.
 
    lv_seconds = cl_abap_tstmp=>subtract(
      tstmp1 = lv_time
      tstmp2 = lc_epoch ).
 
    rv_time = lv_seconds.
    CONDENSE rv_time.
    rv_time+11 = '+0000'.
 
  ENDMETHOD.
 
 
  METHOD get_unix_from_local.
* returns seconds since Unix epoch, including timezone indicator
* the input is expected to be in the time zone of the application server,
* like SY-DATUM and SY-UZEIT and like most date and time fields on the
* database, eg E070-AS4DATE and E070-AS4TIME, it is converted to UTC, so
* the timezone indicator is always '+0000', same layout as GET_UNIX returns
* note SY-ZONLO is the personal time zone of the user, it can be empty,
* eg in background jobs, so it cannot be used here
 
    CONSTANTS lc_epoch TYPE timestamp VALUE '19700101000000'.
 
    DATA lv_seconds   TYPE i.
    DATA lv_timestamp TYPE timestamp.
    DATA lv_timezone  TYPE timezone.
 
    IF iv_date IS INITIAL.
      zcx_abapgit_exception=>raise( 'Cannot determine unix time, date is initial' ).
    ENDIF.
 
    lv_timezone = zcl_abapgit_time_date=>get_system_timezone( ).
 
    CONVERT DATE iv_date TIME iv_time
      INTO TIME STAMP lv_timestamp TIME ZONE lv_timezone.
    IF sy-subrc <> 0.
      zcx_abapgit_exception=>raise( |Unknown time zone "{ lv_timezone }"| ).
    ENDIF.
 
    IF lv_timestamp < lc_epoch.
      zcx_abapgit_exception=>raise( |Date { iv_date } is before the unix epoch| ).
    ENDIF.
 
    lv_seconds = cl_abap_tstmp=>subtract(
      tstmp1 = lv_timestamp
      tstmp2 = lc_epoch ).
 
    rv_time = |{ lv_seconds } +0000|.
 
  ENDMETHOD.
 
 
  METHOD get_utc.
 
    CONSTANTS lc_epoch TYPE d VALUE '19700101'.
 
    DATA: lv_i       TYPE i,
          lv_utcdiff TYPE t,
          lv_utcsign TYPE c LENGTH 1.
 
 
    lv_i = iv_unix(10).
    lv_utcsign = iv_unix+11.
    lv_utcdiff = iv_unix+12.
 
    " GMT + time-zone
    CASE lv_utcsign.
      WHEN '+'.
        lv_i = lv_i + lv_utcdiff.
      WHEN '-'.
        lv_i = lv_i - lv_utcdiff.
    ENDCASE.
 
    ev_time = lv_i MOD 86400.
    lv_i = lv_i - ev_time.
    lv_i = lv_i / 86400.
    ev_date = lv_i + lc_epoch.
 
  ENDMETHOD.
ENDCLASS.