Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Compare date+hour

82 views
Skip to first unread message

Paulo Oliveira

unread,
Jun 4, 2013, 10:29:52 AM6/4/13
to
Hi all,

I have a date1 as date, cHour1 as string,date2 as date, cHour2 as string

I want compare date1+hour1=date2+hour2

any suggestion?

Paulo Oliveira

Pilks

unread,
Jun 4, 2013, 10:52:59 AM6/4/13
to
Paulo
> I want compare date1+hour1=date2+hour2
>
Why not:
dtos(date1)+hour1=dtos(date2)+hour2
?

Richard
>
>
> any suggestion?
>
>
>
> Paulo Oliveir

Wolfgang Riedmann

unread,
Jun 4, 2013, 11:57:41 AM6/4/13
to
Hi Paulo,

> I have a date1 as date, cHour1 as string,date2 as date, cHour2 as
> string
>
> I want compare date1+hour1=date2+hour2

use a numeric value as hours with fractions:

nHours1 := ( Date1 - ConDate( 1900, 1, 1 ) * 24 ) + ( Secs( Hour1 ) /
3600 )

nHours2 := ( Date2 - ConDate( 1900, 1, 1 ) * 24 ) + ( Secs( Hour2 ) /
3600 )

Wolfgang


--

Arne Ortlinghaus

unread,
Jun 4, 2013, 12:05:28 PM6/4/13
to
I use always the SQL date time string format of the form YYYY-MM-DD HH:MM:SS
Below are some conversion functions.

Arne Ortlinghaus
ACS Data Systems

function MakeTimeStampBase(dDate, nSeconds )
//p Erzeugen eines Timestamps aus Datum und Sekunden
//a dDate - Datum
//a nSeconds - Int Sekunden seit Mitternacht
//d Wenn nSeconds < 0, wird dDate um eins verkleinert und nSeconds
hochgez�hlt
//r Timestamp ODBC-Format (formattierter String 19 Zeichen)

local cTimestamp as string
local wHours as word
local wMinutes as word
local cFrac as string
local cTime as string

if IsNil(dDate)
dDate := Today()
endif
if IsNil (nSeconds)
nSeconds := 0 // gibt sonst Probleme mit Offene Posten in Radix
endif

while nSeconds < 0
dDate := dDate-1
nSeconds += 86400
enddo

cFrac := LTrim(__Str( Frac( nSeconds ) ))
cFrac := SubStr2( cFrac, At2( ".", cFrac ) + 1 )
cFrac := PadR( cFrac, 9, "0" )

nSeconds := Integer( nSeconds )

wHours := Integer( nSeconds / 3600 )
nSeconds := MOD( nSeconds, 3600 )
wMinutes := Integer( nSeconds / 60 )
nSeconds := MOD( nSeconds, 60 )

cTime := StrZero( wHours, 2, 0 ) + ":" + ;
StrZero( wMinutes, 2, 0 ) + ":" + ;
StrZero( nSeconds, 2, 0 ) + "." + ;
cFrac

cTimestamp := Left(DToCSQLBase( dDate ) + " " + cTime, 19)

return cTimeStamp

func DToCSQLBase ( dDate as date ) as
string
local nDay as usual // usual wegen Problemen mit StrZero
local nMonth as usual
local nYear as usual

nDay := Day(dDate)
nMonth := Month(dDate)
nYear := Year(dDate)

return Str(nYear, 4) +"-" + StrZero (nMonth, 2)+"-"+ StrZero (nDay, 2)


func TimeStampAddTime (cT, nAddDays, nAddSeconds)
//p Addieren/Subtrahieren Monate
//a cT - Timestamp-String
//a nAdddays - Int: Anzahl Tage dazu (Negative weg)
//a nAddSeconds - Int: Anzahl Sekunden dazu (Negative weg)
//r Timestamp
local a as array
local d as date
local nSeconds as int
if Empty(cT)
return cT
endif
a := TimeStampToDateandTime (cT)
d := a[1]
nSeconds := a[2]
if !Empty(nAddDays)
d += nAddDays
endif
if !Empty(nAddSeconds)
nSeconds += nAddSeconds
endif
do while nSeconds >= 86400
nSeconds -= 86400
d += 1
enddo
do while nSeconds < 0
nSeconds += 86400
d -= 1
enddo
return MakeTimeStampBase(d, nSeconds)
function TimeStampDifference(cT1, cT2, nFormat)
//p Differenz von zwei Zeiten cT1-cT2
//a cT1 - String: Timestamp
//a cT2 - String: Timestamp
//a nFormat - Int: 2: Datum, 3: Zeit in Minuten, 4: Zeit in Sekunden
local a2, a1 as array
local nSec as int
a1 := TimeStampToDateandTime (cT1)
a2 := TimeStampToDateandTime (cT2)
if nFormat == 2
return a1[1] - a2[1]
endif

nSec := a1[2] - a2[2]
do while a1[1] > a2[1]
a1[1] := a1[1]-1
nSec += 86400
enddo
do while a1[1] < a2[1]
a1[1] := a1[1]+1
nSec -= 86400
enddo
if nFormat == 3
return nSec / 60
elseif nFormat == 4
return nSec
else
MsgInternalError("TimeStampDifference - Wrong Format: " +
Typevalue2String(nFormat))
return 0
endif
function TimeStampToDateandTime (cTimeStamp)
//p Aufteilen eines Timestamps in Datum und Zeit
//a Timestamp ODBC-Format (formattierter String 19 Zeichen)
//r Array: {dDate - Datum, nSeconds - Int Sekunden seit Mitternacht}
local dDate as date
local nSeconds as int
dDate := CToDAnsi(cTimeStamp)
cTimeStamp := SubStr(cTimeStamp, 12)
nSeconds := Val(SubStr(cTimeStamp, 1, 2)) * 3600
nSeconds += Val(SubStr(cTimeStamp, 4, 2)) * 60
nSeconds += Val(SubStr(cTimeStamp, 7, 2))
return {dDate, nSeconds}

0 new messages