Nutzergesteuerte Datum/Zeit-Ausgabe

    • Offizieller Beitrag

    Folgendes ist bei einem Projekt von mit angefallen:

    Der Nutzer soll vollkommen frei und für ihn verständlich Tagesdatum/Zeit oder beliebige Elemente daraus formatieren können.
    Folgende Makros stehen zur Verfügung:
    YYYY / YY - Jahr 4/2 -stellig
    MM / M - Monat 2/1 -stellig
    DD / D - Tag 2/1 -stellig
    hh / h - Stunde 2/1 -stellig
    mm / m - Minute 2/1 -stellig
    ss / s - Sekunde 2/1 -stellig
    ww / w - Wochentag lang/kurz (Montag/Mo)
    Bsp. "YYYY-MM-DD", "YYYYMMDD", "D.M.YY", "ww, DD.MM.YYYY", "DD.MM.YYYY hh:mm:ss" (Standard)
    Einstellig bedeutet, wenn einstelliger Wert, wird er auch einstellig ausgegeben. Zweistellig fügt dann eine Vornull an.
    Optional gibt es die Möglichkeit einen String vorne und/oder hinten anzufügen.
    Die vom User gewünschte Formatierung kann z.B. aus einer INI eingelesen und von diesem intuitiv angepasst werden.

    Spoiler anzeigen
    [autoit]

    ;===============================================================================
    ; Function Name....: _DateTimeUser
    ; Description......: Nutzergesteuerte Datum- und/oder Zeitausgabe
    ; Parameter(s).....: $_time_format Nutzerdefiniertes Ausgabeformat
    ; .................: Makros YYYY / YY - Jahr 4/2 -stellig
    ; .................: MM / M - Monat 2/1 -stellig
    ; .................: DD / D - Tag 2/1 -stellig
    ; .................: hh / h - Stunde 2/1 -stellig
    ; .................: mm / m - Minute 2/1 -stellig
    ; .................: ss / s - Sekunde 2/1 -stellig
    ; .................: ww / w - Wochentag lang/kurz (Montag/Mo)
    ; .................: Bsp. "YYYY-MM-DD", "YYYYMMDD", "D.M.YY", "DD.MM.YYYY hh:mm:ss" (Standard)
    ; .................: "ww, DD.MM.YYYY"
    ; .................: Auch einzelne Werte und beliebige Kombinationen können abgefragt werden. "DD-hh-mm"
    ; ....[optional]...: $_pre vorangestellter String
    ; ....[optional]...: $_post nachgestellter String
    ; Return Value(s)..: Der formatierte Datum/Zeitstring für das heutige Datum
    ; Author(s)........: BugFix ( [email='bugfix@autoit.de'][/email] )
    ;===============================================================================
    Func _DateTimeUser($_time_format='', $_pre='', $_post='')
    $_time_format = ($_time_format = '') ? 'DD.MM.YYYY hh:mm:ss' : $_time_format
    Local $aTime[10][2] = [['MM', @MON], ['DD', @MDAY], ['hh', @HOUR], ['mm', @MIN], ['ss', @SEC], _
    ['M', @MON], ['D', @MDAY], ['h', @HOUR], ['m', @MIN], ['s', @SEC]]
    StringReplace($_time_format, 'Y', 'Y')
    Local $sYear = StringLeft('YYYY', @extended), $output = $_time_format
    Local $srepl = (StringLen($sYear) < 4) ? StringRight(@YEAR, 2) : @YEAR
    If $sYear <> '' Then _
    $output = StringReplace($_time_format, $sYear, $srepl)
    Local $sformat
    For $i = 0 To 9
    $sformat = '%0' & StringLen($aTime[$i][0]) & 'd'
    $output = StringReplace($output, $aTime[$i][0], StringFormat($sformat, $aTime[$i][1]), 1, 1)
    Next
    If StringInStr($output, 'w') Then
    Local $YY = StringRight(@YEAR, 2), $aMonval[13] = ['',0,3,3,6,1,4,6,2,5,0,3,5], $iDiff
    Local $Y_val = Mod(($YY + Int($YY/4)), 7), $M_val = $aMonval[@MON], $D_val = @MDAY
    Local $aY_val[7][2] = [[2300,0],[2200,2],[2100,4],[2000,6],[1900,0],[1800,2],[1700,4]]
    For $i = 0 To 6
    If @YEAR >= $aY_val[$i][0] Then
    $iDiff = $aY_val[$i][1]
    ExitLoop
    EndIf
    Next
    If ((Mod(@YEAR,400)=0)?1:(Mod(@YEAR,100)=0)?0:(Mod(@YEAR,4)=0)?1:0) Then $iDiff -= 1
    Local $aWDay[7][2] = [['Sonntag','So'],['Montag','Mo'],['Dienstag','Di'],['Mittwoch','Mi'], _
    ['Donnerstag','Do'],['Freitag','Fr'],['Samstag','Sa']]
    Local $w = Mod($Y_val + $M_val + $D_val + $iDiff, 7)
    $output = StringReplace($output, 'ww', $aWDay[$w][0], 1, 1)
    $output = StringReplace($output, 'w', $aWDay[$w][1], 1, 1)
    EndIf
    Return $_pre & $output & $_post
    EndFunc ;==>_DateTimeUser

    [/autoit]

    Die Wochentags- und Schaltjahrermittlung habe ich bewußt ohne externe Funktionen berechnet, damit diese Funktion auch ohne Includes auskommt.

    • Offizieller Beitrag

    Eine schöne Funktion, aber ich würde die Makros nur optional verwenden. Den Aufruf lieber auch mit beliebigen Daten zulassen.
    Also so:

    Spoiler anzeigen
    [autoit]


    ConsoleWrite(_DateTimeUser('ww DD.MM.YYYY hh:mm:ss', '', '', 2014, 1, 1, 0, 0, 0) & @CR)

    [/autoit] [autoit][/autoit] [autoit]

    ;===============================================================================
    ; Function Name....: _DateTimeUser
    ; Description......: Nutzergesteuerte Datum- und/oder Zeitausgabe
    ; Parameter(s).....: $_time_format Nutzerdefiniertes Ausgabeformat
    ; .................: Makros YYYY / YY - Jahr 4/2 -stellig
    ; .................: MM / M - Monat 2/1 -stellig
    ; .................: DD / D - Tag 2/1 -stellig
    ; .................: hh / h - Stunde 2/1 -stellig
    ; .................: mm / m - Minute 2/1 -stellig
    ; .................: ss / s - Sekunde 2/1 -stellig
    ; .................: ww / w - Wochentag lang/kurz (Montag/Mo)
    ; .................: Bsp. "YYYY-MM-DD", "YYYYMMDD", "D.M.YY", "DD.MM.YYYY hh:mm:ss" (Standard)
    ; .................: "ww, DD.MM.YYYY"
    ; .................: Auch einzelne Werte und beliebige Kombinationen können abgefragt werden. "DD-hh-mm"
    ; ....[optional]...: $_pre vorangestellter String
    ; ....[optional]...: $_post nachgestellter String
    ; Return Value(s)..: Der formatierte Datum/Zeitstring für das heutige Datum
    ; Author(s)........: BugFix ( [email='bugfix@autoit.de'][/email] )
    ;===============================================================================
    Func _DateTimeUser($_time_format='', $_pre='', $_post='', $iYear = @YEAR, $iMon = @MON, $iDay = @MDAY, $iHour = @HOUR, $iMin = @MIN, $iSec = @SEC)
    $_time_format = ($_time_format = '') ? 'DD.MM.YYYY hh:mm:ss' : $_time_format
    Local $aTime[10][2] = [['MM', $iMon], ['DD', $iDay], ['hh', $iHour], ['mm', $iMin], ['ss', $iSec], _
    ['M', $iMon], ['D', $iDay], ['h', $iHour], ['m', $iMin], ['s', $iSec]]
    StringReplace($_time_format, 'Y', 'Y')
    Local $sYear = StringLeft('YYYY', @extended), $output = $_time_format
    Local $srepl = (StringLen($sYear) < 4) ? StringRight($iYear, 2) : $iYear
    If $sYear <> '' Then _
    $output = StringReplace($_time_format, $sYear, $srepl)
    Local $sformat
    For $i = 0 To 9
    $sformat = '%0' & StringLen($aTime[$i][0]) & 'd'
    $output = StringReplace($output, $aTime[$i][0], StringFormat($sformat, $aTime[$i][1]), 1, 1)
    Next
    If StringInStr($output, 'w') Then
    Local $YY = StringRight($iYear, 2), $aMonval[13] = ['',0,3,3,6,1,4,6,2,5,0,3,5], $iDiff
    Local $Y_val = Mod(($YY + Int($YY/4)), 7), $M_val = $aMonval[$iMon], $D_val = $iDay
    Local $aY_val[7][2] = [[2300,0],[2200,2],[2100,4],[2000,6],[1900,0],[1800,2],[1700,4]]
    For $i = 0 To 6
    If $iYear >= $aY_val[$i][0] Then
    $iDiff = $aY_val[$i][1]
    ExitLoop
    EndIf
    Next
    If ((Mod($iYear,400)=0)?1:(Mod($iYear,100)=0)?0:(Mod($iYear,4)=0)?1:0) Then $iDiff -= 1
    Local $aWDay[7][2] = [['Sonntag','So'],['Montag','Mo'],['Dienstag','Di'],['Mittwoch','Mi'], _
    ['Donnerstag','Do'],['Freitag','Fr'],['Samstag','Sa']]
    Local $w = Mod($Y_val + $M_val + $D_val + $iDiff, 7)
    $output = StringReplace($output, 'ww', $aWDay[$w][0], 1, 1)
    If Not @extended Then $output = StringReplace($output, 'w', $aWDay[$w][1], 1, 1)
    EndIf
    Return $_pre & $output & $_post
    EndFunc ;==>_DateTimeUser

    [/autoit]


    Die Zeile 51 habe ich noch um

    [autoit]

    If Not @extended Then

    [/autoit]


    ergänzt, weil sonst beim Mittwoch das "w" nochmal durch "Mi" ersetzt wird.

    • Offizieller Beitrag

    Ich dachte mir, die Funktion kann man doch auch etwas internationaler gestalten und da ich das ja schonmal gemacht habe (_DateDayOfWeekML),
    habe ich Deine Funktion diesbezüglich noch ein wenig verändert:

    Spoiler anzeigen
    [autoit]


    ConsoleWrite('Italian: ' & _DateTimeUser('ww DD.MM.YYYY hh:mm:ss', '', '', 2014, 1, 1, 0, 0, 0, "0410") & @CR)
    ConsoleWrite('German: ' & _DateTimeUser('ww DD.MM.YYYY hh:mm:ss', '', '', 2014, 1, 1, 0, 0, 0, "0407") & @CR)
    ConsoleWrite('English: ' & _DateTimeUser('ww DD.MM.YYYY hh:mm:ss', '', '', 2014, 1, 1, 0, 0, 0, "0409") & @CR)
    ConsoleWrite('French: ' & _DateTimeUser('ww DD.MM.YYYY hh:mm:ss', '', '', 2014, 1, 1, 0, 0, 0, "040c") & @CR)
    ConsoleWrite('Norwegian: ' & _DateTimeUser('ww DD.MM.YYYY hh:mm:ss', '', '', 2014, 1, 1, 0, 0, 0, "0414") & @CR)

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    ;===============================================================================
    ; Function Name....: _DateTimeUser
    ; Description......: Nutzergesteuerte Datum- und/oder Zeitausgabe
    ; Parameter(s).....: $_time_format Nutzerdefiniertes Ausgabeformat
    ; .................: Makros YYYY / YY - Jahr 4/2 -stellig
    ; .................: MM / M - Monat 2/1 -stellig
    ; .................: DD / D - Tag 2/1 -stellig
    ; .................: hh / h - Stunde 2/1 -stellig
    ; .................: mm / m - Minute 2/1 -stellig
    ; .................: ss / s - Sekunde 2/1 -stellig
    ; .................: ww / w - Wochentag lang/kurz (Montag/Mo)
    ; .................: Bsp. "YYYY-MM-DD", "YYYYMMDD", "D.M.YY", "DD.MM.YYYY hh:mm:ss" (Standard)
    ; .................: "ww, DD.MM.YYYY"
    ; .................: Auch einzelne Werte und beliebige Kombinationen können abgefragt werden. "DD-hh-mm"
    ; ....[optional]...: $_pre vorangestellter String
    ; ....[optional]...: $_post nachgestellter String
    ; Return Value(s)..: Der formatierte Datum/Zeitstring für das heutige Datum
    ; Author(s)........: BugFix ( [email='bugfix@autoit.de'][/email] )
    ;===============================================================================
    Func _DateTimeUser($_time_format = '', $_pre = '', $_post = '', $iYear = @YEAR, $iMon = @MON, $iDay = @MDAY, $iHour = @HOUR, $iMin = @MIN, $iSec = @SEC, $sLang = @OSLang)
    $_time_format = ($_time_format = '') ? 'DD.MM.YYYY hh:mm:ss' : $_time_format
    Local $aTime[10][2] = [['MM', $iMon], ['DD', $iDay], ['hh', $iHour], ['mm', $iMin], ['ss', $iSec], _
    ['M', $iMon], ['D', $iDay], ['h', $iHour], ['m', $iMin], ['s', $iSec]]
    StringReplace($_time_format, 'Y', 'Y')
    Local $sYear = StringLeft('YYYY', @extended), $output = $_time_format
    Local $srepl = (StringLen($sYear) < 4) ? StringRight($iYear, 2) : $iYear
    If $sYear <> '' Then _
    $output = StringReplace($_time_format, $sYear, $srepl)
    Local $sformat
    For $i = 0 To 9
    $sformat = '%0' & StringLen($aTime[$i][0]) & 'd'
    $output = StringReplace($output, $aTime[$i][0], StringFormat($sformat, $aTime[$i][1]), 1, 1)
    Next
    If StringInStr($output, 'w') Then
    Local $sDayFormat = StringInStr($_time_format, 'ww') ? 'dddd' : 'ddd'
    Local $tDATE = DllStructCreate('word Year;word Month;word Dow;word Day;word Hour;word Minute;word Second;word MSeconds')
    DllStructSetData($tDATE, 'Year', $iYear)
    DllStructSetData($tDATE, 'Month', $iMon)
    DllStructSetData($tDATE, 'Day', $iDay)
    Local $Ret = DllCall('kernel32.dll', 'int', 'GetDateFormatW', 'ulong', Dec($sLang), 'dword', 0, 'ptr', DllStructGetPtr($tDATE), 'wstr', $sDayFormat, 'wstr', '', 'int', 2048)
    If @error Or Not $Ret[0] Then Return SetError(1, 0, '')
    Local $sWDay = $Ret[5]
    $output = StringReplace($output, 'ww', $sWDay, 1, 1)
    If Not @extended Then $output = StringReplace($output, 'w', $sWDay, 1, 1)
    EndIf
    Return $_pre & $output & $_post
    EndFunc ;==>_DateTimeUser

    [/autoit]