Programmering - Assembler - INT - INT21 - 57 : File Date and Time

When a file is created or changed, DOS records the date and time in the directory entry as 4 bytes: 2 for the date and 2 for the time:

         bits
  Time   0- 4 : second/2 (0..30)
         5-10 : minute (0..59)
        11-15 : hour (0..23)
  Date   0- 4 : day of month (1..31)
         5- 8 : month (1..12)
         9-15 : year-1980 (0=1980, 20=2000)

>Disks: Root entries: Time-Date

 

INT 21, 57 reads and writes these 4 bytes - the file must be open and the handle known.
>INT 21, 3D : Open File with Handle

 

INT 21, 5700 : Get File Date and Time
AX = 5700h
BX = handle of file
CF = 1 : error
  AX = error code
     = 6 : ERROR_INVALID_HANDLE
CF = 0 : no error
  CX = time
  DX = date

 

INT 21, 5701 : Set File Date and Time
AX = 5701h
BX = handle of file
CX = time
DX = date
CF = 1 : error
  AX = error code
     = 6 : ERROR_INVALID_HANDLE
CF = 0 : no error

 

Update the date and time for the file "test.txt"
;filetime.asm
TITLE        FileTime
CODE         SEGMENT
ASSUME       CS:CODE, DS:CODE, SS:CODE
ORG          100H
start:       jmp begin
;-----------------------
filename     db "test.txt", 0        ;zero-term. string
handle       dw 0
filetime     dw 0
filedate     dw 0
;-----------------------
begin:       call OpenFile
             call GetFileTime
                                     ;Change file date or time - ex:
             mov ax, filedate
                                     ;add 1 to year (bit 9-15)
             add ax, 200h            ;'1000000000'
             mov filedate, ax

             call SetFileTime
             call CloseFile
        
             mov ax,4C00h            ;Terminate
             int 21h
;----------------------------

OpenFile:    mov dx, offset filename
             mov al, 2               ;OPEN_ACCESS_READWRITE
             mov ah, 3Dh             ;Open File with Handle
             int 21h
             mov handle, ax
             ret

CloseFile:   mov bx, handle
             mov ah, 3Eh             ;Close File with Handle
             int 21h
             ret

GetFileTime: mov bx, handle
             mov ax, 5700h           ;Get File Date and Time
             int 21h
             mov filetime, cx
             mov filedate, dx
             ret

SetFileTime: mov bx, handle
             mov cx, filetime
             mov dx, filedate
             mov ax, 5701h           ;Set File Date and Time
             int 21h
             ret

;----------------------------
CODE         ENDS
END          start
Compilation to filetime.com:
>masm filetime;
>link filetime;
>exe2bin filetime filetime.com