| xHarbour Reference Documentation > Statement Reference |
![]() |
![]() |
![]() |
Defines a scalar class for a native data type.
ASSOCIATE CLASS <ClassName> WITH TYPE <datatype>
The ASSOCIATE CLASS statement associates a user defined scalar class with a native data type. Objects of such class are used in place of the data type <datatype>. A scalar class can have only CLASSDATA and METHODs but no DATA, since a scalar object itself is the data.
A scalar class must be declared and implemented following the CLASS statement. Default implementations of scalar classes can be enabled with the ENABLE TYPE CLASS. Note, however, that ASSOCIATE CLASS and ENABLE TYPE CLASS are mutually exclusive statements.
| See also: | ENABLE TYPE CLASS, CLASS, EXTEND CLASS...WITH METHOD |
| Category: | Class declaration , Declaration , xHarbour extensions |
| Header: | hbclass.ch |
| Source: | rtl\tclass.prg |
| LIB: | xhb.lib |
| DLL: | xhbdll.dll |
// The example implements a class that extends the data type
// Date with advanced date and time formatting capabilities.
// It distinguishes upper and lower case letters for the Day,
// Month and time. A formatting template is built with the
// following characters:
//
// d - numeric day without leading zero
// dd - numeric day with leading zero
// ddd - numeric day without leading zero and postfix (st, nd, rd, th)
// D - first letter of weekday
// DD - name of weekday abbreviated to two characters
// DDD - name of weekday abbreviated to three characters
// DDDD - complete name of weekday
// M - numeric month without leading zero
// MM - numeric month with leading zero
// MMM - month name abbreviated to three letters
// MMMM - complete month name
// yy - year without century
// yyyy - year with century
// h - hour without leading zero
// hh - hour with leading zero
// m - minute without leading zero
// mm - minute with leading zero
// s - second without leading zero
// ss - second with leading zero
// \ - escape character for d, D, m, M, y, h, m, s
#include "HbClass.ch"
PROCEDURE Main
LOCAL dDate
ASSOCIATE CLASS DateFormatter WITH TYPE DATE
dDate := Ctod( "09/01/2006" )
? dDate
// result: 09/01/06
? dDate:shortDay()
// result: Fri
? dDate:shortMonth()
// result: Sep
? dDate:format( "dd MMM. yyyy" )
// result: 01 Sep. 2006
? dDate:format( "dd-MM-yy hh:mm\h", Time() )
// result: 01-09-06 18:07h
dDate += 2
? dDate:format( "DDDD, MMMM ddd, at h \hour\s an\d m \minute\s" )
// result: Sunday, September 3rd, at 18 hours and 7 minutes
? dDate:httpDate()
// result: Sun, 03 Sep 2006 18:07:45
RETURN
CLASS DateFormatter
EXPORTED:
METHOD shortDay INLINE Left( CDow(self), 3 )
METHOD shortMonth INLINE Left( CMonth(self), 3 )
METHOD time INLINE Time()
METHOD asString INLINE DtoS(self)
METHOD httpDate
METHOD format
ENDCLASS
METHOD httpDate( cTime ) CLASS DateFormatter
RETURN ::format( "DDD, dd MMM yyyy hh:mm:ss", cTime )
METHOD format( cFormat, cTime ) CLASS DateFormatter
LOCAL aToken := {}
LOCAL cToken := ""
LOCAL cChar
IF cTime == NIL
cTime := ::time()
ENDIf
FOR EACH cChar IN cFormat
IF .NOT. cChar IN "DdMyhms"
IF Len( cToken ) > 0
AAdd( aToken, cToken )
ENDIF
IF cChar == "\"
cToken := cChar
ELSE
AAdd( aToken, cChar )
cToken := ""
ENDIF
ELSE
cToken += cChar
ENDIF
NEXT
AAdd( aToken, cToken )
cFormat := ""
FOR EACH cToken IN aToken
IF cToken == ""
LOOP
ENDIF
SWITCH cToken[1]
CASE "y"
IF Len( cToken ) == 4
cFormat += Left( ::asString(), 4 )
ELSE
cFormat += SubStr( ::asString(), 3, 2 )
ENDIF
EXIT
CASE "M"
SWITCH Len( cToken )
CASE 1
cToken := SubStr( ::asString(), 5, 2 )
IF cToken[1] == "0"
cFormat += cToken[2]
ELSE
cFormat += cToken
ENDIF
EXIT
CASE 2
cFormat += SubStr( ::asString(), 5, 2 )
EXIT
CASE 3
cFormat += ::shortMonth()
EXIT
DEFAULT
cFormat += CMonth(self)
END
EXIT
CASE "D"
IF Len( cToken ) <= 3
cFormat += Left( CDoW(self), Len(cToken) )
ELSE
cFormat += CDoW(self)
ENDIF
EXIT
CASE "d"
SWITCH Len(cToken)
CASE 1
cToken := Right( ::asString(), 2 )
IF cToken[1] == "0"
cFormat += cToken[2]
ELSE
cFormat += cToken
ENDIF
CASE 2
cFormat += Right( ::asString(), 2 )
EXIT
DEFAULT
cToken := Right( ::asString(), 2 )
cFormat += IIf( cToken[1] == "0", cToken[2], cToken )
DO CASE
CASE cToken == "11" .OR. ;
cToken == "12" ; cFormat += "th"
CASE cToken[2] == "1" ; cFormat += "st"
CASE cToken[2] == "2" ; cFormat += "nd"
CASE cToken[2] == "3" ; cFormat += "rd"
OTHERWISE
cFormat += "th"
ENDCASE
END
EXIT
CASE "h"
CASE "m"
CASE "s"
SWITCH cToken[1]
CASE "h" ; cChar := Left (cTime,2) ; EXIT
CASE "m" ; cChar := SubStr(cTime,4,2) ; EXIT
CASE "s" ; cChar := Right (cTime,2) ; EXIT
END
IF Len( cToken ) == 1 .AND. cChar[1] == "0"
cChar := cChar[2]
ENDIF
cFormat += cChar
EXIT
CASE "\"
cFormat += SubStr( cToken, 2 )
EXIT
DEFAULT
cFormat += cToken
END
NEXT
RETURN cFormat
http://www.xHarbour.com