home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast.iso / pcmag / vol7n10.zip / TD.ASM < prev    next >
Assembly Source File  |  1988-05-31  |  12KB  |  388 lines

  1.         name    td
  2.         title   TD.ASM --- time & date formatting
  3.         page    55,132
  4. ;
  5. ; TD.ASM --- Time and Date Formatting Functions
  6. ;
  7. ; Copyright (C) 1988 Ziff Communications Co.
  8. ; Ray Duncan * PC Magazine
  9. ;
  10. ; This module contains six public routines:
  11. ;
  12. ; TCVT          convert time to ASCII
  13. ; DCVT          convert date to ASCII
  14. ;
  15. ; SYSTCVT       convert current time to ASCII
  16. ; SYSDCVT       convert current date to ASCII
  17. ;
  18. ; DIRTCVT       convert time in directory format to ASCII
  19. ; DIRDCVT       convert date in directory format to ASCII
  20. ;
  21.  
  22. DGROUP  group   _DATA
  23.  
  24.  
  25. _DATA   segment word public 'DATA'
  26.  
  27. cbuff   db      34 dup (0)      ; current country info
  28. dbuff   db      8  dup (' ')    ; date formatting buffer
  29. tbuff   db      11 dup (' ')    ; time formatting buffer
  30.  
  31.                                 ; filled in by 'getctry'
  32. doffs   dw      0               ; offset of ASCII day
  33. moffs   dw      0               ; offset of ASCII month
  34. yoffs   dw      0               ; offset of ASCII year
  35.  
  36.                                 ; date format determined
  37.                                 ; by Int 21H Func. 38H 
  38. dtab    dw      mdy             ; 0 = USA format
  39.         dw      dmy             ; 1 = Europe format
  40.         dw      ymd             ; 2 = Japan format       
  41.  
  42. mdy     dw      dbuff+3         ; USA: month day year
  43.         dw      dbuff
  44.         dw      dbuff+6
  45.  
  46. dmy     dw      dbuff           ; Europe: day month year
  47.         dw      dbuff+3
  48.         dw      dbuff+6
  49.  
  50. ymd     dw      dbuff+6         ; Japan: year month day
  51.         dw      dbuff+3
  52.         dw      dbuff
  53.  
  54. _DATA   ends
  55.  
  56.  
  57. _TEXT   segment word public 'CODE'
  58.  
  59.         assume  cs:_TEXT,ds:DGROUP
  60.  
  61.         public  dcvt            ; make routines available 
  62.         public  tcvt            ; to Linker
  63.         public  systcvt
  64.         public  sysdcvt
  65.         public  dirtcvt
  66.         public  dirdcvt
  67.  
  68.  
  69. sysdcvt proc    near            ; format system date
  70.                                 ; BX    = length
  71.                                 ; DS:SI = buffer
  72.                                 ; preserves all registers
  73.  
  74.         push    ax              ; save registers
  75.         push    bx
  76.         push    cx
  77.         push    dx
  78.         
  79.         mov     ah,2ah          ; func. 2AH = get date
  80.         int     21h             ; transfer to MS-DOS
  81.  
  82.         call    dcvt            ; convert to ASCII
  83.  
  84.         pop     dx              ; restore registers
  85.         pop     cx
  86.         pop     bx
  87.         pop     ax
  88.  
  89.         ret                     ; back to caller
  90.  
  91. sysdcvt endp
  92.  
  93.  
  94. systcvt proc    near            ; format system time
  95.                                 ; BX    = length
  96.                                 ; DS:SI = buffer
  97.                                 ; preserves all registers
  98.  
  99.         push    ax              ; save registers
  100.         push    bx
  101.         push    cx
  102.         push    dx
  103.         
  104.         mov     ah,2ch          ; func. 2CH = get time
  105.         int     21h             ; transfer to MS-DOS
  106.  
  107.         call    tcvt            ; convert to ASCII
  108.  
  109.         pop     dx              ; restore registers
  110.         pop     cx
  111.         pop     bx
  112.         pop     ax
  113.  
  114.         ret                     ; back to caller
  115.  
  116. systcvt endp
  117.  
  118.  
  119. dirdcvt proc    near            ; format directory date
  120.                                 ; AX    = directory date
  121.                                 ; BX    = length
  122.                                 ; DS:SI = buffer
  123.                                 ; preserves all registers
  124.  
  125.         push    ax              ; save registers
  126.         push    bx
  127.         push    cx
  128.         push    dx
  129.         
  130.         mov     dx,ax           ; isolate months & days
  131.         and     dx,01ffh
  132.         mov     cl,3            ; position month        
  133.         shl     dx,cl
  134.         shr     dl,cl           ; position day
  135.  
  136.         mov     cl,9            ; position year
  137.         shr     ax,cl
  138.         add     ax,1980
  139.         mov     cx,ax
  140.  
  141.         call    dcvt            ; convert to ASCII
  142.  
  143.         pop     dx              ; restore registers
  144.         pop     cx
  145.         pop     bx
  146.         pop     ax
  147.  
  148.         ret                     ; back to caller
  149.  
  150. dirdcvt endp
  151.  
  152.  
  153. dirtcvt proc    near            ; format directory time
  154.                                 ; AX    = directory time
  155.                                 ; BX    = length
  156.                                 ; DS:SI = buffer
  157.                                 ; preserves all registers
  158.  
  159.         push    ax              ; save registers
  160.         push    bx
  161.         push    cx
  162.         push    dx
  163.         
  164.         mov     dx,ax           ; isolate seconds field
  165.         and     dx,1fh          ; and position it
  166.         mov     cl,9            ; (includes seconds*2)
  167.         shl     dx,cl
  168.  
  169.         mov     cl,3            ; position hours
  170.         shr     ax,cl
  171.  
  172.         mov     cl,2            ; position minutes
  173.         shr     al,cl
  174.         mov     cx,ax
  175.  
  176.         call    tcvt            ; convert to ASCII
  177.  
  178.         pop     dx              ; restore registers
  179.         pop     cx
  180.         pop     bx
  181.         pop     ax
  182.  
  183.         ret                     ; back to caller
  184.  
  185. dirtcvt endp
  186.  
  187.  
  188. dcvt    proc    near            ; format ASCII date 
  189.                                 ; BX    = length
  190.                                 ; CX    = year (1980+)
  191.                                 ; DH    = month (1-12)
  192.                                 ; DL    = day (1-31)
  193.                                 ; DS:SI = buffer
  194.                                 ; length clamped to 8
  195.                                 ; destroys AX BX CX DX
  196.  
  197.         cmp     bx,8            ; make sure length OK
  198.         jle     dcvt1
  199.         mov     bx,8            ; too long, use 8 max
  200.  
  201. dcvt1:  push    es              ; save registers
  202.         push    di
  203.         push    si
  204.         push    bx
  205.  
  206.         call    getctry         ; get country info
  207.  
  208.         mov     si,moffs        ; convert month
  209.         mov     al,dh
  210.         call    b2dec   
  211.  
  212.         mov     si,doffs        ; convert day
  213.         mov     al,dl
  214.         call    b2dec
  215.  
  216.         mov     si,yoffs        ; convert year, 
  217.         sub     cx,1900         ; corrected to 80-99
  218.         mov     al,cl
  219.         call    b2dec
  220.  
  221.         mov     ax,ds           ; transfer ASCII date
  222.         mov     es,ax           ; to caller's buffer
  223.         mov     si,offset DGROUP:dbuff
  224.         pop     cx              ; buffer length
  225.         pop     di              ; buffer address
  226.         push    di
  227.         rep movsb               ; copy string
  228.  
  229.         pop     si              ; restore registers     
  230.         pop     di
  231.         pop     es
  232.  
  233.         ret                     ; return to caller
  234.  
  235. dcvt    endp
  236.  
  237.                                          
  238. tcvt    proc    near            ; format ASCII time
  239.                                 ; BX    = length
  240.                                 ; CH    = hour
  241.                                 ; CL    = minute
  242.                                 ; DH    = second
  243.                                 ; DL    = hundredths
  244.                                 ; DS:SI = buffer
  245.                                 ; length clamped to 11
  246.                                 ; destroys AX BX CX DX
  247.  
  248.         cmp     bx,11           ; make sure length OK
  249.         jle     tcvt1
  250.         mov     bx,11           ; too long, use 11 max
  251.  
  252. tcvt1:  push    es              ; save registers
  253.         push    di
  254.         push    si
  255.         push    bx
  256.  
  257.         call    getctry         ; get country info
  258.  
  259.         mov     al,ch           ; convert hours
  260.         mov     si,offset DGROUP:tbuff
  261.         call    b2dec
  262.  
  263.         mov     al,cl           ; convert minutes
  264.         add     si,3
  265.         call    b2dec
  266.  
  267.         mov     al,dh           ; convert seconds
  268.         add     si,3
  269.         call    b2dec
  270.  
  271.         mov     al,dl           ; convert hundredths
  272.         add     si,3
  273.         call    b2dec
  274.  
  275.         mov     ax,ds           ; transfer ASCII time
  276.         mov     es,ax           ; to caller's buffer
  277.         mov     si,offset DGROUP:tbuff
  278.         pop     cx              ; buffer length
  279.         pop     di              ; buffer address
  280.         push    di
  281.         rep movsb               ; copy string
  282.  
  283.         pop     si              ; restore registers     
  284.         pop     di
  285.         pop     es
  286.  
  287.         ret                     ; return to caller
  288.  
  289. tcvt    endp
  290.  
  291.  
  292. b2dec   proc    near            ; convert binary 0-99
  293.                                 ; to two ASCII digits.
  294.                                 ; AL    = value
  295.                                 ; DS:SI = storage address
  296.                                 ; destroys AX 
  297.  
  298.         aam                     ; divide AL by 10 ->
  299.                                 ; AH = quot., AL = rem.
  300.  
  301.         add     ax,'00'         ; convert to ASCII
  302.         xchg    ah,al
  303.         mov     [si],ax         ; and store digits
  304.  
  305.         ret                     ; back to caller
  306.  
  307. b2dec   endp
  308.  
  309.  
  310. getctry proc    near            ; get country information
  311.  
  312.         test    doffs,-1        ; did we already get info?
  313.         jnz     getc4           ; if we did, just exit
  314.         
  315.         push    ax              ; save registers
  316.         push    bx              ; (in case destroyed by
  317.         push    cx              ;  Int 21H Function 38H)
  318.         push    dx
  319.  
  320.         mov     ax,3000h        ; Fxn. 30h = get DOS vers.
  321.         int     21h             ; transfer to MS-DOS
  322.  
  323.         or      al,al           ; is it version 1.x?
  324.         jz      getc1           ; yes, jump
  325.  
  326.         push    ax              ; save MS-DOS version
  327.  
  328.         mov     ax,3800h        ; get current country info
  329.         mov     dx,offset DGROUP:cbuff
  330.         int     21h             ; transfer to MS-DOS
  331.  
  332.         pop     ax              ; restore MS-DOS version
  333.         jc      getc1           ; jump if get cntry failed
  334.  
  335.         cmp     al,3            ; is it version 3.x?
  336.         jne     getc2           ; jump if version 2
  337.  
  338.                                 ; MS-DOS version 3...
  339.         mov     al,cbuff+9      ; get decimal separator
  340.         mov     bh,cbuff+11     ; get date separator
  341.         mov     bl,cbuff+13     ; get time separator
  342.         jmp     getc3
  343.  
  344. getc1:                          ; version 1.x or get
  345.                                 ; country info failed,
  346.                                 ; force date format=mdy
  347.         mov     word ptr cbuff,0
  348.  
  349. getc2:                          ; versions 1.x and 2.x, 
  350.         mov     al,'.'          ; force decimal separator
  351.         mov     bh,'/'          ; force date separator
  352.         mov     bl,':'          ; force time separator
  353.  
  354. getc3:  mov     tbuff+8,al      ; store decimal separator
  355.  
  356.         mov     tbuff+2,bl      ; store time separators
  357.         mov     tbuff+5,bl
  358.  
  359.         mov     dbuff+2,bh      ; store date separators
  360.         mov     dbuff+5,bh
  361.  
  362.                                 ; set date field offsets
  363.                                 ; using country info    
  364.         mov     bx,word ptr cbuff
  365.         shl     bx,1            ; date code*2=dtab index
  366.         mov     bx,[bx+dtab]
  367.         mov     ax,[bx]         ; offset for ASCII day
  368.         mov     doffs,ax
  369.         mov     ax,[bx+2]       ; offset for ASCII month
  370.         mov     moffs,ax
  371.         mov     ax,[bx+4]       ; offset for ASCII year
  372.         mov     yoffs,ax
  373.  
  374.         pop     dx              ; restore registers
  375.         pop     cx
  376.         pop     bx
  377.         pop     ax
  378.  
  379. getc4:  ret                     ; back to caller
  380.  
  381. getctry endp
  382.  
  383.  
  384. _TEXT   ends
  385.  
  386.  
  387.         end
  388.