Page 1 of 1

How to convert a Date to an Unix TimeStamp?

PostPosted: Mon Jan 29, 2024 12:22 pm
by vilian
Hi Guys,

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

Re: How to convert a Date to an Unix TimeStamp?

PostPosted: Mon Jan 29, 2024 1:08 pm
by Ruth

Re: How to convert a Date to an Unix TimeStamp?

PostPosted: Mon Jan 29, 2024 1:19 pm
by vilian
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.

Re: How to convert a Date to an Unix TimeStamp?

PostPosted: Mon Jan 29, 2024 1:26 pm
by Antonio Linares
We have already implemented those functions for FWH already

Please lets wait for Mr. Rao comments about them

Re: How to convert a Date to an Unix TimeStamp?

PostPosted: Mon Jan 29, 2024 1:37 pm
by nageswaragunupudi
* New functions: (valtostr.prg)
FW_DateToUnix( dDate/tDateTime ) --> nUnixTimeStamp in MilliSeconds
FW_UnixToDate( nUnixTimeStamp(inMilliSeconds) ) --> tDateTime

Code: Select all  Expand view
#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 )

Re: How to convert a Date to an Unix TimeStamp?

PostPosted: Mon Jan 29, 2024 1:42 pm
by nageswaragunupudi
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: Select all  Expand view
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: Select all  Expand view
? 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.

Re: How to convert a Date to an Unix TimeStamp?

PostPosted: Mon Jan 29, 2024 2:08 pm
by vilian
Thank you Mr Rao,

If I try with your function, I'm having this result: 168486480000 for 2023-05-23T18:00:00

Code: Select all  Expand view

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 ?

Image

Re: How to convert a Date to an Unix TimeStamp?

PostPosted: Mon Jan 29, 2024 2:43 pm
by nageswaragunupudi
Both are same.
In that website there is an implicit conversion from your time zone to UTC.
Try this code:
Code: Select all  Expand view
? FW_DateToUnix( UTC_TIMESTAMP() )

Note: UTC_TIMESTAMP() is an FWH function

Re: How to convert a Date to an Unix TimeStamp?

PostPosted: Mon Jan 29, 2024 2:53 pm
by vilian
Thank you, now it's working perfectly :)