Re: Convert Java <long> to C <?>



Simple Simon wrote:
Java longs are 8 bytes. I have a Java long that is coming in from the network, and that represents milliseconds since Epoch (Jan 1 1970 00:00:00). I'm having trouble understanding how to get it into a struct timeval object.

I get the ByteBuffer as an array of const unsigned char, 'buffer'.

Here's an example: 00 00 01 0A 29 1D 07 E4

This value maps somehow to Thu Mar 23 16:57:49 2006 and some millisecs which I haven't sussed out yet.

I'm beating my simple brains out trying to figure out how to get that into a struct timeval t so that t.tv_sec would spit out

You no doubt mean a struct tm t, with t.tm_sec, since there ain't no such thing as a struct timeval in standard C (until you define it).
You really don't want to assign your value (divided by 1000) to t.tm_sec, which need not be able to hold it and for which such value may be meaningless.


Thu Mar 23 16:57:49 2006

if used as an argument to ctime().

You might try an experiment like the following.
You can see that I am 5 hours off. That suggests that I have not resolved a time zone problem. Your value is very likely a UTC time for which gmtime rather than localtime is represented. There are several unresolved issues in this code which I will leave as an exercise.

#include <stdio.h>
#include <time.h>
#include <string.h>

int main(void)
{
struct tm now = {
.tm_year = 106,
.tm_mon = 2,
.tm_mday = 23,
.tm_hour = 16,
.tm_min = 57,
.tm_sec = 49,
.tm_isdst = -1
};
time_t t;
unsigned char buf[sizeof t];
size_t ndx;
unsigned long long tx;
unsigned char bufx[sizeof tx];
t = mktime(&now);
memcpy(buf, &t, sizeof t);
printf("asctime(&now) = %s", asctime(&now));
printf("ctime(&t) = %s", ctime(&t));
for (ndx = 0; ndx < sizeof t; ndx++)
printf("buf[%lu] = %#04hx\n", (unsigned long) ndx, buf[ndx]);
putchar('\n');
tx = 1000ull * t;
memcpy(bufx, &tx, sizeof tx);
for (ndx = 0; ndx < sizeof tx; ndx++)
printf("buf[%lu] = %#04hx\n", (unsigned long) ndx, bufx[ndx]);

printf("\n\nHaving run this experiment before, I will place\n"
"the OP's values into the array,\n"
"move that to the unsigned long long,\n"
"divide by 1000, assign that value to a time_t,\n"
"and see what happens.\n");
memcpy(bufx, &(unsigned char[sizeof bufx]) {
0xe4, 0x07, 0x1d, 0x29, 0x0a, 0x01, 0, 0}
, sizeof bufx);
printf("The buffer as I have assigned it:\n");
for (ndx = 0; ndx < sizeof tx; ndx++)
printf("buf[%lu] = %#04hx\n", (unsigned long) ndx, bufx[ndx]);
memcpy(&tx, bufx, sizeof tx);
printf("corresponding to %llu (%#018llx)\n", tx, tx);
t = tx / 1000;
printf("The time_t now holds %lu\n", (unsigned long) t);
printf("ctime(&t) = %s", ctime(&t));


return 0;
}

asctime(&now) = Thu Mar 23 16:57:49 2006
ctime(&t) = Thu Mar 23 16:57:49 2006
buf[0] = 0x8d
buf[1] = 0xd3
buf[2] = 0x22
buf[3] = 0x44

buf[0] = 0xc8
buf[1] = 0x5e
buf[2] = 0x0a
buf[3] = 0x28
buf[4] = 0x0a
buf[5] = 0x01
buf[6] = 0000
buf[7] = 0000


Having run this experiment before, I will place
the OP's values into the array,
move that to the unsigned long long,
divide by 1000, assign that value to a time_t,
and see what happens.
The buffer as I have assigned it:
buf[0] = 0xe4
buf[1] = 0x07
buf[2] = 0x1d
buf[3] = 0x29
buf[4] = 0x0a
buf[5] = 0x01
buf[6] = 0000
buf[7] = 0000
corresponding to 1143151069156 (0x0000010a291d07e4)
The time_t now holds 1143151069
ctime(&t) = Thu Mar 23 21:57:49 2006

.



Relevant Pages