Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

PB/DOS: Convert a Windows FILETIME value to a date string

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • PB/DOS: Convert a Windows FILETIME value to a date string

    The purpose of this code is to convert Windows FILETIME values (QUAD integers) to date strings. I've tested it with a couple of FILETIME values and it seemed to convert them correctly. Let me know in the PB/DOS forum if you find a bug in it.

    This program could be modified to accept dates from the command line, or you can include the functions in your own application.

    N.B. Any time component of the FILETIME value will be ignored.
    Code:
    REM ********************************************************************************
    REM *                        Convert FILETIME to Date/Time                         *
    REM *                                                                              *
    REM * Compiler: PB/DOS                                                             *
    REM * Created: 11/Apr/2002 by Matthew Berg                                         *
    REM ********************************************************************************
    $COMPILE EXE
     
    TYPE structCountryInfo
       nDateFormat AS WORD
       sCurrencySymbol AS ASCIIZ * 5
       sThousandsSeparator AS ASCIIZ * 2
       sDecimalSeparator AS ASCIIZ * 2
       sDateSeparator AS ASCIIZ * 2
       sTimeSeparator AS ASCIIZ * 2
       nCurrencyFormat AS BYTE
       nCurrencyDecimalDigits AS BYTE
       nTimeFormat AS BYTE
       lpCaseMap AS DWORD
       sDataListSeparator AS ASCIIZ * 2
       RESERVED AS STRING * 10
    END TYPE
     
    Begin:
     
    'Declare constants
    %FALSE = 0
    %TRUE = NOT %FALSE
    %AX = 1 : %BX = 2 : %CX = 3 : %DX = 4
    %SI = 5 : %DI = 6 : %BP = 7 : %DS = 8
    %ES = 9 : %FLAGS = 0
     
    GOTO Main
     
    FUNCTION FileTimeConvert(  BYVAL nFileTime AS QUAD, _
                               nDateYear AS WORD, _
                               nDateMonth AS WORD, _
                               nDateDay AS WORD ) AS INTEGER
     
       DIM bRet AS INTEGER
       DIM nA AS DWORD
       DIM nB AS DWORD
       DIM nC AS DWORD
       DIM nD AS DWORD
       DIM nE AS DWORD
       DIM nJulianDay AS DWORD
       DIM nM AS DWORD
     
       LET bRet = %TRUE
     
       'Perform conversion calculations
       LET nJulianDay = nFileTime \ 864000000000&& + 2305814???
       LET nA = nJulianDay + 32044
       LET nB = ( 4 * nA + 3 ) \ 146097
       LET nC = nA - ( 146097 * nB ) \ 4
       LET nD = ( 4 * nC + 3 ) \ 1461
       LET nE = nC - ( 1461 * nD ) \ 4
       LET nM = ( 5 * nE + 2 ) \ 153
     
       'Return date components in function arguments
       LET nDateDay = nE - ( 153 * nM + 2 ) \ 5 + 1
       LET nDateMonth = nM + 3 - 12 * ( nM \ 10 )
       LET nDateYear = 100 * nB + nD - 4800 + ( nM \ 10 )
     
       FUNCTION = bRet
     
    END FUNCTION
     
    FUNCTION DateToString(  BYVAL nDateYear AS WORD, _
                            BYVAL nDateMonth AS WORD, _
                            BYVAL nDateDay AS WORD, _
                            sDate AS STRING ) AS INTEGER
     
       DIM bRet AS INTEGER
       DIM nFlags AS WORD
       DIM oCountryInfo AS structCountryInfo
     
       LET bRet = %TRUE
     
       'Validate input arguments
       IF nDateYear < 1 OR nDateYear > 3267 THEN LET bRet = %FALSE
       IF nDateMonth < 1 OR nDateMonth > 12 THEN LET bRet = %FALSE
       IF nDateDay < 1 OR nDateDay > 31 THEN LET bRet = %FALSE
     
       'Convert date components to string
       IF ISTRUE bRet THEN
          'Get country information
          REG %AX, &H3800
          REG %DS, VARSEG( oCountryInfo )
          REG %DX, VARPTR( oCountryInfo )
          CALL INTERRUPT &H21
     
          LET nFlags = REG( %FLAGS )
          IF ISFALSE( BIT( nFlags, 0 ) ) THEN
             SELECT CASE oCountryInfo.nDateFormat
                CASE 0		' "USA" format - mm/dd/yyyy
                   LET sDate = LTRIM$( STR$( nDateMonth ) ) + oCountryInfo.sDateSeparator + _
                               LTRIM$( STR$( nDateDay ) ) + oCountryInfo.sDateSeparator + _
                               LTRIM$( STR$( nDateYear ) )
                CASE 1		' "European" format - dd/mm/yyyy
                   LET sDate = LTRIM$( STR$( nDateDay ) ) + oCountryInfo.sDateSeparator + _
                               LTRIM$( STR$( nDateMonth ) ) + oCountryInfo.sDateSeparator + _
                               LTRIM$( STR$( nDateYear ) )
                CASE 2		' "Japanese" format - yyyy/mm/dd
                   LET sDate = LTRIM$( STR$( nDateYear ) ) + oCountryInfo.sDateSeparator + _
                               LTRIM$( STR$( nDateMonth ) ) + oCountryInfo.sDateSeparator + _
                               LTRIM$( STR$( nDateDay ) )
             END SELECT
          ELSE
             LET bRet = %FALSE
          END IF
       END IF
     
       FUNCTION = bRet
     
    END FUNCTION
     
    Main:
    DIM nDateDay AS WORD
    DIM nDateMonth AS WORD
    DIM nDateYear AS WORD
    DIM nFileTime AS QUAD
    DIM sDate AS STRING
     
    INPUT "Enter Windows FILETIME value: ", nFileTime
     
    CALL FileTimeConvert( nFileTime, nDateYear, nDateMonth, nDateDay )
    IF DateToString( nDateYear, nDateMonth, nDateDay, sDate ) THEN
       STDOUT "The specified FILETIME value corresponds with the date " + sDate
    ELSE
       STDOUT "Failure!"
    END IF
     
    END
    [This message has been edited by Matthew Berg (edited April 11, 2002).]
    If you try to make something idiot-proof, someone will invent a better idiot.
Working...
X