

'//
'//
'//
Function ctx_MakeDateTime_Internal( ByVal xdb As CHEETAH4_TYPE Ptr ) As Double

   Local pDBF      As DBF_TYPE Ptr  ' DateTime must be in YYYYMMDD HH:MM:SS  17 chars
   Local sDateTime As String
   
   Local Elapsed As Long
   Local Year    As Long
   Local Month   As Long
   Local Day     As Long
   Local Hours   As Long
   Local Minutes As Long
   Local Seconds As Long
  
   Local DateVar As Long
   Local TimeVar As Long  
  
   Local pLong   As Long Ptr
  
   Local DateTimeDouble As Double
  
   If @xdb.reserved = 0 Then @xdb.errcode = %ERR_DATA: GoTo ExitOut
   If @xdb.nData    = 0 Then @xdb.errcode = %ERR_DATA: GoTo ExitOut

   
   ' That is the number of days since the date  Jan 1, 4713 BC */
   ' Ex.  Jan 1, 1981 is  2444606 */
   ' convert from format 'HHxMMxSS where 'x' can be any character, so input may
   ' be 12:14:32  or may be '12.32.43'

   pDBF      = @xdb.reserved
   sDateTime = Peek$( @xdb.nData, @xdb.nLen )
   
   If Len(sDateTime) <> 17 Then @xdb.errcode = %ERR_DATETIME: GoTo ExitOut
  
   'Calculate the Julian date
   Year  = Val(Mid$(sDateTime, 1, 4))
   month = Val(Mid$(sDateTime, 5, 2))
   day   = Val(Mid$(sDateTime, 7, 2))
  
   If month < 3 Then                      ' January or February?
      month = month + 12                  ' 13th or 14th month ....
      Decr year                           ' .... of prev. year
   End If
  
   Elapsed = Int((year + 4712) * 365.25)  ' years elapsed
   Elapsed = Elapsed - (year \ 100)       ' substract century leapdays
   Elapsed = Elapsed + (year \ 400)       ' re-add valid ones
   Elapsed = Elapsed + _
             Int(30.6 * (month - 1) + .2) ' months elapsed + adjustm.

   DateVar = Elapsed + day               ' days of final month


   'Calculate the time
   Hours   = Val( Mid$( sDateTime, 10, 2) )
   Minutes = Val( Mid$( sDateTime, 13, 2) )
   Seconds = Val( Mid$( sDateTime, 16, 2) )
  
   TimeVar = 0
   TimeVar = TimeVar + (Hours   * 3600000 )
   TimeVar = TimeVar + (Minutes * 60000 )
   TimeVar = TimeVar + (Seconds * 1000 ) 
  
   pLong = VarPtr(DateTimeDouble)
   @pLong = DateVar
   Incr pLong
   @pLong = TimeVar
  
   @xdb.nData8byte = DateTimeDouble
  
ExitOut:
   If @xdb.errcode Then 
      Function = -1
   Else
      Function = 0
   End If
End Function



'//
'//  Swap bytes of a WORD sized variable (Big Endian format)
'//                                                         
Function SwapBytesWord (ByVal n As Integer) As Word
   Rotate Left n, 8
   Function = n
End Function


'//
'//  Swap bytes of a DWORD sized variable (Big Endian format)
'//                                                         
Function SwapBytesDWord (ByVal n As Long) As Dword
   Local p As Byte Ptr
   p = VarPtr(n)   
   Swap @p[0], @p[3]
   Swap @p[1], @p[2]
   Function = n
End Function
                   
                   

'//
'//  Truncate an existing, open, file at the specified byte position
'//
Function ctx_SetEndOfFile_Internal( ByVal hFile As Long, _
                                    ByVal nByteOffset As Long, _
                                    ByVal UseEOFchar As Long _
                                    ) As Long
   
   Local errcode As Long
   Local mEof    As String
                 
   ' Must flush the file before trying to truncate it. Important.
   FlushFileBuffers hFile

   ' Truncate the database
   If UseEOFchar Then
      mEof = Chr$(26)
      fWriteAt hFile, nByteOffset, 1, StrPtr(mEof)
      Incr nByteOffset
   End If
   
   ' Set the end of file by chopping off the file at this point
   fSeek hFile, nByteOffset 
   If SetEndOfFile( hFile ) = 0 Then
      errcode = %ERR_FILEACCESS: GoTo ExitOut
   End If
    
   FlushFileBuffers hFile

ExitOut:
   Function = errcode
   
End Function

           