home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / BC_502 / OWLSRC.PAK / DATETIME.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  4.2 KB  |  188 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // Copyright (c) 1993, 1997 by Borland International, All Rights Reserved
  4. //
  5. //$Revision:   10.6  $
  6. //
  7. // Implementation of date/time helpers, mostly for Win16
  8. //----------------------------------------------------------------------------
  9. #include <owl/pch.h>
  10. #if !defined(OWL_DEFS_H)
  11. # include <owl/defs.h>
  12. #endif
  13. #include <stdio.h>
  14. #include <dos.h>
  15.  
  16.  
  17. OWL_DIAGINFO;
  18.  
  19. #if defined(BI_NAMESPACE)
  20. namespace OWL {
  21. #endif
  22.  
  23. #if !defined(BI_PLAT_WIN32)
  24.  
  25. //
  26. // Convert date and time structures to 64-bit FILETIME structure
  27. // multiply seconds by 10^7 and adjust offset to Jan. 1, 1601
  28. // adjustment from Jan.1, 1970 = 116,444,736,000,000,000 = 0x019DB1DED53E8000
  29. // This code is for Win32 and OLE2 compatibility when using Win16
  30. //
  31. void
  32. DateTimeToFileTime(struct date* dosd, struct time* dost, FILETIME FAR* pft)
  33. {
  34.   union {
  35.     long udt;
  36.     int  w[2];
  37.   } u;
  38.   u.udt = dostounix(dosd, dost);
  39.   _BL = dost->ti_hund;
  40.   _AX = u.w[0];
  41.   _CX = u.w[1];
  42.   asm mov  di,200
  43.   asm mul  di
  44.   asm mov  bh,0
  45.   asm add  bx,bx
  46.   asm add  ax,bx
  47.   asm adc  dl,dh
  48.   asm xchg ax,cx
  49.   asm mov  bx,dx
  50.   asm mul  di
  51.   asm add  bx,ax
  52.   asm mov  si,0
  53.   asm adc  si,dx
  54.   asm les  di,pft
  55.   asm mov  ax,50000
  56.   asm xchg ax,cx
  57.   asm mul  cx
  58.   asm add  ax,0x8000
  59.   asm adc  dx,0
  60.   asm mov  es:[di],ax
  61.   asm xchg ax,dx
  62.   asm xchg ax,bx
  63.   asm mul  cx
  64.   asm add  ax,bx
  65.   asm adc  dx,0
  66.   asm add  ax,0xD53E
  67.   asm adc  dx,0
  68.   asm mov  es:[di+2],ax
  69.   asm xchg ax,dx
  70.   asm xchg ax,si
  71.   asm mul  cx
  72.   asm add  ax,si
  73.   asm adc  dx,0
  74.   asm add  ax,0xB1DE
  75.   asm adc  dx,0x019D
  76.   asm mov  es:[di+4],ax
  77.   asm mov  es:[di+6],dx
  78. }
  79.  
  80. //
  81. // Convert FILETIME structure to date and time structures
  82. //
  83. bool
  84. FileTimeToDateTime(FILETIME FAR* pft, struct date* dosd, struct time* dost)
  85. {
  86.   union {
  87.     long udt;
  88.     int  w[2];
  89.   } u;
  90.   asm les  di,pft
  91.   asm mov  dx,es:[di+6]
  92.   asm mov  ax,es:[di+4]
  93.   asm sub  ax,0xB1DE
  94.   asm sbb  dx,0x019D
  95.   asm jnc OK
  96.   return false;
  97. OK:
  98.   asm mov  cx,50000
  99.   asm div  cx
  100.   asm xchg bx,ax
  101.   asm mov  ax,es:[di+2]
  102.   asm sub  ax,0xD53E
  103.   asm sbb  dx,0
  104.   asm div  cx
  105.   asm xchg si,ax
  106.   asm mov  ax,es:[di]
  107.   asm sub  ax,(0x8000-5000)
  108.   asm sbb  dx,0
  109.   asm div  cx
  110.   asm mov  dx,bx
  111.   asm xchg ax,si
  112.   asm mov  cx,200
  113.   asm div  cx
  114.   asm xchg ax,si
  115.   asm div  cx
  116.   asm shr  dx,1
  117.   asm push dx
  118.   u.w[0] = _AX;
  119.   u.w[1] = _SI;
  120.   unixtodos(u.udt, dosd, dost);
  121.   asm pop  ax
  122.   dost->ti_hund = _AL;
  123.   return true;
  124. }
  125.  
  126. //
  127. // Win32/OLE2 compatible FILETIME support for 16-bit applications
  128. // These functions to be incorporated into TDateTime class
  129. //
  130. int
  131. FormatDateTime(struct date filed, struct time filet, void far* dest, int textlen)
  132. {
  133.   char buf[40];
  134.   if (!textlen) {
  135.     DateTimeToFileTime(&filed, &filet, (FILETIME FAR*)dest);
  136.     return sizeof(FILETIME);
  137.   }
  138.   int len = sprintf(buf,"%d/%d/%d %02d:%02d:%02d.%02d",
  139.                     filed.da_mon,filed.da_day,filed.da_year,
  140.                     filet.ti_hour,filet.ti_min,filet.ti_sec,filet.ti_hund);
  141.   if (textlen > len)
  142.     textlen = len;
  143.   memcpy(dest, buf, textlen);
  144.   *((char far*)dest + textlen) = 0;
  145.   return len;
  146. }
  147.  
  148. #endif
  149.  
  150. //
  151. // Public global function to format a file date+time string
  152. //
  153. int _OWLFUNC
  154. FormatFileTime(FILETIME* pft, void far* dest, int textlen)
  155. {
  156.   char buf[40];
  157.   int len;
  158.  
  159.   if (!textlen) {
  160.     *(FILETIME FAR*)dest = *pft;
  161.     return sizeof(FILETIME);
  162.   }
  163. #if defined(BI_PLAT_WIN32)
  164.   SYSTEMTIME dt;
  165.   FileTimeToSystemTime(pft, &dt);
  166.   len = sprintf(buf,"%d/%d/%d %02d:%02d:%02d.%02d",
  167.                 dt.wMonth, dt.wDay, dt.wYear,
  168.                 dt.wHour, dt.wMinute, dt.wSecond, dt.wMilliseconds/10);
  169. #else
  170.   struct time filet;
  171.   struct date filed;
  172.   if (!(FileTimeToDateTime(pft, &filed, &filet)))
  173.     return false;
  174.   len = sprintf(buf,"%d/%d/%d %02d:%02d:%02d.%02d",
  175.                 filed.da_mon,filed.da_day,filed.da_year,
  176.                 filet.ti_hour,filet.ti_min,filet.ti_sec,filet.ti_hund);
  177. #endif
  178.   if (textlen > len)
  179.     textlen = len;
  180.   memcpy(dest, buf, textlen);
  181.   *((char far*)dest + textlen) = 0;
  182.   return len;
  183. }
  184.  
  185. #if defined(BI_NAMESPACE)
  186. } // namespace OWL
  187. #endif
  188.