FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour How to convert a Date to an Unix TimeStamp?
Posts: 1067
Joined: Wed Nov 09, 2005 02:17 AM
How to convert a Date to an Unix TimeStamp?
Posted: Mon Jan 29, 2024 12:22 PM

Hi Guys,

How could I convert a date/time to an Unix TimeStamp?

Sds,
Vilian F. Arraes
vilian@vfatec.com.br
Belém-Pa-Brazil
Posts: 179
Joined: Fri Dec 07, 2007 01:26 PM
Re: How to convert a Date to an Unix TimeStamp?
Posted: Mon Jan 29, 2024 01:08 PM
Posts: 1067
Joined: Wed Nov 09, 2005 02:17 AM
Re: How to convert a Date to an Unix TimeStamp?
Posted: Mon Jan 29, 2024 01:19 PM
Ruth,
I tried it, but didn´t work here. IF you compare the result with that generated from https://www.timestamp-converter.com/, they aren´t the same.
Sds,
Vilian F. Arraes
vilian@vfatec.com.br
Belém-Pa-Brazil
Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
Re: How to convert a Date to an Unix TimeStamp?
Posted: Mon Jan 29, 2024 01:26 PM

We have already implemented those functions for FWH already

Please lets wait for Mr. Rao comments about them

regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: How to convert a Date to an Unix TimeStamp?
Posted: Mon Jan 29, 2024 01:37 PM
* New functions: (valtostr.prg)
FW_DateToUnix( dDate/tDateTime ) --> nUnixTimeStamp in MilliSeconds
FW_UnixToDate( nUnixTimeStamp(inMilliSeconds) ) --> tDateTime
Code (fw): Select all Collapse
#ifdef __XHARBOUR__
   #xtranslate HB_STOT( <c> ) => STOT( <c> )
   #xtranslate HB_DateTime()  => DateTime()
#endif



//----------------------------------------------------------------------------//

function FW_DateToUnix( tDateTime )  // ( dDate or tDateTime ) --> nMilliSecs

   DEFAULT tDateTime := HB_DateTime()

return INT( ( FW_DTOT( tDateTime ) - HB_STOT( "19700101000000" ) ) * 86400000.0 )

//----------------------------------------------------------------------------//

function FW_UnixToDate( nMilliSecs ) // --> tDateTime

   if ValType( nMilliSecs ) == "C"
      nMilliSecs := Val( nMilliSecs )
   else
      DEFAULT nMilliSecs := 0
   endif

return HB_STOT( "19700101000000" ) + ( nMilliSecs / 86400000.0 )

//----------------------------------------------------------------------------//
Please keep in mind that the FWH functions deal with Unix time in MilliSeconds. Not seconds.
Depending on the requirement, you need to provide the conversion ( *1000 or /1000 )
Regards



G. N. Rao.

Hyderabad, India
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: How to convert a Date to an Unix TimeStamp?
Posted: Mon Jan 29, 2024 01:42 PM
vilian wrote:Hi Guys,

How could I convert a date/time to an Unix TimeStamp?
If you want to use this functionality with MySql, you can use the MySql built-in functions
Code (fw): Select all Collapse
UNIX_TIMESTAMP( <datetime> ) 
FROM_UNIXTIM( nUnixTime )
Please note that in this case unix timestamp or in Seconds. Not milli seconds. We need to take care of conversions.

Please note that FWH functions deal with the UnixTime in millliseconds whereas MySql functions deal with Seconds.

Examples:
Code (fw): Select all Collapse
? oCn:QueryResult( "SELECT UNIX_TIMESTAMP( ? )",  { HB_DateTime() } )
? oCn:QueryResult( "SELECT FROM_UNIXTIME( ? , '%d-%m-%Y %H:%i:%S')", { nUnixTimeInSeconds} )
Also while dealing with TimeStamps in MySql, we need to keep in mind automatic TimeZone conversions from UTC.
Regards



G. N. Rao.

Hyderabad, India
Posts: 1067
Joined: Wed Nov 09, 2005 02:17 AM
Re: How to convert a Date to an Unix TimeStamp?
Posted: Mon Jan 29, 2024 02:08 PM
Thank you Mr Rao,

If I try with your function, I'm having this result: 168486480000 for 2023-05-23T18:00:00
Code (fw): Select all Collapse
SET DATE BRIT
myDate := Hb_Dtot(Ctod("23/05/2023"),"18:00:00")

? FW_DateToUnix( MyDate ) //168486480000
But in https://www.timestamp-converter.com/, I'm having a different result(as you can view bellow). Do you know why ?

Sds,
Vilian F. Arraes
vilian@vfatec.com.br
Belém-Pa-Brazil
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: How to convert a Date to an Unix TimeStamp?
Posted: Mon Jan 29, 2024 02:43 PM
Both are same.
In that website there is an implicit conversion from your time zone to UTC.
Try this code:
Code (fw): Select all Collapse
? FW_DateToUnix( UTC_TIMESTAMP() )
Note: UTC_TIMESTAMP() is an FWH function
Regards



G. N. Rao.

Hyderabad, India
Posts: 1067
Joined: Wed Nov 09, 2005 02:17 AM
Re: How to convert a Date to an Unix TimeStamp?
Posted: Mon Jan 29, 2024 02:53 PM
Thank you, now it's working perfectly :)
Sds,
Vilian F. Arraes
vilian@vfatec.com.br
Belém-Pa-Brazil

Continue the discussion