Re: Convert Java <long> to C <?>
- From: Simple Simon <ssimon@xxxxxxxxxxxxxx>
- Date: Fri, 31 Mar 2006 01:37:20 GMT
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.
- References:
- Convert Java <long> to C <?>
- From: Simple Simon
- Re: Convert Java <long> to C <?>
- From: Me
- Convert Java <long> to C <?>
- Prev by Date: Re: Convert Java <long> to C <?>
- Next by Date: Re: Convert Java <long> to C <?>
- Previous by thread: Re: Convert Java <long> to C <?>
- Next by thread: Re: Convert Java <long> to C <?>
- Index(es):
Relevant Pages
|
|