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



anti_spam_email2003@xxxxxxxxx wrote...
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

Thu Mar 23 16:57:49 2006

if used as an argument to ctime().

Wha? timeval isn't in standard C and ctime takes a time_t *.

OK: "...if stuffed into a time_t var whose address is used as an
argument to ctime()."

All C says
is that time_t is an arithmetic type, it doesn't say the start time or
what unit of measurement it uses. Many C implementations use the number
of seconds since UNIX's epoch so you can get by with:

unsigned long long val = 0;

Didn't know about 'long long' though. Thanks very much.

for (size_t i = 0; i < 8; ++i)
val = val<<8 | buffer[i];
time_t time = val/1000;
const char *tstr = ctime(&time);

But odds are, if you have access to sockets on your implementation, the
above should work.
.



Relevant Pages

  • Re: Convert Java to C
    ... and that represents milliseconds since Epoch (Jan 1 1970 ... I get the ByteBuffer as an array of const unsigned char, ... Many C implementations use the number ...
    (comp.lang.c)
  • Re: Convert Java to C
    ... and that represents milliseconds since Epoch (Jan 1 1970 ... You're risking an overflow there, and certainly incorrect results if the ... Sign-extend, convert to signed long long, THEN divide ...
    (comp.lang.c)
  • Re: Convert Java to C
    ... and that represents milliseconds since Epoch (Jan 1 1970 ... You're risking an overflow there, and certainly incorrect results if the ... Sign-extend, convert to signed long long, THEN divide ...
    (comp.lang.c)
  • Re: Epoch Question
    ... I am assuming that the first column is the epoch time stamp ... Forget about the milliseconds. ... "Reply" at the bottom of the article headers. ...
    (comp.lang.perl.misc)