Xojo.Core.Date.SecondsFrom1970 is not localized

I have:

dim d as Xojo.Core.Date = Date.Now

I understand from the manual that Date.Now is giving the time/date the timezone of my device.
Why then is Xojo.Core.Date.SecondsFrom1970 not corrected to this timezone?

[quote=171712:@Jaap Cammeraat]I have:

dim d as Xojo.Core.Date = Date.Now

I understand from the manual that Date.Now is giving the time/date the timezone of my device.
Why then is Xojo.Core.Date.SecondsFrom1970 not corrected to this timezone?[/quote]

I may be mistaken, but TotalSeconds and SecondsFrom1970 are inherently a local reference. There is no way you can count seconds from January first, 1970 any other way than locally. You are not going to say “I want to know how many seconds since January 1, 1970 in Barbados”, for instance.

From what I see, secondsFrom1970 gives exactly the same result whether the timezone is set to European Time and US West Coast. As a matter of fact, if you want for instance the value of SecondsFrom1970 in reference to a time zone one hour late, what you are asking for is SecondsFrom1970, January First, 1 AM. Not very logical. That said, if that is what you are after, each hour is 3600 seconds, so it is easy to subtract or add.

Yes I see that too!
The time in my Date object is my local time but SecondsFrom1970 is UTC-0 and that sounds weird!

[quote=171750:@Jaap Cammeraat]Yes I see that too!
The time in my Date object is my local time but SecondsFrom1970 is UTC-0 and that sounds weird![/quote]

How many seconds are there from 00:00 hours, January 1st, 1970 ? If you think about it, the answer is the same wherever in the world.

You do not seem to grasp that the time difference is constant, no matter the timezone, since that time difference is local by essence. The weirdness comes from your misperception of the concept.

Wait. I am in error. SecondsFrom1970 is indeed UTC. Well. If you need it localized, add offset*3600.

Offhand, this sounds like a bug to me.

Edit: Not a bug.

If no time zone is speficied the number of seconds since January, 1st 1970 is the same at any given time all over the world. It’s only different if you use two different time zones for both dates.

Not so sure it is a bug. Since SecondsFrom1970 is a master property used to construct other dates with different timezones, it must be the same worldwide to indicate an absolute time that serves as reference to all date objects.

Otherwise Constructor(secondsFrom1970 As Double, timezone As TimeZone) would become impossible.

I see how a constructor using local SecondsFrom1970 could work, but that would be a mess.

Why not add SecondsFrom1970_Local instead ?

Thinking about it and remembering some of the design decisions, I think you’re right. SecondsFrom1970 is meant to represent an absolute instant in time.

SecondsFrom1970 is a name for the Unix timestamp, which is indeed defined from the start of 1970 in UTC. The property name may be badly chosen, but it is what it appears to be meant to be.

Well I know how to solve but it stays strange to me.
One part of the Date object is locally correct and the other part not.
I’m running my object in UTC+1 so when I ask the SecondsFrom1970 ( = epoch http://en.wikipedia.org/wiki/Unix_time ) then I want seconds that are corrected to my TimeZone.

I don’t care if it stays this way.
But please mention this choice and working in the manual and don’t change it ever!

I believe this is already mentioned on the SecondsFrom1970 section:

I’m sorry but I can’t understand this.
This is quite easy in the “old” framework.
Can we have an example to convert a date (whatever timezone) to a local timezone date and reverse ?

http://developer.xojo.com/xojo-core-date$TimeZone

Create a new date with the specified timezone from the date your have (in whatever timezone):

[code]Using Xojo.Core
Dim myDate As Date = Date.Now ’ current date/time in my time zone (EDT)

’ Convert to GMT
Dim GMTZone As New TimeZone(“GMT”)
Dim GMTDate As New Date(myDate.SecondsFrom1970, GMTZone)[/code]

Thanks Paul, that’s in the manual :wink:
I was looking for both ways…
I’ve created 2 fonctions that work fine but there’s maybe a better way to do this :

Public Function DatetoGMT(d as xojo.Core.Date) as xojo.Core.Date Dim gmtOffset As Integer Dim di As New DateInterval gmtOffset = Xojo.Core.TimeZone.Current.SecondsFromGMT di.Seconds = gmtOffset Dim d2 As xojo.Core.Date = d - di return d2 End Function

Public Function DateFromGMT(d as xojo.Core.Date) as xojo.Core.Date Dim gmtOffset As Integer Dim di As New DateInterval gmtOffset = Xojo.Core.TimeZone.Current.SecondsFromGMT di.Seconds = gmtOffset Dim d2 As xojo.Core.Date = d + di return d2 End Function

I really think you are mistaken about localizing secondsFrom1970. It is a UTC common reference. Not a local date.

https://en.wikipedia.org/wiki/Unix_time

Something like this seems simpler:

[code]Public Function DateToGMT(origDate As Xojo.Core.Date) as Xojo.Core.Date
Dim GMTZone As New Xojo.Core.TimeZone(“GMT”)
Dim GMTDate As New Xojo.Core.Date(origDate.SecondsFrom1970, GMTZone)

Return GMTDate
End Function[/code]

Public Function DateToLocale(anyDate As Xojo.Core.Date) as Xojo.Core.Date Dim localeDate As New Xojo.Core.Date(anyDate.SecondsFrom1970, Xojo.Core.TimeZone.Current) Return localeDate End Function