How to convert a Date to an Unix TimeStamp?

How to convert a Date to an Unix TimeStamp?

Postby vilian » 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
User avatar
vilian
 
Posts: 977
Joined: Wed Nov 09, 2005 2:17 am
Location: Brazil

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

Postby Ruth » Mon Jan 29, 2024 1:08 pm

User avatar
Ruth
 
Posts: 169
Joined: Fri Dec 07, 2007 1:26 pm

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

Postby vilian » Mon Jan 29, 2024 1: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
User avatar
vilian
 
Posts: 977
Joined: Wed Nov 09, 2005 2:17 am
Location: Brazil

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

Postby Antonio Linares » Mon Jan 29, 2024 1: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
User avatar
Antonio Linares
Site Admin
 
Posts: 42099
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

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

Postby nageswaragunupudi » Mon Jan 29, 2024 1:37 pm

* 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 )
Regards

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

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

Postby nageswaragunupudi » Mon Jan 29, 2024 1: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: 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.
Regards

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

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

Postby vilian » Mon Jan 29, 2024 2: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: 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
Sds,
Vilian F. Arraes
vilian@vfatec.com.br
Belém-Pa-Brazil
User avatar
vilian
 
Posts: 977
Joined: Wed Nov 09, 2005 2:17 am
Location: Brazil

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

Postby nageswaragunupudi » Mon Jan 29, 2024 2:43 pm

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
Regards

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

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

Postby vilian » Mon Jan 29, 2024 2:53 pm

Thank you, now it's working perfectly :)
Sds,
Vilian F. Arraes
vilian@vfatec.com.br
Belém-Pa-Brazil
User avatar
vilian
 
Posts: 977
Joined: Wed Nov 09, 2005 2:17 am
Location: Brazil


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 78 guests