Android Question Datetime value conversion problem

doncx

Active Member
Licensed User
Longtime User
I am attempting to read a date from an array of bytes.

The byte data format is described as "32-bit time_t low byte first".

The datetime represented by the values in buf below should be within the past 24 hours.

get date from bytes:
Dim buf() As Byte = Array As Byte(0xEE, 0x63, 0xDF, 0x65)
raf.Initialize3(buf,True)
Log(raf.ReadInt(0))

The log result of this, 1.709138926E9, is tantalizing but is not a proper datetime representation.

I believe the values represent a signed date and I'm reading them as an unsigned integer, not a signed long, but I can't figure out the proper conversion.

I would be most appreciative of any assistance.

Thank you
 
Solution
what's the proper way to read/convert this to a B4A Long datetime?

Lol there is no one proper way, but give this a burl:

B4X:
Dim buf() As Byte = Array As Byte(0xEE, 0x63, 0xDF, 0x65)

Dim DT As Long = 0
For I = 3 To 0 Step -1    'start at high byte to simplify shifting
    DT = Bit.ShiftLeftLong(DT, 8) + Bit.And(buf(I), 0xFF)    'convert signed Byte to unsigned
Next
DT = DT * 1000    'convert from time_t seconds to B4A/Java milliseconds (both start at 1/1/1970 epoch)

Log(DT)
Log(DateTime.Date(DT) & " " & DateTime.Time(DT))
Log output:
Waiting for debugger to connect...
Program started.
1709138926000
02/29/2024 03:48:46

MicroDrie

Well-Known Member
Licensed User
Every byte have 8 bits. You have 4 bytes that make 32 bits. The position of a byte has a weight. From left to right (or b4 to b1) you can use the function ((0xff & b4) < 24) | ((0xff & b3) < 16) | ((0xff & b2) < 8) | (0xff & b1). Be aware that the Unix time max value is 7F FF FF FF so a byte number 0xEE is Strange or not standard Unix time.
 
Upvote 0

doncx

Active Member
Licensed User
Longtime User
@MicroDrie - Thanks, but I believe the 0xEE is the lowest byte in Little Endian.

@emexes - Yes, that makes sense thanks. But what's the proper way to read/convert this to a B4A Long datetime?
 
Upvote 0

emexes

Expert
Licensed User
what's the proper way to read/convert this to a B4A Long datetime?

Lol there is no one proper way, but give this a burl:

B4X:
Dim buf() As Byte = Array As Byte(0xEE, 0x63, 0xDF, 0x65)

Dim DT As Long = 0
For I = 3 To 0 Step -1    'start at high byte to simplify shifting
    DT = Bit.ShiftLeftLong(DT, 8) + Bit.And(buf(I), 0xFF)    'convert signed Byte to unsigned
Next
DT = DT * 1000    'convert from time_t seconds to B4A/Java milliseconds (both start at 1/1/1970 epoch)

Log(DT)
Log(DateTime.Date(DT) & " " & DateTime.Time(DT))
Log output:
Waiting for debugger to connect...
Program started.
1709138926000
02/29/2024 03:48:46
 
Last edited:
Upvote 0
Solution

doncx

Active Member
Licensed User
Longtime User
Lol there is no one proper way, but give this a burl:

B4X:
Dim buf() As Byte = Array As Byte(0xEE, 0x63, 0xDF, 0x65)

Dim DT As Long = 0
For I = 3 To 0 Step -1
    DT = Bit.ShiftLeftLong(DT, 8) + Bit.And(buf(I), 0xFF)    'convert signed Bytes to unsigned
Next
DT = DT * 1000    'convert from time_t seconds to B4A/Java milliseconds ("ticks")

Log(DT)
Log(DateTime.Date(DT) & " " & DateTime.Time(DT))

Log output:
Waiting for debugger to connect...
Program started.
1709138926000
02/29/2024 03:48:46
Thank you, thank you. I will study your code fully to understand it, as it's new territory for me. Most appreciated.
 
Upvote 0

emexes

Expert
Licensed User
to understand it

Add these lines to reveal the shifting magic:

B4X:
Dim buf() As Byte = Array As Byte(0xEE, 0x63, 0xDF, 0x65)

Dim DT As Long = 0
For I = 3 To 0 Step -1    'start at high byte to simplify shifting
    DT = Bit.ShiftLeftLong(DT, 8) + Bit.And(buf(I), 0xFF)    'convert signed Byte to unsigned

    Dim DTHex As String = Bit.ToHexStringLong(DT)
    Do While DTHex.Length < 10
        DTHex = "0" & DTHex  
    Loop
   
    Log(I & TAB & DTHex & TAB & DT)
Next
DT = DT * 1000    'convert from time_t seconds to B4A/Java milliseconds (both start at 1/1/1970 epoch)

Log(DT)
Log(DateTime.Date(DT) & " " & DateTime.Time(DT))
Log output:
Waiting for debugger to connect...
Program started.
3    0000000065    101
2    00000065df    26079
1    000065df63    6676323
0    0065df63ee    1709138926
1709138926000
02/29/2024 03:48:46
 
Upvote 0
Top