(Updated: 2003.09.04 07:17:35 PM)
| |
Quick and dirty UDF to convert VFP Datetimes to a string recognized as RFC-822 compliant for conversion to XML:
FUNCTION XMLDateTime(tDateTime AS DATETIME) AS STRING
* Returns "Thu, 27 Feb 2003 14:11:12 GMT"
LOCAL lcReturn AS STRING, lcTime AS STRING
lcReturn = LEFT(PROPER(CDOW(tDateTime)),3) +", "
lcReturn = lcReturn + PADL(DAY(tDateTime),2,'0') + SPACE(1)
lcReturn = lcReturn + LEFT(PROPER(CMONTH(tDateTime)),3) + SPACE(1)
lcReturn = lcReturn + STR(YEAR(tDateTime),4)+SPACE(1)
lcTime = TTOC(tDateTime,2) && HH:MM:SS PM
* Compensate for PM
IF RIGHT(lcTime,2) = "PM" AND LEFT(lcTime,2) # "12"
lcTime = STR(12+VAL(lcTime),2)+SUBSTR(lcTime, 3, 6)
ELSE
lcTime = LEFT(lcTime, 8)
ENDIF
* Fake the time zone
lcTime = lcTime + " EST"
lcReturn = lcReturn + lcTime
RETURN lcReturn
ENDFUNC && XMLDateTime
--
Ted Roche
Or, converting from server time to UTC time:
FUNCTION csXMLDateTime(tDateTime as Datetime) as String
* Returns "Thu, 27 Feb 2003 14:11:12 GMT"
* get the UTC offset -- No need to fake the TZ
DECLARE integer GetTimeZoneInformation IN Win32API ;
STRING @ TimeZoneStruct
lcTZStruct = SPACE(256)
lnSunTime = GetTimeZoneInformation(@lcTZStruct )
lnUTCOffset = WordToInt(SUBSTR(lcTZStruct ,1,4), .t.)
lnDaylightBias = WordToInt(RIGHT(ALLTRIM(lcTZStruct), 4), .t.)
* Add the bias if daylight savings is active
IF lnSunTime = 2
lnUTCOffset = lnUTCOffset + lnDaylightBias
ENDIF
* convert the offset to seconds
lnUTCOffset = lnUTCOffset * 60
ltUTCTime = tDateTime + lnUTCOffset
LOCAL lcReturn as String
lcReturn = LEFT(PROPER(CDOW(ltUTCTime)),3) +", "
lcReturn = lcReturn + PADL(DAY(ltUTCTime),2,'0') + SPACE(1)
lcReturn = lcReturn + LEFT(PROPER(CMONTH(ltUTCTime)),3) + SPACE(1)
lcReturn = lcReturn + STR(YEAR(ltUTCTime),4)+SPACE(1)
lcReturn = lcReturn + STUFF(STUFF(RIGHT(TTOC(ltUTCTime,1), 6), 5, 0, ":"), 3, 0, ":") + SPACE(1)
lcReturn = lcReturn + "GMT"
* Alternatively, to get an ISO-8601 result of:
* YYYY-MM-DDTHH:MM:SSZ iaw http://www.w3.org/TR/NOTE-datetime
- lcReturn = TRANSFORM(TTOC(ltUTCTime,1), "@R 9999-99-99T99:99:99Z")
- -- Ted Roche
RETURN lcReturn
endfunc
************************************************************************
FUNCTION WordToInt(tcWordString, tlSigned)
LOCAL lcHexString as String, lnResult as Integer, lnChar as Integer
lcHexString = '0x'
FOR lnChar = LEN(tcWordString) TO 1 STEP -1
lcHexString = lcHexString + PADL(right(TRANSFORM(ASC(SUBSTR(tcWordString, lnChar, 1)), '@0'), 2), 2, '0')
ENDFOR
lnResult = EVALUATE(lcHexString)
IF tlSigned and lnResult > 0x80000000
lnResult = lnResult - 1 - 0xFFFFFFFF
ENDIF
RETURN lnResult
ENDFUNC
--
Andrew Coates
Category Code Samples