Obtaining GMT seconds from Date function in XojoWeb

I just want to be clear on what is the point of reference for the Date object in a WebApp. My WebApps run on the Linux platform.

If I have

[code]Function UnixTime(Extends d As Date) As Integer
Return d.TotalSeconds - 2082844800
End Function

Function CurrentTime() As Int64
Dim d As New Date
Return d.UnixTime
End Function
[/code]

So my question is: If current GMT seconds are 1402848084, will I get 1402848084 when my WebApp (hosted in the cloud) calls CurrentTime() no matter which timezone the user is physically located in?

[quote=100594:@Scott Rich]I just want to be clear on what is the point of reference for the Date object in a WebApp. My WebApps run on the Linux platform.

If I have

[code]Function UnixTime(Extends d As Date) As Integer
Return d.TotalSeconds - 2082844800
End Function

Function CurrentTime() As Int64
Dim d As New Date
Return d.UnixTime
End Function
[/code]

So my question is: If current GMT seconds are 1402848084, will I get 1402848084 when my WebApp (hosted in the cloud) calls CurrentTime() no matter which timezone the user is physically located in?[/quote]

The time you get from New Date is the host system time. It usually is set to the place where the server is located, and may or may not be accurate. And yes, it does not relate to the location of the user.

There was a discussion about how to get the time from a time server over the Internet, with a method that you may want to use to get the precise time :
https://forum.xojo.com/11738-internet-long-date

Michel,

I like the NTP server solution for obtaining exact GMT time. I notice it takes about 5 secs. for a reply.

However, I am servicing many, many users from multiple timezones and creating/modifying database records contantly where I store/update the timestamp as seconds. Where I’m hitting the DB so often, time is of the essence. The NTP server does not provide a good solution.

I’m thinking that since you confirmed it’s the server’s date that is returned, I should be reasonably safe using that date object. If there is a better solution, I’d love to hear it.

Since you’re using a database, you can get the UTC time from any database I know of.

E.g., to get it from a Xojo database:

dim utctime as Int64 dim rs as RecordSet Dim TimeDB as RealSQLDatabase TimeDB = New RealSQLDatabase if TimeDB.Connect then rs = TimeDB.SQLSelect("SELECT strftime('%s','now');") end utctime = rs.IdxField(1).Int64Value

Also, the Date object has a GMTOffset property – can’t you use that to get the time you want?

The session has a ClientTime property which you can use to get the time zone of the client… Assuming the user has it set correctly on their computer.

[quote=100607:@Scott Rich]I like the NTP server solution for obtaining exact GMT time. I notice it takes about 5 secs. for a reply.

However, I am servicing many, many users from multiple timezones and creating/modifying database records contantly where I store/update the timestamp as seconds. Where I’m hitting the DB so often, time is of the essence. The NTP server does not provide a good solution.

I’m thinking that since you confirmed it’s the server’s date that is returned, I should be reasonably safe using that date object. If there is a better solution, I’d love to hear it.[/quote]

I mentioned the NTP as you seemed to require to the second precision. But for usual day-to-day operations, as long as the time reference is stable, you should be alright with server time. Additionally, a lot of servers keep a good time by using an NTP server themselves.

I agree Michel. Thanks for the NTP option. I have some ideas where I can use this.

Walter, the problem with simply using the GMTOffset on a default Xojo Date object is that the TotalSeconds is “The number of seconds since 12:00AM, January 1, 1904” – Xojo Documentation whereas, Unix time is defined as “The Unix epoch (or Unix time or POSIX time or Unix timestamp) is the number of seconds that have elapsed since January 1, 1970 (midnight UTC/GMT)” - A difference of 2082844800 seconds which you have to account for.

The key here is to be consistent with which scheme you choose. Since I interface with several Linux/Unix based systems that provide a Unix timestamp in seconds, I needed a way to ensure Unix timestamps instead of Xojo timestamps.

I came up with the following if anyone is interested.

[code]Function UnixTime(Extends d As Date) As Integer
Return d.TotalSeconds - 2082844800

End Function

Function CurrentTime(Optional GetGMT As Boolean) As Int64
Dim d As New Date

if GetGMT then d.GMTOffset = 0.0

Return d.UnixTime
End Function
[/code]

Using these 2 functions,
CurrentTime() = Current local (from the server’s perspective) time in seconds
CurrentTime(True) = Current GMT (again from the perspective of the server) time in seconds

Thanx to all of you for your help!