Time typedef structure

L

Lorn

I was hoping some of you might be able to help me understand the
following code defining a typedef for a time query to the WINAPI. I
understand the basics of what's going on, I just don't understand the
format of the output it should provide. Here is the code below are some
more comments:

typedef struct tagGTime32
{
union
{
struct
{
unsigned char chSec100;
unsigned char chSec;
unsigned char chMin;
unsigned char chHour;
};

unsigned long dwTime;
};
}GTime32;
#pragma pack()

#define MAKEWTIME(h, m, s) ((unsigned long)(\
((unsigned char)(0)) << 0 |\
((unsigned char)(s)) << 8 |\
((unsigned char)(m)) << 16 |\
((unsigned char)(h)) << 24))
#ifdef __cplusplus
extern "C"
{
#endif//__cplusplus
int WINAPI GetGTime32Seconds(const GTime32 *gt);
int WINAPI DiffGTime32Seconds(const GTime32 *gt1, const GTime32 *gt2);
int WINAPI GetGTime32Minutes(const GTime32 *gt);
int WINAPI DiffGTime32Minutes(const GTime32 *gt1, const GTime32 *gt2);
#ifdef __cplusplus
}

When I make a call to a class which uses this def to store the time of
a particular event under a record (*pRcd), I get a value returned that
I'm confused about. I'm defining the trace output as an integer ("%d",
pRcd->gtime) and am returned a 9 digit number. However, if it were time
measured in miliseconds, it should be a max of 8... which leads me to
believe I don't really understand what's going on. If anyone could help
explain what I'm missing, I would be very grateful.

Regards,
Lorn
 
V

Victor Bazarov

Lorn said:
I was hoping some of you might be able to help me understand the
following code defining a typedef for a time query to the WINAPI. I
understand the basics of what's going on, I just don't understand the
format of the output it should provide. Here is the code below are some
more comments:

typedef struct tagGTime32
{
union
{
struct
{
unsigned char chSec100;
unsigned char chSec;
unsigned char chMin;
unsigned char chHour;
};

unsigned long dwTime;
};
}GTime32;
#pragma pack()

#define MAKEWTIME(h, m, s) ((unsigned long)(\
((unsigned char)(0)) << 0 |\
((unsigned char)(s)) << 8 |\
((unsigned char)(m)) << 16 |\
((unsigned char)(h)) << 24))
#ifdef __cplusplus
extern "C"
{
#endif//__cplusplus
int WINAPI GetGTime32Seconds(const GTime32 *gt);
int WINAPI DiffGTime32Seconds(const GTime32 *gt1, const GTime32 *gt2);
int WINAPI GetGTime32Minutes(const GTime32 *gt);
int WINAPI DiffGTime32Minutes(const GTime32 *gt1, const GTime32 *gt2);
#ifdef __cplusplus
}




When I make a call to a class

"make a call to a class"? Is your class a functor?
> which uses this def to store the time of
a particular event under a record (*pRcd), I get a value returned that
I'm confused about. I'm defining the trace output as an integer ("%d",
pRcd->gtime) and am returned a 9 digit number.

A number? From a struct? I don't see any conversion from GTime32 to
an int...
> However, if it were time
measured in miliseconds, it should be a max of 8...

Why 8? What's the limit on 'chHour' member in the time value? If it's
255, then (255*3600*1000 - 1) is the number of milliseconds the struct
can represent, no? But it doesn't represent it in milliseconds. It has
some funky format with _centiseconds_ as the smallest subdivision. So,
what number are you getting and why? To know that we need to see _your_
code as well.
> which leads me to
believe I don't really understand what's going on. If anyone could help
explain what I'm missing, I would be very grateful.

Please post the code that uses that struct and shows how you print that
value (and which value).

V
 
L

Larry I Smith

Lorn said:
I was hoping some of you might be able to help me understand the
following code defining a typedef for a time query to the WINAPI. I
understand the basics of what's going on, I just don't understand the
format of the output it should provide. Here is the code below are some
more comments:

typedef struct tagGTime32
{
union
{
struct
{
unsigned char chSec100;
unsigned char chSec;
unsigned char chMin;
unsigned char chHour;
};

unsigned long dwTime;
};
}GTime32;
#pragma pack()

#define MAKEWTIME(h, m, s) ((unsigned long)(\
((unsigned char)(0)) << 0 |\
((unsigned char)(s)) << 8 |\
((unsigned char)(m)) << 16 |\
((unsigned char)(h)) << 24))
#ifdef __cplusplus
extern "C"
{
#endif//__cplusplus
int WINAPI GetGTime32Seconds(const GTime32 *gt);
int WINAPI DiffGTime32Seconds(const GTime32 *gt1, const GTime32 *gt2);
int WINAPI GetGTime32Minutes(const GTime32 *gt);
int WINAPI DiffGTime32Minutes(const GTime32 *gt1, const GTime32 *gt2);
#ifdef __cplusplus
}


When I make a call to a class which uses this def to store the time of
a particular event under a record (*pRcd), I get a value returned that
I'm confused about. I'm defining the trace output as an integer ("%d",
pRcd->gtime) and am returned a 9 digit number. However, if it were time
measured in miliseconds, it should be a max of 8... which leads me to
believe I don't really understand what's going on. If anyone could help
explain what I'm missing, I would be very grateful.

Regards,
Lorn

GTime32 is a union which overlays 4 distinct time fields onto
an unsigned long. It's data can be accessed as a single
unsigned long (dwTime) or via the individual time fields
(chHour, chMin, chSec, chSec100).

Compile and run this sample C program may shed some light on it
for you:

#include <stdio.h>

/* would normally include the header file defining
* GTime32 here, but this is a cut-and-paste of the
* relevant code.
*/
typedef struct tagGTime32
{
union
{
struct
{
unsigned char chSec100;
unsigned char chSec;
unsigned char chMin;
unsigned char chHour;
};

unsigned long dwTime;
};
}GTime32;


#define MAKEWTIME(h, m, s) ((unsigned long)(\
((unsigned char)(0)) << 0 |\
((unsigned char)(s)) << 8 |\
((unsigned char)(m)) << 16 |\
((unsigned char)(h)) << 24))

/* end of the GTime32 cut-and-paste */

int main()
{
GTime32 gtime;

/* create a time of 24:45:30.00 in gtime */
gtime.dwTime = MAKEWTIME(24, 45, 30);

printf("setting gtime.dwTime = MAKEWTIME(24, 45, 30)\n");

printf("gtime.dwTime (time as a long) = %ld (0x%lx)\n",
gtime.dwTime, gtime.dwTime);

printf("gtime.chHour = %02d (0x%02x)\n",
gtime.chHour, gtime.chHour);

printf("gtime.chMin = %02d (0x%02x)\n",
gtime.chMin, gtime.chMin);

printf("gtime.chSec = %02d (0x%02x)\n",
gtime.chSec, gtime.chSec);

printf("gtime.chSec100 = %02d (0x%02x)\n",
gtime.chSec100, gtime.chSec100);

printf("gtime as a time = %02d:%02d:%02d.%02d\n",
gtime.chHour, gtime.chMin, gtime.chSec, gtime.chSec100);

return 0;
}

Regards,
Larry
 
L

Larry I Smith

Larry said:
GTime32 is a union which overlays 4 distinct time fields onto
an unsigned long. It's data can be accessed as a single
unsigned long (dwTime) or via the individual time fields
(chHour, chMin, chSec, chSec100).

Compile and run this sample C program may shed some light on it
for you:

#include <stdio.h>

/* would normally include the header file defining
* GTime32 here, but this is a cut-and-paste of the
* relevant code.
*/
typedef struct tagGTime32
{
union
{
struct
{
unsigned char chSec100;
unsigned char chSec;
unsigned char chMin;
unsigned char chHour;
};

unsigned long dwTime;
};
}GTime32;


#define MAKEWTIME(h, m, s) ((unsigned long)(\
((unsigned char)(0)) << 0 |\
((unsigned char)(s)) << 8 |\
((unsigned char)(m)) << 16 |\
((unsigned char)(h)) << 24))

/* end of the GTime32 cut-and-paste */

int main()
{
GTime32 gtime;

/* create a time of 24:45:30.00 in gtime */
gtime.dwTime = MAKEWTIME(24, 45, 30);

printf("setting gtime.dwTime = MAKEWTIME(24, 45, 30)\n");

printf("gtime.dwTime (time as a long) = %ld (0x%lx)\n",
gtime.dwTime, gtime.dwTime);

printf("gtime.chHour = %02d (0x%02x)\n",
gtime.chHour, gtime.chHour);

printf("gtime.chMin = %02d (0x%02x)\n",
gtime.chMin, gtime.chMin);

printf("gtime.chSec = %02d (0x%02x)\n",
gtime.chSec, gtime.chSec);

printf("gtime.chSec100 = %02d (0x%02x)\n",
gtime.chSec100, gtime.chSec100);

Oops, the format string in the following printf() is incorrect.
The ending ".%02d\n" should be ".%2d\n" or even just ".%d\n".
The way I had it originally will cause 2.3 seconds to be
displayed as "02.03" instead of "02.3".

printf("gtime as a time = %02d:%02d:%02d.%02d\n",
gtime.chHour, gtime.chMin, gtime.chSec, gtime.chSec100);

return 0;
}

Regards,
Larry

Of course, you can also set the 4 time fields directly:

gtime.chHour = 10;
gtime.chMin = 15;
etc.

If you will be setting all of the individual fields
directly for an new uninitialized gtime, then you should
do 'gtime.dwTime = 0' first to ensure that any bits in the
unsigned long that are NOT used by the 4 time fields are
set to zero.

Regards,
Larry
 
L

Larry I Smith

Larry said:
Larry I Smith wrote:
[snip]


Oops, the format string in the following printf() is incorrect.
The ending ".%02d\n" should be ".%2d\n" or even just ".%d\n".
The way I had it originally will cause 2.3 seconds to be
displayed as "02.03" instead of "02.3".

printf("gtime as a time = %02d:%02d:%02d.%02d\n",
gtime.chHour, gtime.chMin, gtime.chSec, gtime.chSec100);

return 0;
}
[snip]

Grrr, I shouldn't be allowed to code before I get my
first cup of coffee...

To correct my above correction:

The ending ".%02d\n" should be ".%-2d\n" or even just ".%d\n".
Depending on whether or not you desire a constant fixed length
output from printf/sprintf for any random time value.

Larry
 
L

Lorn

Wow, thank you very much Victor and Larry. Victor for getting me to go
back to the code (this wasn't my code by the way, someone else's I was
trying to figure out) and study it harder so I could give a proper
reply. Larry for showing the exact example of what I needed. I now have
it printing the correct time. Woohoo, it was a long time coming.

Thanks again, this is such a great group.

Lorn
 
L

Larry I Smith

Lorn said:
Wow, thank you very much Victor and Larry. Victor for getting me to go
back to the code (this wasn't my code by the way, someone else's I was
trying to figure out) and study it harder so I could give a proper
reply. Larry for showing the exact example of what I needed. I now have
it printing the correct time. Woohoo, it was a long time coming.

Thanks again, this is such a great group.

Lorn

Be sure to read my last (3rd) post that includes the correction
to my first correction...

Larry
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,591
Members
45,101
Latest member
MarcusSkea
Top