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

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