Wiki Home

XML Timezone Class


Namespace: VFP
While working on an XML project I found a need to convert the XML datetime.tz data type to one that VFP could work with and vice versa. Here's the code but be advised it has not been thoroughly tested Bob Tracy:
I made one small change below, tell me what you think. Alek Massey

I guess I'm not aware of a stictdate setting of "3" so I think you meant "2". Good idea since CTOT can create compile errors. Thanks! Bob Tracy

You are right, I meant 2 not 3. Glad to help, Alek Massey

DEFINE CLASS TimeZone AS custom

PROTECTED Application,          ;
          BaseClass,            ;
          Class,                ;
          ClassLibrary,		;
          Comment,		;
          ControlCount,		;
          Controls,		;
          Height,		;
          HelpContextID,	;
          Left,			;
          Name,			;
          Object,		;
          Parent,		;
          ParentClass,		;
          Picture,		;
          Tag,			;
          Top,			;
          WhatsThisHelpID,	;
          Width,		;
          cOldHours,		;
          cOldDate,		;
          cOldCentury
			
***************************************************************
* Convert system time to UTC and return as a string formatted *
* in ISO 8601 format                                          *
***************************************************************		
FUNCTION GetGMTDateTimeTZ()
	LOCAL lcOldCentury,lcOldDate,lcTZ,lnBias,lcZulu,lcRetVal
	lcTZ = THIS.GetTZInfo()
	IF !EMPTY(lcTZ)
		THIS.SetClock()
		lnBias = THIS.Str2Long(SUBSTR(lcTZ,1,4))
		lcZulu = TTOC(DATETIME()+(lnBias*60))
		lcRetVal = CHRTRAN(lcZulu,'.','-')
		lcRetVal = SUBSTR(lcRetVal,1,AT(' ',lcRetVal,1)-1)+'T'+ ;
				   SUBSTR(lcRetVal,AT(' ',lcRetVal,1)+1,8)+'Z'
		THIS.ResetClock()
	ENDIF		   
	RETURN lcRetVal
ENDFUNC		

*************************************************************
* Format system time in ISO 8601 format and append time     *
* zone information.                                         *
*************************************************************
FUNCTION GetLocalDateTimeTZ()
	LOCAL lcTZ,lnBias,lcOffset,lcDT,lcRetVal
	lcTZ = THIS.GetTZInfo()
	IF !EMPTY(lcTZ)
		THIS.SetClock()
		lnBias = THIS.Str2Long(SUBSTR(lcTZ,1,4))
		IF lnBias >= 0
			lcOffset = "-"
		ELSE
			lcOffset = "+"
		ENDIF
		IF lnBias/60 < 10
			lcOffset = lcOffset+"0"+STR(lnBias/60,1,0)+":00"
		ELSE
			lcOffset = lcOffset+STR(lnBias/60,2,0)+":00"
		ENDIF				
		lcDT = TTOC(DATETIME())
		lcRetVal = CHRTRAN(lcDT,'.','-')
		lcRetVal = SUBSTR(lcRetVal,1,AT(' ',lcRetVal,1)-1)+'T'+ ;
				   SUBSTR(lcRetVal,AT(' ',lcRetVal,1)+1,8)+lcOffset
		THIS.ResetClock()
	ENDIF		   
	RETURN lcRetVal
ENDFUNC

*************************************************************
* Convert an XML datetime string to a VFP datetime          *
* datatype.  The string passed must be a valid format and   *
* there is no UTC/Local conversion performed.               *
*************************************************************
FUNCTION ConvertXMLDateTime(tcDT)
	LOCAL ldRetVal
	THIS.SetClock()
	IF 'T'$tcDT
		tcDT = STUFF(tcDT,AT('T',tcDT),1,' ')
	ENDIF	
	IF 'Z'$tcDT
		tcDT = SUBSTR(tcDT,1,AT('Z',tcDT)-1)
	ENDIF
	IF LEN(tcDT) > 19
		tcDT = SUBSTR(tcDT,1,19)
	ENDIF	
* ALEKMASSEY I changed this so the strictdate setting could stay at 2 (the most strict)
	tcDT = '^'+tcDT
	ldRetVal = {&tcDT}
	*ldRetVal = CTOT(tcDT)
	THIS.ResetClock()
	RETURN ldRetVal
ENDFUNC

************************************************************
* Get the system time zone information                     *
************************************************************		
PROTECTED FUNCTION GetTZInfo()
	LOCAL liRetCode
	TZInfo = SPACE(172)
	DECLARE INTEGER GetTimeZoneInformation IN kernel32 STRING @TZInfo
	liRetCode = GetTimeZoneInformation(@TZInfo)
	IF liRetCode < 1
		TZInfo = ""
	ENDIF
	RETURN TZInfo
ENDFUNC

***********************************************************
* Convert the time zone string information to an integer  *
***********************************************************
PROTECTED FUNCTION Str2Long(tcTZ)
	LOCAL i,lnRetVal
	lnRetval = 0
	FOR i = 0 TO 24 STEP 8
		lnRetval = lnRetval + (ASC(tcTZ) * (2^i))
		tcTZ = RIGHT(tcTZ, LEN(tcTZ) - 1)
	NEXT i
	RETURN lnRetval
ENDFUNC

***********************************************************
* Save the current time-related system functions and set  *
* them for our time conversions                           *
***********************************************************
PROTECTED FUNCTION SetClock()
	WITH THIS
		.cOldHours = SET('HOURS')
		.cOldDate = SET('DATE')
		.cOldCentury = SET('CENTURY')
	ENDWITH
	SET HOURS TO 24
	SET CENTURY ON
	SET DATE ANSI
ENDFUNC

**********************************************************
* Reset the system time-related functions to their       *
* original settings                                      *
**********************************************************
PROTECTED FUNCTION ResetClock()
	LOCAL lcOldHours,lcOldDate,lcOldCentury
	WITH THIS
		lcOldHours = .cOldHours
		lcOldDate = .cOldDate
		lcOldCentury = .cOldCentury
	ENDWITH	
	SET HOURS TO &lcOldHours
	SET DATE &lcOldDate
	SET CENTURY &lcOldCentury
ENDFUNC
	
ENDDEFINE

Category Code Samples
( Topic last updated: 2001.03.02 12:07:38 PM )