Date value to unix timestamp


is it possible to convert a Date variable value to unix timestamp number format ?

Is this what you are looking for ? - it has general multiplatform code for a lower resolution timestamp as well as the windows stuff…

timestamp link

Place the following method somewhere in a module:

Function UnixTime(Extends d As Date) As Integer return d.TotalSeconds - 2082844800 End Function

You can now get the unix timestamps from your date objects as follow:

Function UnixTime(Extends d As Date) As Integer
  return d.TotalSeconds - 2082844800 
End Function

Sorry, second piece of code should have been:

  Dim d as new Date
  MsgBox Str(d.UnixTime)

MsgBox format(d.UnixTime,"#") else you end up in Scientific notation (just mentioned on another time related post a few min ago)

I like to give some background information because the question was in Getting started

Xojo help states

UNIX date is based on [quote]Thu Jan 1 00:00:00 CET 1970[/quote]

So the value 2082844800 above is just the difference in seconds between these base dates .

You can calculate it like this:

dim unixDate As new Date(1970, 1, 1, 0, 0, 0) MsgBox Format(unixDate.TotalSeconds, "#")

Thanks for the info

I’ve been having some issues with this as well.

I have a unixtime time stamp in my MySQl DB that was created by a Java server application.
The Xojo Application is a tool we provide for viewing this data.
I convert the unixtime stamp with a method that contains the following:

Dim d As New Date
Dim epochDate As Date =New Date(1970, 1, 1, 0, 0, 0,0.0) 'used to calculate the amount of seconds difference from RB epoch - 1904 to Unix - 1970
‘d.GMTOffset =0
d.TotalSeconds =( (milliseconds)/1000.0) + epochDate.TotalSeconds’ I was using this value but it doesn’t make any difference to the out come - 2082844800 seconds

Return d

I then use the Date object to format the results and display it in a readable way.

date.Hour + “:”+ date.Minute + “:” … etc

The problem is the time is always 1 hour behind what it should be. The Java code displays the unix timestamp correctly.

I’ve played around with the GMTOffset but not seen any changes. Is there anything else I can try?

  dim unix_epoch As new Date(1970, 1, 1, 0, 0, 0)
  dim today As new Date()
  Dim gmtOffset As Integer = Xojo.Core.TimeZone.Current.SecondsFromGMT
  dim unix_time as Integer = (today.TotalSeconds - unix_epoch.TotalSeconds) - gmtOffset