UNIX to date

UNIX to date

Postby Silvio.Falconi » Thu May 11, 2023 10:30 am

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 ?
Since from 1991/1992 ( fw for clipper Rel. 14.4 - Momos)
I use : FiveWin for Harbour November 2023 - January 2024 - Harbour 3.2.0dev (harbour_bcc770_32_20240309) - Bcc7.70 - xMate ver. 1.15.3 - PellesC - mail: silvio[dot]falconi[at]gmail[dot]com
User avatar
Silvio.Falconi
 
Posts: 7061
Joined: Thu Oct 18, 2012 7:17 pm

Re: UNIX to date

Postby nageswaragunupudi » Fri May 12, 2023 5:22 am

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/
Regards

G. N. Rao.
Hyderabad, India
User avatar
nageswaragunupudi
 
Posts: 10643
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Re: UNIX to date

Postby Silvio.Falconi » Fri May 12, 2023 7:47 am

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
Since from 1991/1992 ( fw for clipper Rel. 14.4 - Momos)
I use : FiveWin for Harbour November 2023 - January 2024 - Harbour 3.2.0dev (harbour_bcc770_32_20240309) - Bcc7.70 - xMate ver. 1.15.3 - PellesC - mail: silvio[dot]falconi[at]gmail[dot]com
User avatar
Silvio.Falconi
 
Posts: 7061
Joined: Thu Oct 18, 2012 7:17 pm


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 56 guests