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



Apologies for top-posting, but...

Thank you Jordan!

random832@xxxxxxxxx wrote...
On 2006-03-31, Me <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 *. 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;
for (size_t i = 0; i < 8; ++i)
val = val<<8 | buffer[i];
time_t time = val/1000;

You're risking an overflow there, and certainly incorrect results if the
value is negative. Sign-extend, convert to signed long long, THEN divide
by 1000 to put in the time_t. Still can overflow, but then it'll only do
it on values that deserve it.

Suppose i have the unsigned long long value 0xFFFFFFFFFFFF0000, that is,
what is meant to be the signed value -65536. that divided by 1000 is
0x4189374BC6A7AE, which is a huge positive value. Converting it to
a 32-bit signed value will result in 0x4BC6A7AE, which is the time Thu
Apr 15 01:44:14 2010, instead of the time Wed Dec 31 18:58:55 1969 [both
times converted using Eastern time.]

And portable code to convert seconds-since-1970 to a time_t is certainly
_possible_. You can probably even get mktime to do most of the work, but
you'll have to know [or cleverly determine] what timezone you're working
in.


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 ... 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 ... 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 ... I get the ByteBuffer as an array of const unsigned char, ... Many C implementations use the number ...
    (comp.lang.c)
  • Re: Simple date compare
    ... get the timestamp long, and divide by the number of milliseconds in ... Calendar lets one zero out the time fields, leaving only the date fields filled. ... That's going to be a lot less code and a lot more direct than messing with milliseconds and timezone calculations. ...
    (comp.lang.java.programmer)
  • 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)