Seconds to time oddness

When using this the output is wrong.

var time as New DateTime(3700,TimeZone.Current)
msgbox Format(time.Hour, "00") + ":" + Format(time.Minute, "00") + ":" + Format(time.Second, "00")

This should output 01:01:40 but it outputs 02:01:40
Not sure why, anyone?

Maybe the 3700 is the number of seconds in UTC time which is then converted to the current time zone (which if you are in mainland Europe, would be 1 hour ahead).

I get: 19:01:40
The SQLDate is 1969-12-31 and for you (I guess) is 1970-01-01, right?

From the documentation:

The number of seconds since the first 1 January 1970, 00:00 in the specified TimeZone. If no time zone was specified, the time zone is that of the device.

I’m not a native English speaker but for me this means that the code should put me also at 01:01:40 of 1970-01-01, good thing is that it also mentions:

This value is also referred to as the Unix epoch, Unix time or Unix timestamp.

and a quick search in Google gives this information:

Unix time is a date and time representation widely used in computing. It measures time by the number of seconds that have elapsed since 00:00:00 UTC on 1 January 1970, the beginning of the Unix epoch, less adjustments made due to leap seconds.

That makes more sense to me, it is NOT the number of seconds since (the first) 1 January 1970 in the specified TimeZone but in UTC.

Funny, this works.

var zone As New TimeZone(0)
var time as New DateTime(3700,zone)
msgbox Format(time.Hour, "00") + ":" + Format(time.Minute, "00") + ":" + Format(time.Second, "00")

FWW this code is about 18x faster (tested in a for/next loop doing it 50000 times).

var secs as double = 3700
var hours, minutes, seconds as int64
hours = secs \ 3600
secs = secs - (3600 * hours)
minutes = secs \ 60
secs = secs - (60 * minutes)
seconds = secs \ 1
secs = secs - (secs * seconds)
msgbox format(hours,"00")+":"+format(minutes,"00")+":"+format(seconds,"00")

Because you’re telling it TimeZone = 0 = GMT.
Do you not understand that you’re asking the framework for 3700 seconds after the epoch in the passed TimeZone?

It “works” because ‘time’ is now UTC and not your local time zone, is the exact same point of time:

  • your 02:01:40
  • UTC 01:01:40
  • my 19:01:40

that’s the idea, no?

Maybe you only care about the time and you don’t need date or time zones, so in your case a different approach is needed and DateTime is not the best.

1 Like

No, I just need a way to convert seconds in a time 00:00:00 format. Zones are not important in this case.
Anyhow, I use the second methode now. It way faster for doing this (which was provided by ChatGPT).

1 Like

I now asked ChatGPT to optimise it more for speed and it comes with this code that is now doing it 21x times faster. :slight_smile:

var secs as double = 3700
var hours, minutes, seconds as Integer
hours = secs \ 3600
secs = secs Mod 3600
minutes = secs \ 60
seconds = secs Mod 60
msgbox Format(hours, "00") + ":" + Format(minutes, "00") + ":" + Format(seconds, "00")

Why you need that instead of:

seconds = secs

Edit: Oh, I see that now ChatGPT gave you a better answer.

1 Like

Because AI writes bad code.

4 Likes

You could argue why ChatGPT did first gave a solution that is working and faster as using Date, but not the fastest solution.
But asking it again to optimise it for speed, it did do that (read: faster code). So probably my question wasn’t clear enough the first time.

Anyhow, it is worth trying to optimise code using ChatGPT, so it seems.

Somewhere off in the distance I hear my boss saying “stop playing with this ChatGPT nonsense and GET BACK TO WORK!” :stuck_out_tongue:

1 Like

Well, it can help you too. :slight_smile:
I have manage to optimise code in several circumstances with ChatGPT. Sometimes even very complex things.
So your boss will be very happy instead, I guess. :slight_smile:

If only there were some built-in system function for doing this and someone who’s figured out how to use that function from within Xojo…

2 Likes
// This is slightly faster, sadly it's the usual way humans always did it before ChatGPT

Var hours, minutes, seconds, secs As Integer

secs = 3700

hours = secs \ 3600
secs = secs - (hours * 3600)
minutes = secs \ 60
seconds = secs - (minutes * 60)

msgbox Format(hours, "00") + ":" + Format(minutes, "00") + ":" + Format(seconds, "00")