Numeric value returned by DATEUNIX and TIMEUNIX

Place code snippets and demo code here
Post Reply
SteanX
Posts: 39
Joined: Sat Jun 19, 2021 10:33 pm
Has thanked: 17 times
Been thanked: 6 times

Numeric value returned by DATEUNIX and TIMEUNIX

Post by SteanX »

Hi everyone,

Apologies for the long post :-( All I really wanted was help with code to give me a UNIX TIMESTAMP in integer format, so how to get from 2024-03-11 10:19:12 to "1710145152" . But this is how it developed...

I was wondering if someone could assist me with a better understanding of the data type returned by the Annex DATEUNIX and TIMEUNIX functions?
My understanding was that these would return normal numbers, which in Annex are double precision floats. In the documentation, it says that numbers "...can also store integer numbers with a precision equivalent to 32bits"

To test that the generic float/integer duality is as I think it would be, I used the following code:

Code: [Local Link Removed for Guests]

MAX32 = 2^32 - 1
WLOG STR$(MAX32) & ", " &  STR$(MAX32,"%d") & ", " & STR$(MAX32,"%u")
I get the output as : 4.2949673e+09, -2097152, 4292870144 which is sortof what I would expect since %d treats output as an integer and %u treats output as an unsigned integer, although, looking at it now, I would have expected the %d to return -1 and the %u 4294967295

So I thought OK, if I want to get a Unix Timestamp, all I need to do is compute DATEUNIX(DATE$) + TIMEUNIX(TIME$). But I am not getting the desired result. If I use the following code:

Code: [Local Link Removed for Guests]

D = DATEUNIX(DATE$)
T = TIMEUNIX(TIME$)
C = D+T
WLOG "D: " & STR$(D) & ", " & STR$(D,"%d") & ", " & STR$(D,"%u")
WLOG "T: " & STR$(T) & ", " & STR$(T,"%d") & ", " & STR$(T,"%u")
WLOG "C: " & STR$(C) & ", " & STR$(C,"%d") & ", " & STR$(C,"%u")
I get :
D: 1.710108e+09, 1476395008, 1476395008
T: 37051, 0, 0
C: 1.7101451e+09, -2034237440, 2260729856


I have no idea why the T returns 0 for %d and %u, but at first glance it would seem that the value of C, 2260729856, is a proper timestamp. Except, it isn't because 2260729856 as a UNIX Timestamp is actually 2041-08-21 22:30:56 :-(
Funny Note : This is actually beyond the 32 bit Year 2038 caveat on Unix Timestamps - glad to see that timestamps on my machine have been patched for this :-)

To test a few more variations, I wrote something like this:

Code: [Local Link Removed for Guests]

DIM F$(4) = "%d","%u","%ld","%lu"
FDim = 4

MAX32 = 2^32 - 1
WLOG "---MAX32---"
WLOG MAX32
FOR X=0 To FDim-1  ' This is where UBound would be nice :-) For X = 0 To UBound(F$)
  WLOG F$(X) & " : " & STR$(MAX32,F$(X))
NEXT X

D$ = "11/03/24"
T$ = "08:53:12"
' Just choosing a predefined date here, which we know should have UNIX Timestamp 1710143578
D = DATEUNIX(D$)
T = TIMEUNIX(T$)
C = D+T

WLOG "---D---"
WLOG D
FOR X=0 To FDim-1
  WLOG F$(X) & " : " & STR$(D,F$(X))
NEXT X

WLOG "---T---"
WLOG T
FOR X=0 To FDim-1
  WLOG F$(X) & " : " & STR$(T,F$(X))
NEXT X

WLOG "---C---"
WLOG C
FOR X=0 To FDim-1
  WLOG F$(X) & " : " & STR$(C,F$(X))
NEXT X
which gives me an output like this:

---MAX32---
4.2949673e+09
%d : -2097152
%u : 4292870144
%ld : -2097152
%lu : 4292870144
---D---
1.710108e+09
%d : 1476395008
%u : 1476395008
%ld : 1476395008
%lu : 1476395008
---T---
31992
%d : 0
%u : 0
%ld : 0
%lu : 0
---C---
1.71014e+09
%d : -1778384896
%u : 2516582400
%ld : -1778384896
%lu : 2516582400

So yes, I am a bit baffled about the numbers that DATEUNIX and TIMEUNIX returns and I would very much appreciate if someone could show me how to use these to values to come up with a UNIX TIMESTAMP that is in unsigned long format?
User avatar
cicciocb
Site Admin
Posts: 2056
Joined: Mon Feb 03, 2020 1:15 pm
Location: Toulouse
Has thanked: 439 times
Been thanked: 1354 times
Contact:

Re: Numeric value returned by DATEUNIX and TIMEUNIX

Post by cicciocb »

There is a "simple" mistake in your code (unfortunately you are not the first one) :

The function STR$ requires an additional ,1 if the argument must be considered integer
So, in your case, you can simply do

Code: [Local Link Removed for Guests]

MAX32 = 2^32 - 1
WLOG STR$(MAX32) & ", " &  STR$(MAX32,"%d", 1) & ", " & STR$(MAX32,"%u", 1)
image.png
You do not have the required permissions to view the files attached to this post.
SteanX
Posts: 39
Joined: Sat Jun 19, 2021 10:33 pm
Has thanked: 17 times
Been thanked: 6 times

Re: Numeric value returned by DATEUNIX and TIMEUNIX

Post by SteanX »

Doh, RTFM :-)
Thanks Francesco - sorry for the long post
Post Reply