Skip to content

Instantly share code, notes, and snippets.

@kitroed
Created October 23, 2012 14:10
Show Gist options
  • Save kitroed/3938956 to your computer and use it in GitHub Desktop.
Save kitroed/3938956 to your computer and use it in GitHub Desktop.
Translate a standard FoxPro datetime timestamp to a FOX system file timestamp.
PROCEDURE GetTimeStamp
#define DEBUGGING .F.
*=======================================================
* GetTimeStamp( vDateTime )
*
* Returns a FOX system file timestamp
* from a date time value, any data type
*
* Calls: intToBin(), binToInt() defined below.
*=======================================================
lparameter tvDateTime
*-------------------------------------------------------
* Default to current datetime
*-------------------------------------------------------
LOCAL ltDateTime, lvFoxTimeStamp, lvTemp
ltDateTime = CTOT(TRANSFORM(tvDateTime))
IF EMPTY(ltDateTime)
ltDateTime = DATETIME()
ENDIF
#IF DEBUGGING
ACTI SCREEN
CLEAR
? ltDateTime
#ENDIF
*-------------------------------------------------------
* bits 4-0, seconds in two-second increments
*-------------------------------------------------------
lvTemp = SEC(ltDateTime) / 2
lvFoxTimeStamp = PADL(RIGHT(THIS.IntToBin(lvTemp),5),5,"0")
#IF DEBUGGING
? lvTemp
? lvFoxTimeStamp
?
#ENDIF
*-------------------------------------------------------
* bits 10-5, minutes
*-------------------------------------------------------
lvTemp = MINUTE(ltDateTime)
lvFoxTimeStamp = PADL(RIGHT(THIS.IntToBin(lvTemp),6),6,"0") + lvFoxTimeStamp
#IF DEBUGGING
? lvTemp
? lvFoxTimeStamp
?
#ENDIF
*-------------------------------------------------------
* bits 15-11, hours
*-------------------------------------------------------
lvTemp = HOUR(ltDateTime)
lvFoxTimeStamp = PADL(RIGHT(THIS.IntToBin(lvTemp),5),5,"0") + lvFoxTimeStamp
#IF DEBUGGING
? lvTemp
? lvFoxTimeStamp
?
#ENDIF
*-------------------------------------------------------
* bits 20-16, days
*-------------------------------------------------------
lvTemp = DAY(ltDateTime)
lvFoxTimeStamp = PADL(RIGHT(THIS.IntToBin(lvTemp),5),5,"0") + lvFoxTimeStamp
#IF DEBUGGING
? lvTemp
? lvFoxTimeStamp
?
#ENDIF
*-------------------------------------------------------
* bits 24-21, months
*-------------------------------------------------------
lvTemp = MONTH(ltDateTime)
lvFoxTimeStamp = PADL(RIGHT(THIS.IntToBin(lvTemp),4),4,"0") + lvFoxTimeStamp
#IF DEBUGGING
? lvTemp
? lvFoxTimeStamp
?
#ENDIF
*-------------------------------------------------------
* bits 31-25, years with a 1980 offset
*-------------------------------------------------------
lvTemp = YEAR(ltDateTime)-1980
lvFoxTimeStamp = PADL(RIGHT(THIS.IntToBin(lvTemp),7),7,"0") + lvFoxTimeStamp
#IF DEBUGGING
? lvTemp
? LEN(lvFoxTimeStamp)
? lvFoxTimeStamp
?
#ENDIF
lvFoxTimeStamp = THIS.BinToInt(lvFoxTimeStamp)
RETURN lvFoxTimeStamp
ENDPROC
PROCEDURE BinToInt
*=======================================================
* BinToInt( cBytes )
*
* Returns an integer form of binary data
*=======================================================
LPARAMETERS tcBinary
LOCAL lcInteger,lnInteger,lnCount,lnStrLen
IF EMPTY(tcBinary)
RETURN 0
ENDIF
lnStrLen=LEN(tcBinary)
lnInteger=0
FOR lnCount = 0 TO (lnStrLen-1)
IF SUBSTR(tcBinary,lnStrLen-lnCount,1)=="1"
lnInteger=lnInteger+2^lnCount
ENDIF
ENDFOR
RETURN INT(lnInteger)
ENDPROC
PROCECURE IntToBin
*=======================================================
* IntToBin( int )
*
* Returns a binary form of an integer
*=======================================================
LPARAMETERS tnInteger
LOCAL lnInteger,lcBinary,lnDivisor,lnCount
IF EMPTY(tnInteger)
RETURN "0"
ENDIF
lnInteger=INT(tnInteger)
lcBinary=""
FOR lnCount = 31 TO 0 STEP -1
lnDivisor=2^lnCount
IF lnDivisor>lnInteger
lcBinary=lcBinary+"0"
LOOP
ENDIF
lcBinary=lcBinary+IIF((lnInteger/lnDivisor)>0,"1","0")
lnInteger=INT(lnInteger-lnDivisor)
ENDFOR
RETURN lcBinary
ENDPROC
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment