some time ago I wanted to store a date/time stamp (yymmdd + hhmmss) with in a 4 byte (32 bit) variable; here the small code snippet I came up with:
//
// CC_datetime.cpp
//
// *jm*20030902
typedef struct
{
unsigned nSecond : 6; // 0..59 (6 bits)
unsigned nMinute : 6; // 0..59 (6 bits)
unsigned nHour : 5; // 0..23 (5 bits)
unsigned nDay : 5; // 1..31 (5 bits)
unsigned nMonth : 4; // 1..12 (4 bits)
unsigned nYear : 6; // 0..63 (6 bits)
} sDateTime; // = 32 bits
typedef union
{
sDateTime ccDTb;
CHAR ccDTc [4];
} uDateTime;
//
// campaignDT
//
uDateTime ccDT;
char campaignDTStr [16];
SYSTEMTIME SystemTime;
GetLocalTime ( &SystemTime );
ccDT.ccDTb.nDay = SystemTime.wDay;
ccDT.ccDTb.nMonth = SystemTime.wMonth;
ccDT.ccDTb.nYear = SystemTime.wYear % 100;
ccDT.ccDTb.nHour = SystemTime.wHour;
ccDT.ccDTb.nMinute = SystemTime.wMinute;
ccDT.ccDTb.nSecond = SystemTime.wSecond;
sprintf ( campaignDTStr, „%02d%02d%02d%02d%02d%02d“,
/* */ ccDT.ccDTb.nYear, ccDT.ccDTb.nMonth, ccDT.ccDTb.nDay,
/* */ ccDT.ccDTb.nHour, ccDT.ccDTb.nMinute, ccDT.ccDTb.nSecond );
…