'===========================================================================
' Subject: GET/SET FILES DATE/TIME            Date: 07-02-95 (00:00)       
'  Author: Christy Gemmell                    Code: PB                     
'  Origin: GET,SET,FILE,DATE,TIME           Packet: PB.ABC
'===========================================================================
' FILEDATE.BAS  get and set a files date and time stamps.
'
'   Author:     Christy Gemmell
'   Date:       3/7/1995
'   Compiler:   PowerBASIC
'
    DECLARE FUNCTION GetDateFormat% ()
    DECLARE FUNCTION GetFileDate$ (FileName$)
    DECLARE SUB SetFileDate (FileName$, FileDate$, Fmt%, Done%)

    CLS : PRINT : FileName$ = "PB.EXE"
    OldDate$ = GetFileDate$(FileName$)
    IF OldDate$ <> "" THEN
       PRINT FileName$; " is currently dated "; OldDate$
       PRINT
       NewDate$ = LEFT$(DATE$, 6) + MID$(DATE$, 9, 2) + "  " + TIME$
       PRINT "Setting file to current date and time... ";
       CALL SetFileDate(FileName$, NewDate$, -1, Done%)
       IF Done% THEN
          PRINT "done"
          NewDate$ = GetFileDate$(FileName$)
          PRINT
          PRINT FileName$; " is now dated "; NewDate$
          PRINT
          PRINT "Now reverting back to previous setting... ";
          CALL SetFileDate(FileName$, OldDate$, 0, Done%)
          IF Done% THEN
             PRINT "done"
             DateNow$ = GetFileDate$(FileName$)
             PRINT
             PRINT FileName$; " is now dated "; DateNow$
          ELSE
             PRINT "failed!"
          END IF
       ELSE
          PRINT "failed!"
       END IF
    END IF
END

'   Returns a code indicating the national date format.
'
'   Return values:  1 = MM-DD-YY   (USA)
'                   2 = DD/MM/YY   (Europe)
'                   3 = YY-MM-DD   (Japan)
'
'   Depends on COUNTRY = setting in CONFIG.SYS (default = USA)
'
FUNCTION GetDateFormat%
    B$ = SPACE$(64)                     ' To hold country information
    REG 8, STRSEG(B$)                   ' DS = segment of buffer
    REG 4, STRPTR(B$)                   ' DX = offset of buffer
    REG 1, &H3800                       ' DOS Service 56
    CALL INTERRUPT &H21                 ' - get country information
    GetDateFormat% = ASC(B$)            ' Date format is first byte
END FUNCTION

'   Returns date and time a file was last updated.
'
'   The date and time are returned as a string in one of these formats:
'
'       --123456789012345678--
'
'         MM-DD-YY  HH:MM:SS    (for USA)
'         DD/MM/YY  HH:MM:SS    (for Europe)
'         YY-MM-DD  HH:MM:SS    (for Japan)
'
'   (there are two blank spaces between the date and time
'
FUNCTION GetFileDate$ (FileName$)
    Dt$ = ""                            ' Assume failure
    F$ = FileName$ + CHR$(0)            ' Make filespec ASCIIZ
    REG 8, STRSEG(F$)                   ' DS = segment of filespec
    REG 4, STRPTR(F$)                   ' DX = offset of filespec
    REG 1, &H3D00                       ' DOS Service 61
    CALL INTERRUPT &H21                 ' - open file for reading
    Carry% = REG(0) AND 1               ' Check carry flag
    IF Carry% = 0 THEN                  ' If no error occurred..
       Handle% = REG(1)                 ' Get handle from AX
       REG 2, Handle%                   ' Transfer it to BX
       REG 1, &H5700                    ' DOS Service 87
       CALL INTERRUPT &H21              ' - get file date and time
       Carry% = REG(0) AND 1            ' Check carry flag
       IF Carry% = 0 THEN               ' If no error occurred..
          FlTime% = REG(3)              ' Bit-encoded time from CX
          FlDate% = REG(4)              ' Bit-encoded date from DX
          Yr% = FlDate%                 ' Extract
          SHIFT RIGHT Yr%, 9            '   the
          Yr% = Yr% + 1980              '     year
          FlDate% = FlDate% AND &H1FF   ' Isolate day and month
          Mth% = FlDate%                ' Extract
          SHIFT RIGHT Mth%, 5           '   the month
          Day% = FlDate% AND &H1F       ' Extract day
          Hrs% = FlTime%                ' Extract
          SHIFT RIGHT Hrs%, 11          '   hours
          FlTime% = FlTime% AND &H7FF   ' Isolate minutes and seconds
          Mins% = FlTime%               ' Extract
          SHIFT RIGHT Mins%, 5          '   minutes
          Sex% = (FlTime% AND &H1F) * 2 ' Extract seconds
          Y$ = RIGHT$("0" + LTRIM$(RTRIM$(STR$(Yr%))), 2)
          M$ = RIGHT$("0" + LTRIM$(RTRIM$(STR$(Mth%))), 2)
          D$ = RIGHT$("0" + LTRIM$(RTRIM$(STR$(Day%))), 2)
'(Continued to next message)
'(Continued from previous message)
          Fmt% = GetDateFormat%         ' Get national date format
          SELECT CASE Fmt%
              CASE 0                    ' USA
                   Dt$ = M$ + "-" + D$ + "-" + Y$
              CASE 1                    ' Europe
                   Dt$ = D$ + "/" + M$ + "/" + Y$
              CASE 2                    ' Japan
                   Dt$ = Y$ + "-" + M$ + "-" + D$
              CASE ELSE
          END SELECT
          H$ = RIGHT$("0" + LTRIM$(RTRIM$(STR$(Hrs%))), 2)
          M$ = RIGHT$("0" + LTRIM$(RTRIM$(STR$(Mins%))), 2)
          S$ = RIGHT$("0" + LTRIM$(RTRIM$(STR$(Sex%))), 2)
          Dt$ = Dt$ + "  " + H$ + ":" + M$ + ":" + S$
       END IF
       REG 2, Handle%                   ' File handle to BX
       REG 1, &H3E00                    ' DOS Service 62
       CALL INTERRUPT &H21              ' - close the file
    END IF
    GetFileDate$ = Dt$                  ' Return date and time as string
END FUNCTION

'   Sets the last-access date and time of the specified file.
'
'   Note: FileDate$ must be in one of the following formats:
'
'       --123456789012345678--
'
'         MM-DD-YY  HH:MM:SS    (for USA)
'         DD/MM/YY  HH:MM:SS    (for Europe)
'         YY-MM-DD  HH:MM:SS    (for Japan)
'
'   (there are two blank spaces between the date and time
'
'   If Fmt% is TRUE (non-zero) then the procedure uses the date
'   format for the country corresponding to the COUNTRY= setting
'   in the computers CONFIG.SYS file (default = USA)
'
'   If Fmt% is FALSE (zero) then USA format is used.
'
SUB SetFileDate (FileName$, FileDate$, Fmt%, Done%)
    Done% = 0                           ' Assume failure
    F$ = FileName$ + CHR$(0)            ' Make filespec ASCIIZ
    REG 8, STRSEG(F$)                   ' DS = segment of filespec
    REG 4, STRPTR(F$)                   ' DX = offset of filespec
    REG 1, &H3D00                       ' DOS Service 61
    CALL INTERRUPT &H21                 ' - open file for reading
    Carry% = REG(0) AND 1               ' Check carry flag
    IF Carry% = 0 THEN                  ' If no error occurred..
       Handle% = REG(1)                 ' Get handle from AX
       IF Fmt% THEN
          Fmt% = GetDateFormat%         ' Get national date format
       END IF
       SELECT CASE Fmt%
           CASE 0                       ' USA
                Day% = VAL(MID$(FileDate$, 4, 2))
                Mth% = VAL(LEFT$(FileDate$, 2))
                Yr% = VAL(MID$(FileDate$, 7, 2))
           CASE 1                       ' Europe
                Mth% = VAL(MID$(FileDate$, 4, 2))
                Day% = VAL(LEFT$(FileDate$, 2))
                Yr% = VAL(MID$(FileDate$, 7, 2))
           CASE 2                       ' Japan
                Mth% = VAL(MID$(FileDate$, 4, 2))
                Yr% = VAL(LEFT$(FileDate$, 2))
                Day% = VAL(MID$(FileDate$, 7, 2))
           CASE ELSE
       END SELECT
       Hrs% = VAL(MID$(FileDate$, 11, 2))
       Mins% = VAL(MID$(FileDate$, 14, 2))
       Sex% = VAL(MID$(FileDate$, 17, 2)) \ 2
       IF Yr% < 80 THEN Yr% = Yr% + 100 ' Remember the 21st Century
       FlDate& = Yr% - 80               ' Juggle date
       SHIFT LEFT FlDate&, 9            '   into the
       SHIFT LEFT Mth%, 5               '     appropriate
       FlDate& = FlDate& + Mth% + Day%  '       bit-fields
       REG 4, FlDate&                   ' Load result into DX
       FlTime& = Hrs%                   ' Juggle time
       SHIFT LEFT FlTime&, 11           '   into the
       SHIFT LEFT Mins%, 5              '     appropriate
       FlTime& = FlTime& + Mins% + Sex% '       bit-fields
       REG 3, FlTime&                   ' Load result into CX
       REG 2, Handle%                   ' File handle to BX
'(Continued to next message)
'(Continued from previous message)
       REG 1, &H5701                    ' DOS Service 87
       CALL INTERRUPT &H21              ' - set file date and time
       Carry% = REG(0) AND 1            ' Check carry flag
       IF Carry% = 0 THEN               ' If no error occurred..
          Done% = -1                    '   report success
       END IF
       REG 2, Handle%                   ' File handle to BX
       REG 1, &H3E00                    ' DOS Service 62
       CALL INTERRUPT &H21              ' - close the file
    END IF
END SUB

