views:

134

answers:

2

Hi, I'm trying to insert a timestamp (hour:min:sec) into a two-byte array and i'm a little confused on how to accomplish this...any help is greatly appreciated!

int Hour = CTime::GetCurrentTime().GetHour(); 
int Minute = CTime::GetCurrentTime().GetMinute(); 
int Second = CTime::GetCurrentTime().GetSecond(); 

BYTE arry[2];

//Need to insert 'Hour', 'Minute', & 'Second' into 'arry'

Thanks!

+4  A: 

You can't. There are potentially 86402 seconds in a day (a day can have up to two leap seconds), but the 16 bits available to you in a byte[2] array can only represent 65536 separate values.

jemfinch
A day cannot have more than one leap second (no matter what the documentation for `gmtime()` and friends states -- the POSIX committee screwed up bigtime on this one).
Jim Lewis
How could I accomplish this if I could increase the size of my byte array?
JB_SO
if you want to use an 8 character array you use: sprintf(arry, "%d:%d:%d", Hour, Minute, Second); But that is old style C code. It should work for you if you have 9 bytes (8 plus one for null)
Tim
If you had three bytes, you could just store it with hours at index 0, minutes at index 1, and seconds at index 2.
jemfinch
So, I could do something like this?BYTE arry[9];sprintf(arry, "%d:%d:%d", Hour, Minute, Second);
JB_SO
+1  A: 
  1. hour:min:sec is not what people call timestamp. A timestamp is the number of seconds elapsed since 1970-01-01 and will surely not fit into 16 bits.
  2. Assuming ranges of hours=[0;24], minutes=[0;60], seconds=[0;60] (leap seconds included) you will need 5+6+6=17 bits which still won't fit into 16 bits.

If you had a 32-bit array, it would fit:

int Hour = CTime::GetCurrentTime().GetHour(); 
int Minute = CTime::GetCurrentTime().GetMinute(); 
int Second = CTime::GetCurrentTime().GetSecond(); 

uint8_t array[4];

// Just an example
*(uint32_t*)array = (Hour << 12) | (Minute << 6) | Second;

This sounds somewhat like homework for me... or what is the exact purpose of doing this?

AndiDog
I need to send out "heartbeat" packet every 30ms in our simulation and as part of this packet, I need to include the "timestamp", that's the purpose of this
JB_SO
Here is the info. from the ICDByte 1 - 2 Bits 15:0 Time StampTime Stamp = Time of message (hearbeat message) creation.
JB_SO