what is the difference between gmtime() and localtime?

bridged with qdn.public.qnxrtp.applications
Post Reply
Masmito

what is the difference between gmtime() and localtime?

Post by Masmito » Sun Apr 28, 2002 9:22 pm

I thought that the gmtime( ) returns without the timezone consideration and
the localtime should,but the result of the following codes confused me:
-------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

time_t nowtime;
struct tm *gmt,*lct;

main()

{
nowtime=time(NULL);
gmt=gmtime(&nowtime);
lct=localtime(&nowtime);
if((memcmp(gmt,lct,sizeof(struct tm)))==0) {
fprintf (stdout,"the gmtime and the localtime are the same\n");
}
else {
fprintf (stdout,"the gmtime and the localtime have some
difference\n");
}
return 0;

}
-------------------------------
no matter how I changed the timezone setting,the codes tell me "the gmtime
and the localtime are the same".
What is the problem?

Pavol Kycina

Re: what is the difference between gmtime() and localtime?

Post by Pavol Kycina » Mon Apr 29, 2002 6:35 am

"Masmito" <Mssmittomail@hotmail.com> wrote in message
news:aaikpd$bl4$1@inn.qnx.com...
I thought that the gmtime( ) returns without the timezone consideration
and
the localtime should,but the result of the following codes confused me:
-------------------------------
#include <stdio.h
#include <stdlib.h
#include <time.h

time_t nowtime;
struct tm *gmt,*lct;

main()

{
nowtime=time(NULL);
gmt=gmtime(&nowtime);
lct=localtime(&nowtime);
if((memcmp(gmt,lct,sizeof(struct tm)))==0) {
fprintf (stdout,"the gmtime and the localtime are the same\n");
}
else {
fprintf (stdout,"the gmtime and the localtime have some
difference\n");
}
return 0;

}
-------------------------------
no matter how I changed the timezone setting,the codes tell me "the gmtime
and the localtime are the same".
What is the problem?
Those functions return pointer, you have to copy data it is pointing to
somewhere, before isueing tha call again.

e.g
struct tm gmt, lct, *tmp;
tmp = gmtime();
memcpy(&gmt, tmp, sizeof...)
tmp = localtime();
memcpy(&lct, tmp, sizeof...)

memcmp(&gmt, &lct, ...)

Pavol Kycina

Guest

Re: what is the difference between gmtime() and localtime?

Post by Guest » Mon Apr 29, 2002 6:53 am

Masmito <Mssmittomail@hotmail.com> wrote:
I thought that the gmtime( ) returns without the timezone consideration and
the localtime should,but the result of the following codes confused me:
-------------------------------
#include <stdio.h
#include <stdlib.h
#include <time.h

time_t nowtime;
struct tm *gmt,*lct;

main()

{
call tzset() here
nowtime=time(NULL);
gmt=gmtime(&nowtime);
lct=localtime(&nowtime);
if((memcmp(gmt,lct,sizeof(struct tm)))==0) {
fprintf (stdout,"the gmtime and the localtime are the same\n");
}
else {
fprintf (stdout,"the gmtime and the localtime have some
difference\n");
}
return 0;

}
-------------------------------
no matter how I changed the timezone setting,the codes tell me "the gmtime
and the localtime are the same".
What is the problem?

Post Reply

Return to “qdn.public.qnxrtp.applications”