Page 1 of 1

UNIX to date

PostPosted: Thu May 11, 2023 10:30 am
by Silvio.Falconi
I must converte a number -> time_next_update_unix

Image

I found this function but not run ok

Code: Select all  Expand view

FUNCTION UnixToDate(nUnix)
   LOCAL nSecs, dDate, cTime, dDateTime
       nSecs := nUnix - 28800 // correzione fuso orario
   dDate := CTOD("01/01/1970")
   cTime := stot(nSecs / 1000)
   
   dDateTime := dDate + cTime
   
   RETURN dDateTime


chat Open Ai give me this but not run

Code: Select all  Expand view
FUNCTION UnixToDate(nUnix)
   LOCAL nSecs, dDate, cTime, dDateTime
   
   // Converti i secondi Unix in una data Clipper e un valore di tempo
   nSecs := nUnix - 28800 // correzione fuso orario
   dDate := CTOD("01/01/1970")
   cTime := CTOT(TIME(SECONDS(nSecs) * 1000))
   
   // Combina la data e l'ora Clipper in un valore di data e ora Clipper
   dDateTime := dDate + cTime
   
   // Restituisci la data e ora convertite
   RETURN dDateTime
 

ho I can resolve ?

Re: UNIX to date

PostPosted: Fri May 12, 2023 5:22 am
by nageswaragunupudi
Once we know that 1970-01-01 is the epoch date in Unix, it is very easy to write the conversion:
Code: Select all  Expand view
function UnixToDateTime( nUnixDate )
return HB_STOT( "19700101000000" ) + ( nUnixDate / 24.0 / 3600.0 )

This returns the harbor DateTime value.

Testing from India:
present UTC nUnixTime is 1683872102
? tUTC := UnixToDateTime( nUnixTime ) // 12-05-2023 06:15:02
? tLocal := FW_UTCTOLOCAL( tUtc ) // 12-05-2023 11:45:02

Compare your test results with
https://www.epochconverter.com/

Re: UNIX to date

PostPosted: Fri May 12, 2023 7:47 am
by Silvio.Falconi
nageswaragunupudi wrote:Once we know that 1970-01-01 is the epoch date in Unix, it is very easy to write the conversion:
Code: Select all  Expand view
function UnixToDateTime( nUnixDate )
return HB_STOT( "19700101000000" ) + ( nUnixDate / 24.0 / 3600.0 )

This returns the harbor DateTime value.

Testing from India:
present UTC nUnixTime is 1683872102
? tUTC := UnixToDateTime( nUnixTime ) // 12-05-2023 06:15:02
? tLocal := FW_UTCTOLOCAL( tUtc ) // 12-05-2023 11:45:02

Compare your test results with
https://www.epochconverter.com/


thanks I resolve