Globale Datums-Aufbereitung

Hinweise, Tips und Tricks, FAQs - keine Anfragen!!

Globale Datums-Aufbereitung

Postby Tron » Wed Feb 29, 2012 4:41 pm

Insbesondere bei der Formularaufbereitung eines Datums fällt auf, dass die Darstellung von den Benutzereinstellungen abhängig ist. (das ist nicht wirklich schön)

Ich habe eine Konvertierungs-/ Aufbereitungsroutine im SDN ausfindig machen können, welche die Benutzerabhängigkeit "austrickst" und auch ein paar schöne Aufbereitungsoptionen besitzt, die so im Standard nicht vorhanden sind.

Eine Sprachenabhängige Steuerung der Monatsnamen habe ich in die Schnittstelle aufgenommen.

Anders als im SDN-Template, habe ich die Prüfwerte der möglichen Aufbereitungsoptionen
in das Coding übernommen. (siehe ausgesternte Abschnitte)
Wer sicher ist, das Er/Sie sich nicht vertippt, kann den Abschnitt auch völlig streichen.

Die Originalvorlage findet Ihr:
http://wiki.sdn.sap.com/wiki/display/Sn ... VEN+FORMAT

Das neue Coding des Funktionsbausteins:
Code: [Select all] [Expand/Collapse] [Download] (Untitled.txt)
  1. FUNCTION Y_BC_DATE_CONVERT .
  2. *"----------------------------------------------------------------------
  3. *"*"Lokale Schnittstelle:
  4. *"  IMPORTING
  5. *"     REFERENCE(DATE) TYPE  SYDATUM DEFAULT SY-DATUM
  6. *"     REFERENCE(FORMAT) TYPE  CHAR15
  7. *"     REFERENCE(LANGU) TYPE  SYST-LANGU DEFAULT SY-LANGU
  8. *"  EXPORTING
  9. *"     REFERENCE(DATE_FINAL) TYPE  CHAR15
  10. *"  EXCEPTIONS
  11. *"      INCORRECT_FORMAT
  12. *"----------------------------------------------------------------------
  13. * Local variables declaration
  14.   DATA: l_y1         TYPE numc1,
  15.         l_y2         TYPE numc1,
  16.         l_y3         TYPE numc1,
  17.         l_y4         TYPE numc1,
  18.         l_m1         TYPE numc1,
  19.         l_m2         TYPE numc1,
  20.         l_d1         TYPE numc1,
  21.         l_d2         TYPE numc1,
  22.         l_len        TYPE int1,
  23.         l_ctr        TYPE int1,
  24.         w_t247       TYPE t247,
  25.         l_flag_m1    TYPE char1,
  26.         l_flag_init  TYPE char1,
  27.         l_flag_d     TYPE char1,
  28.         l_flag_y     TYPE char1,
  29.         l_format     TYPE char15,
  30.         l_final_date TYPE char15.
  31.  
  32. * Local constants declaration
  33.   CONSTANTS: c_d    TYPE char1 VALUE 'D',
  34.              c_m    TYPE char1 VALUE 'M',
  35.              c_y    TYPE char1 VALUE 'Y',
  36.              c_mmm  TYPE char3 VALUE 'MMM',
  37.              c_yyyy TYPE char4 VALUE 'YYYY'.
  38.  
  39.   PERFORM init_dateref.
  40.  
  41. * Checking the format given by user.
  42.   CLEAR l_format.
  43.   READ TABLE gt_dateformats INTO l_format WITH KEY val = format.
  44.  
  45.  
  46. *  SELECT SINGLE zformat
  47. *    FROM zformat_date
  48. *    INTO l_format
  49. *   WHERE zformat = format.
  50.  
  51.  
  52.   IF sy-subrc EQ 0.
  53. * Processing Logic of the routine
  54.     l_y1 = date+0(1).
  55.     l_y2 = date+1(1).
  56.     l_y3 = date+2(1).
  57.     l_y4 = date+3(1).
  58.     l_m1 = date+4(1).
  59.     l_m2 = date+5(1).
  60.     l_d1 = date+6(1).
  61.     l_d2 = date+7(1).
  62.     l_len = strlen( format ).
  63.     CLEAR : l_flag_m1, l_flag_init, l_flag_d,l_flag_y.
  64.     WHILE l_ctr < l_len.
  65.       IF format+l_ctr(1) = c_m.
  66.         IF NOT format CS c_mmm.
  67.           l_final_date+l_ctr(1) = l_m1.
  68.           l_ctr = l_ctr + 1.
  69.           l_flag_m1 = l_flag_m1 + 1.
  70.           l_final_date+l_ctr(1) = l_m2.
  71.           l_ctr = l_ctr + 1.
  72.           l_flag_m1 = l_flag_m1 + 1.
  73.         ELSE.
  74. *          SELECT SINGLE * FROM t247 INTO w_t247
  75. *             WHERE spras = sy-langu
  76. *               AND  mnr  = date+4(2).
  77.           SELECT SINGLE * FROM t247 INTO w_t247
  78.              WHERE spras = langu
  79.                AND  mnr  = date+4(2).
  80.           IF sy-subrc EQ 0.
  81.             l_final_date+l_ctr(3) = w_t247-ktx.
  82.             l_ctr = l_ctr + 3.
  83.           ENDIF.
  84.         ENDIF.
  85.       ELSEIF format+l_ctr(1) = c_y.
  86.         IF format CS c_yyyy.
  87.           l_final_date+l_ctr(1) = l_y1.
  88.           l_ctr = l_ctr + 1.
  89.           l_final_date+l_ctr(1) = l_y2.
  90.           l_ctr = l_ctr + 1.
  91.           l_final_date+l_ctr(1) = l_y3.
  92.           l_ctr = l_ctr + 1.
  93.           l_final_date+l_ctr(1) = l_y4.
  94.           l_ctr = l_ctr + 1.
  95.         ELSE.
  96.           l_final_date+l_ctr(1) = l_y3.
  97.           l_ctr = l_ctr + 1.
  98.           l_flag_y = l_flag_y + 1.
  99.           l_final_date+l_ctr(1) = l_y4.
  100.           l_ctr = l_ctr + 1.
  101.           l_flag_y = l_flag_y + 1.
  102.         ENDIF.
  103.       ELSEIF format+l_ctr(1) = c_d.
  104.         l_final_date+l_ctr(1) = l_d1.
  105.         l_ctr = l_ctr + 1.
  106.         l_flag_d = l_flag_d + 1.
  107.         l_final_date+l_ctr(1) = l_d2.
  108.         l_ctr = l_ctr + 1.
  109.         l_flag_d = l_flag_d + 1.
  110.       ELSE.
  111.         l_final_date+l_ctr(1) = format+l_ctr(1).
  112.         l_ctr = l_ctr + 1.
  113.       ENDIF.
  114.       IF NOT l_final_date IS INITIAL.
  115.         date_final = l_final_date.
  116.       ENDIF.
  117.     ENDWHILE.
  118.   ELSE.
  119.     CLEAR date_final.
  120.     RAISE incorrect_format.
  121.   ENDIF.
  122.  
  123.  
  124.  
  125. *&---------------------------------------------------------------------*
  126. *&      Form  init_dateref
  127. *&---------------------------------------------------------------------*
  128. *       text
  129. *----------------------------------------------------------------------*
  130. FORM init_dateref.
  131.  
  132.   CHECK gt_dateformats[] IS INITIAL.
  133.  
  134.   APPEND 'DD.MM.YY' TO gt_dateformats.
  135.   APPEND 'MM.DD.YY' TO gt_dateformats.
  136.   APPEND 'MM.YY.DD' TO gt_dateformats.
  137.   APPEND 'YY.MM.DD' TO gt_dateformats.
  138.   APPEND 'YY.DD.MM' TO gt_dateformats.
  139.   APPEND 'DD.YY.MM' TO gt_dateformats.
  140.   APPEND 'DD-MM-YY' TO gt_dateformats.
  141.   APPEND 'MM-DD-YY' TO gt_dateformats.
  142.   APPEND 'MM-YY-DD' TO gt_dateformats.
  143.   APPEND 'YY-MM-DD' TO gt_dateformats.
  144.   APPEND 'YY-DD-MM' TO gt_dateformats.
  145.   APPEND 'DD-YY-MM' TO gt_dateformats.
  146.   APPEND 'DD/MM/YY' TO gt_dateformats.
  147.   APPEND 'MM/DD/YY' TO gt_dateformats.
  148.   APPEND 'MM/YY/DD' TO gt_dateformats.
  149.   APPEND 'YY/MM/DD' TO gt_dateformats.
  150.   APPEND 'YY/DD/MM' TO gt_dateformats.
  151.   APPEND 'DD/YY/MM' TO gt_dateformats.
  152.   APPEND 'DDMMYY' TO gt_dateformats.
  153.   APPEND 'MMDDYY' TO gt_dateformats.
  154.   APPEND 'MMYYDD' TO gt_dateformats.
  155.   APPEND 'YYMMDD' TO gt_dateformats.
  156.   APPEND 'YYDDMM' TO gt_dateformats.
  157.   APPEND 'DDYYMM' TO gt_dateformats.
  158.   APPEND 'DD.MMM.YY' TO gt_dateformats.
  159.   APPEND 'MMM.DD.YY' TO gt_dateformats.
  160.   APPEND 'MMM.YY.DD' TO gt_dateformats.
  161.   APPEND 'YY.MMM.DD' TO gt_dateformats.
  162.   APPEND 'YY.DD.MMM' TO gt_dateformats.
  163.   APPEND 'DD.YY.MMM' TO gt_dateformats.
  164.   APPEND 'DD-MMM-YY' TO gt_dateformats.
  165.   APPEND 'MMM-DD-YY' TO gt_dateformats.
  166.   APPEND 'MMM-YY-DD' TO gt_dateformats.
  167.   APPEND 'YY-MMM-DD' TO gt_dateformats.
  168.   APPEND 'YY-DD-MMM' TO gt_dateformats.
  169.   APPEND 'DD-YY-MMM' TO gt_dateformats.
  170.   APPEND 'DD/MMM/YY' TO gt_dateformats.
  171.   APPEND 'MMM/DD/YY' TO gt_dateformats.
  172.   APPEND 'MMM/YY/DD' TO gt_dateformats.
  173.   APPEND 'YY/MMM/DD' TO gt_dateformats.
  174.   APPEND 'YY/DD/MMM' TO gt_dateformats.
  175.   APPEND 'DD/YY/MMM' TO gt_dateformats.
  176.   APPEND 'DDMMMYY' TO gt_dateformats.
  177.   APPEND 'MMMDDYY' TO gt_dateformats.
  178.   APPEND 'MMMYYDD' TO gt_dateformats.
  179.   APPEND 'YYMMMDD' TO gt_dateformats.
  180.   APPEND 'YYDDMMM' TO gt_dateformats.
  181.   APPEND 'DDYYMMM' TO gt_dateformats.
  182.   APPEND 'DD.MM.YYYY' TO gt_dateformats.
  183.   APPEND 'MM.DD.YYYY' TO gt_dateformats.
  184.   APPEND 'MM.YYYY.DD' TO gt_dateformats.
  185.   APPEND 'YYYY.MM.DD' TO gt_dateformats.
  186.   APPEND 'YYYY.DD.MM' TO gt_dateformats.
  187.   APPEND 'DD.YYYY.MM' TO gt_dateformats.
  188.   APPEND 'DD-MM-YYYY' TO gt_dateformats.
  189.   APPEND 'MM-DD-YYYY' TO gt_dateformats.
  190.   APPEND 'MM-YYYY-DD' TO gt_dateformats.
  191.   APPEND 'YYYY-MM-DD' TO gt_dateformats.
  192.   APPEND 'YYYY-DD-MM' TO gt_dateformats.
  193.   APPEND 'DD-YYYY-MM' TO gt_dateformats.
  194.   APPEND 'DD/MM/YYYY' TO gt_dateformats.
  195.   APPEND 'MM/DD/YYYY' TO gt_dateformats.
  196.   APPEND 'MM/YYYY/DD' TO gt_dateformats.
  197.   APPEND 'YYYY/MM/DD' TO gt_dateformats.
  198.   APPEND 'YYYY/DD/MM' TO gt_dateformats.
  199.   APPEND 'DD/YYYY/MM' TO gt_dateformats.
  200.   APPEND 'DDMMYYYY' TO gt_dateformats.
  201.   APPEND 'MMDDYYYY' TO gt_dateformats.
  202.   APPEND 'MMYYYYDD' TO gt_dateformats.
  203.   APPEND 'YYYYMMDD' TO gt_dateformats.
  204.   APPEND 'YYYYDDMM' TO gt_dateformats.
  205.   APPEND 'DDYYYYMM' TO gt_dateformats.
  206.   APPEND 'DD.MMM.YYYY' TO gt_dateformats.
  207.   APPEND 'MMM.DD.YYYY' TO gt_dateformats.
  208.   APPEND 'MMM.YYYY.DD' TO gt_dateformats.
  209.   APPEND 'YYYY.MMM.DD' TO gt_dateformats.
  210.   APPEND 'YYYY.DD.MMM' TO gt_dateformats.
  211.   APPEND 'DD.YYYY.MMM' TO gt_dateformats.
  212.   APPEND 'DD-MMM-YYYY' TO gt_dateformats.
  213.   APPEND 'MMM-DD-YYYY' TO gt_dateformats.
  214.   APPEND 'MMM-YYYY-DD' TO gt_dateformats.
  215.   APPEND 'YYYY-MMM-DD' TO gt_dateformats.
  216.   APPEND 'YYYY-DD-MMM' TO gt_dateformats.
  217.   APPEND 'DD-YYYY-MMM' TO gt_dateformats.
  218.   APPEND 'DD/MMM/YYYY' TO gt_dateformats.
  219.   APPEND 'MMM/DD/YYYY' TO gt_dateformats.
  220.   APPEND 'MMM/YYYY/DD' TO gt_dateformats.
  221.   APPEND 'YYYY/MMM/DD' TO gt_dateformats.
  222.   APPEND 'YYYY/DD/MMM' TO gt_dateformats.
  223.   APPEND 'DD/YYYY/MMM' TO gt_dateformats.
  224.   APPEND 'DDMMMYYYY' TO gt_dateformats.
  225.   APPEND 'MMMDDYYYY' TO gt_dateformats.
  226.   APPEND 'MMMYYYYDD' TO gt_dateformats.
  227.   APPEND 'YYYYMMMDD' TO gt_dateformats.
  228.   APPEND 'YYYYDDMMM' TO gt_dateformats.
  229.   APPEND 'DDYYYYMMM' TO gt_dateformats.
  230.  
  231. ENDFORM.                    "init_dateref
GeSHi ©


Muß in den Top-Include
Code: [Select all] [Expand/Collapse] [Download] (Untitled.txt)
  1. types: begin of t_dateformats,
  2.        val type char15,
  3.        end of t_dateformats.
  4.  
  5. DATA gt_dateformats TYPE STANDARD TABLE OF t_dateformats.
  6.  
GeSHi ©


Ein kleines Testprogramm:
Code: [Select all] [Expand/Collapse] [Download] (Untitled.txt)
  1. *&---------------------------------------------------------------------*
  2. *& Report  YBC_TEST_DATECONVERT
  3. *&
  4. *&---------------------------------------------------------------------*
  5. *&
  6. *&
  7. *&---------------------------------------------------------------------*
  8.  
  9. REPORT  YBC_TEST_DATECONVERT.
  10.  
  11. data f1 type char15 value 'DD-MMM-YY'.
  12. data f2 type char15 value 'YY.MM.DD'.
  13. data f3 type char15 value 'YY.DD.MM'.
  14.  
  15.  
  16. data df type char15.
  17.  
  18. break bcuser.
  19. CALL FUNCTION 'Y_BC_DATE_CONVERT'
  20.   EXPORTING
  21. *   DATE                   = SY-DATUM
  22.     format                 = f1
  23.  IMPORTING
  24.    DATE_FINAL             = df
  25.  EXCEPTIONS
  26.    INCORRECT_FORMAT       = 1
  27.    OTHERS                 = 2.
  28.  
  29. break bcuser.
  30.  
  31. CALL FUNCTION 'Y_BC_DATE_CONVERT'
  32.   EXPORTING
  33. *   DATE                   = SY-DATUM
  34.     format                 = f2
  35.  IMPORTING
  36.    DATE_FINAL             = df
  37.  EXCEPTIONS
  38.    INCORRECT_FORMAT       = 1
  39.    OTHERS                 = 2.
  40.  
  41. break bcuser.
  42.  
  43. CALL FUNCTION 'Y_BC_DATE_CONVERT'
  44.   EXPORTING
  45. *   DATE                   = SY-DATUM
  46.     format                 = f3
  47.  IMPORTING
  48.    DATE_FINAL             = df
  49.  EXCEPTIONS
  50.    INCORRECT_FORMAT       = 1
  51.    OTHERS                 = 2.
  52.  
  53. break bcuser.
GeSHi ©


Der Einsatz in Smartforms:
Der Aufruf als globale FORM-Routine
Image

Ergebnis:
Image

viel Freude damit
lg Jens
Tron
.....
.....
 
Posts: 1112
Joined: Sat Aug 04, 2007 10:21 pm

Re: Globale Datums-Aufbereitung

Postby Tron » Mon Mar 05, 2012 10:10 am

...Style vergessen :oops:
Im älteren SAP 4.7 gibt es den Datentyp NUMC1 nicht daher:
Code: [Select all] [Expand/Collapse] [Download] (Untitled.txt)
  1. * Local variables declaration
  2.   DATA: l_y1         TYPE num1,
  3.         l_y2         TYPE num1,
  4.         l_y3         TYPE num1,
  5.         l_y4         TYPE num1,
  6.         l_m1         TYPE num1,
  7.         l_m2         TYPE num1,
  8.         l_d1         TYPE num1,
  9.         l_d2         TYPE num1,
  10.         l_len        TYPE int1,
  11.         l_ctr        TYPE int1,
  12.         w_t247       TYPE t247,
  13.         l_flag_m1    TYPE char1,
  14.         l_flag_init  TYPE char1,
  15.         l_flag_d     TYPE char1,
  16.         l_flag_y     TYPE char1,
  17.         l_format     TYPE char15,
  18.         l_final_date TYPE char15.
  19.  
GeSHi ©

gruß Jens
Tron
.....
.....
 
Posts: 1112
Joined: Sat Aug 04, 2007 10:21 pm


Return to Tips + Tricks & FAQs

Who is online

Users browsing this forum: No registered users and 4 guests