Discussion:
Date Class gotcha, and SToD function
(too old to reply)
Rich Assaf
2008-10-16 04:03:47 UTC
Permalink
This was driving me crazy, since I had some deeply buried custom controls that were checking for type('dParameter') == 'D', and couldn't figure out why my code was generating unexpected values...

For those unaware, using d = New Date() generates a type 'O' (Object), and not a type 'D' (as I expected). I know this is WAD, but still annoying....

At any rate, my work-around (and a simpler StringToDate function than is in dUFLP):

function SToD(cDateStr)
// return a date value from DToS value ('20081116')
local d
d = new date()
d.date = val( right(cDateStr,2) )
d.month = val(substr(cDateStr,5,2) )-1 // date object starts with 0 for Jan, 11 for Dec
d.year = val( left(cDateStr,4 ))
return ctod(dtoc(d)) // Date class creates an Object, Not a date....

Converting date values via DToS() and SToD() avoids problems with local century settings, international date format issues, etc.

Rich
Geoff Wass [dBVIPS]
2008-10-16 04:46:47 UTC
Permalink
In article <***@news-server>, ***@hotmail.com
says...
Post by Rich Assaf
This was driving me crazy, since I had some deeply buried custom controls that were checking for type('dParameter') == 'D', and couldn't figure out why my code was generating unexpected values...
For those unaware, using d = New Date() generates a type 'O' (Object), and not a type 'D' (as I expected). I know this is WAD, but still annoying....
function SToD(cDateStr)
// return a date value from DToS value ('20081116')
local d
d = new date()
d.date = val( right(cDateStr,2) )
d.month = val(substr(cDateStr,5,2) )-1 // date object starts with 0 for Jan, 11 for Dec
d.year = val( left(cDateStr,4 ))
return ctod(dtoc(d)) // Date class creates an Object, Not a date....
Converting date values via DToS() and SToD() avoids problems with local century settings, international date format issues, etc.
Rich
Rich,

Or you could do this:

o = new date( )
d = dtToD( o )
? type( "o" )
? type( "d" )
--
Geoff Wass [dBVIPS]
Montréal, Québec, Canada

.|.|.| dBASE info at http://geocities.com/geoff_wass |.|.|.
.|.|.| ---------------------------------------------------------- |.|.|.
.|.|.| IT Consultant http://Geoff_Wass.com |.|.|.
Bruce Beacham
2008-10-16 09:21:03 UTC
Permalink
Post by Rich Assaf
For those unaware, using d = New Date() generates a type 'O'
(Object), and not a type 'D' (as I expected). I know this is WAD,
but still annoying....
? type("new string()") // "O"
? type("new date()") // "O"


Bruce Beacham
unknown
2008-10-16 15:57:33 UTC
Permalink
On Thu, 16 Oct 2008 00:03:47 -0400 Rich Assaf
Sender: Rich Assaf <***@hotmail.com>
wrote the following in:
Newsgroup: dbase.programming
Post by Rich Assaf
This was driving me crazy, since I had some deeply buried custom controls that were checking for type('dParameter') == 'D', and couldn't figure out why my code was generating unexpected values...
For those unaware, using d = New Date() generates a type 'O' (Object), and not a type 'D' (as I expected). I know this is WAD, but still annoying....
An even simpler StringToDate function :-)


function DtosToDate(cDateAsDtos)
return dttod(new date(transform(cDateAsDtos, "@R 9999 99 99")))


Ivar B. Jessen
Rich Assaf
2008-10-18 00:26:14 UTC
Permalink
Ivar,

Very slick, indeed....
Post by unknown
An even simpler StringToDate function :-)
function DtosToDate(cDateAsDtos)
Rich

Wagner
2008-10-16 18:00:04 UTC
Permalink
Post by Rich Assaf
This was driving me crazy, since I had some deeply buried custom controls that were checking for type('dParameter') == 'D', and couldn't figure out why my code was generating unexpected values...
For those unaware, using d = New Date() generates a type 'O' (Object), and not a type 'D' (as I expected). I know this is WAD, but still annoying....
function SToD(cDateStr)
// return a date value from DToS value ('20081116')
local d
d = new date()
d.date = val( right(cDateStr,2) )
d.month = val(substr(cDateStr,5,2) )-1 // date object starts with 0 for Jan, 11 for Dec
d.year = val( left(cDateStr,4 ))
return ctod(dtoc(d)) // Date class creates an Object, Not a date....
Converting date values via DToS() and SToD() avoids problems with local century settings, international date format issues, etc.
Rich
/*
Objetivo: Recebe o string de data no padrão AAAAMMDD e retorna um campo data
*/
function StoD
local cDia,cMes,cAno,dData
local PadraoDeData,cData

parameters String

cData = String

cAno = substr(cData,1,4)
cMes = substr(cData,5,2)
cDia = substr(cData,7,8)

PadraoDeData = set("DATE")

if PadraoDeData = 'AMERICAN'
PadraoDeData = [MM/DD/YY]
elseif PadraoDeData = 'ANSI'
PadraoDeData = [YY.MM.DD]
elseif PadraoDeData = 'BRITISH'
PadraoDeData = [DD/MM/YY]
elseif PadraoDeData = 'FRENCH'
PadraoDeData = [DD/MM/YY]
elseif PadraoDeData = 'GERMAN'
PadraoDeData = [DD.MM.YY]
elseif PadraoDeData = 'ITALIAN'
PadraoDeData = [DD-MM-YY]
elseif PadraoDeData = 'JAPAN'
PadraoDeData = [YY/MM/DD]
elseif PadraoDeData = 'USA'
PadraoDeData = [MM-DD-YY]
elseif PadraoDeData = 'MDY'
PadraoDeData = [MM/DD/YY]
elseif PadraoDeData = 'DMY'
PadraoDeData = [DD/MM/YY]
elseif PadraoDeData = 'YMD'
PadraoDeData = [YY/MM/DD]
endif

PadraoDeData := strtran(PadraoDeData,"DD","D")
PadraoDeData := strtran(PadraoDeData,"MM","M")
PadraoDeData := strtran(PadraoDeData,"YYYY","Y")
PadraoDeData := strtran(PadraoDeData,"YY","Y")

cData = PadraoDeData
cData = strtran(cData,'D',cDia)
cData = strtran(cData,'M',cMes)
cData = strtran(cData,'Y',cAno)

if Val(cDia) < 1 .or.;
Val(cDia) > 31 .or.;
Val(cMes) < 1 .or.;
Val(cMes) > 12 .or.;
Val(cAno) < 1
cData = { / / }
else
cData = CtoD(cData)
endif

return cData

/*
Parametros : VALOR : Texto a ser analisado
ANTERIOR : Valor a ser substituido
NOVO : Valor a substituir
QTDE : Quantidade de ocorrencias, se não passar considera todas.
Objetivo : Substitui a string ANTERIOR pela NOVO dentro da string VALOR
Retorno : texto substituido.
*/
function strtran
local cRet, nLen, nQtde,xTexto,QtdeVez

Parameters xValor, Anterior,Novo, QTDE


if type("xValor") = "O"
if type("xValor.value") <> 'U'
xTexto = xValor.value
else
xTexto = xValor
endif

else
xTexto = xValor
endif

if Valtype(xTexto) = "N"
xTexto = str(xTexto,18,2)
elseif Valtype(xTexto) = "D"
xTexto = DtoC(xTexto)
elseif valtype(xTexto) <> "C"
xTexto = ""
endif

if type("Anterior") <> "C"
Anterior = ""
endif

if type("Novo") <> "C"
Novo = ""
endif

if type("Qtde") <> "N"
QtdeVez = 0
else
QtdeVez = Qtde
endif


nQtde = 0
cRet = ""
nLen = len(xTexto)
nRocha = 0
do while len(xTexto) > 0
temp = substr(xTexto,1,len(Anterior))
if temp == Anterior
cRet = cRet + Novo
xTexto = substr(xTexto,len(Anterior)+1)
nQtde ++
if nQtde == QtdeVez
cRet = cRet + xTexto
Exit
endif
else
cRet = cRet + substr(xTexto,1,1)
xTexto = substr(xTexto,2)
endif
nRocha++
if nRocha > nLen
msgbox('Erro')
exit
endif
enddo


return cRet
Loading...