home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / PP0802.ZIP / TD.ASM < prev    next >
Assembly Source File  |  1988-10-11  |  11KB  |  346 lines

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