datetime to unix time converter

Xbase++ 2.0 Build 554 or later
Post Reply
Message
Author
User avatar
rdonnay
Site Admin
Posts: 4728
Joined: Wed Jan 27, 2010 6:58 pm
Location: Boise, Idaho USA
Contact:

datetime to unix time converter

#1 Post by rdonnay »

Does anyone have a function that converts Xbase++ date and time to Unix time?
The eXpress train is coming - and it has more cars.

patito
Posts: 121
Joined: Tue Aug 31, 2010 9:01 pm

Re: datetime to unix time converter

#2 Post 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
Attachments
ot4xb-time-functions-and-classes (1).rar
Time function
(146.07 KiB) Downloaded 760 times

patito
Posts: 121
Joined: Tue Aug 31, 2010 9:01 pm

Re: datetime to unix time converter

#3 Post by patito »

Hi Roger

You can download this library from this site

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

Best Regard

Hector Pezoa

User avatar
rdonnay
Site Admin
Posts: 4728
Joined: Wed Jan 27, 2010 6:58 pm
Location: Boise, Idaho USA
Contact:

Re: datetime to unix time converter

#4 Post 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
The eXpress train is coming - and it has more cars.

User avatar
rdonnay
Site Admin
Posts: 4728
Joined: Wed Jan 27, 2010 6:58 pm
Location: Boise, Idaho USA
Contact:

Re: datetime to unix time converter

#5 Post 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}
The eXpress train is coming - and it has more cars.

Post Reply