Page 1 of 1

datetime to unix time converter

Posted: Thu Dec 28, 2017 12:41 pm
by rdonnay
Does anyone have a function that converts Xbase++ date and time to Unix time?

Re: datetime to unix time converter

Posted: Thu Dec 28, 2017 12:59 pm
by patito
Hi Roger

Unix time function, see library ot4xb. dll by Pablo Botella

Best Regard
Hector Pezoa

Feliz Navidad y Prospero Año Nuevo

Re: datetime to unix time converter

Posted: Thu Dec 28, 2017 1:03 pm
by patito
Hi Roger

You can download this library from this site

http://www.xbwin.com/download/

Best Regard

Hector Pezoa

Re: datetime to unix time converter

Posted: Fri Dec 29, 2017 8:57 am
by rdonnay
After posting my message, I realized that it would be a simple algorithm, written entirely in Xbase++. Here is the code:

Code: Select all

FUNCTION DC_DateTime2UnixTime( dCurrDate, cCurrTime )

LOCAL nSeconds := 0, nYear, dDate, nDays

DEFAULT dCurrDate := Date(), ;
        cCurrTime := Time()

nYear := 1970

DO WHILE nYear < Year(dCurrDate)

  dDate := StoD(Str(nYear,4,0)+'0101')
  IF IsLeap(dDate)
    nDays := 366
  ELSE
    nDays := 365
  ENDIF

  nSeconds += (nDays * 86400)
  nYear ++

ENDDO

nDays := Doy(dCurrDate-1)
nSeconds += (nDays * 86400)
nSeconds += TimeToSec(cCurrTime)

RETURN nSeconds

Re: datetime to unix time converter

Posted: Fri Dec 29, 2017 1:58 pm
by rdonnay
Here is the reverse function:

Code: Select all

FUNCTION DC_UnixTime2DateTime( nSeconds )

LOCAL dDate, cTime, nYear, nDays, nSecsFromMidnight

IF Empty(nSeconds)
  nSeconds := DC_DateTime2UnixTime()
ENDIF

nYear := 1970

DO WHILE nSeconds > 0

  dDate := StoD(Str(nYear,4,0)+'0101')
  IF IsLeap(dDate)
    nDays := 366
  ELSE
    nDays := 365
  ENDIF

  nSeconds -= (nDays * 86400)
  nYear ++

ENDDO

dDate := StoD(Str(nYear,4,0)+'0101')
nSeconds *= -1
nDays := nSeconds/86400
nSecsFromMidnight := (86400*(nDays - Int(nDays)))
dDate -= (Int(nDays) + 1)
cTime := SecToTime(86400-nSecsFromMidnight)

RETURN {dDate, cTime}