problem with time.h

T

tguclu

Hi everyone ,

Actually I have posted this item in gcc.help but no one replied it .
Then I decided to post it to here because it seems to me that my
question is related to C programming itself .

Question:

I'm trying to pass a pointer to (struct osa_localtime) and get the
local time info to these structure but it doesn't work. After func.
finishes its job and returns to main , my variable in main doesn't
get modified as i expected.

I don't think it's a variable scope issue or is it ? I guess it sth.
specific to structure belongs to "time.h"


gcc version :3.3.1 (cygming special)
host os : winxp pro sp2


Below is the source code


#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <locale.h>
#include <string.h>
#include <sys/time.h>
#include <sys/times.h>


typedef unsigned short WORD;


//typedef struct tm SYSTEMTIME ;


typedef struct _SYSTEMTIME {
WORD wYear;
WORD wMonth;
WORD wDayOfWeek;
WORD wDay;
WORD wHour;
WORD wMinute;
WORD wSecond;
WORD wMilliseconds;



} SYSTEMTIME, *PSYSTEMTIME, *LPSYSTEMTIME;


struct osa_localtime
{
SYSTEMTIME *st;
time_t curtime;
struct tm* localtm;
struct timeval tv;
struct timespec *tp;


};


typedef struct osa_localtime OSA_LOCAL_TIME;

void OSAGetLocaleTime( SYSTEMTIME *local_time);


void OSAGetLocaleTime( SYSTEMTIME *local_time)
{
#ifdef _WIN32_NT_
GetLocalTime(local_time);
#else
OSA_LOCAL_TIME *loc = (OSA_LOCAL_TIME
*)malloc(sizeof(OSA_LOCAL_TIME));
WORD miliseconds;
setlocale (LC_ALL, "");
loc->tp = (struct timespec
*)malloc(sizeof(struct timespec));
clock_gettime(CLOCK_REALTIME,loc->tp);
miliseconds = (WORD)((loc->tp->tv_nsec /
1000 )/1000);
loc-
(SYSTEMTIME *)malloc(sizeof(SYSTEMTIME)) ;
loc->curtime =
time (NULL);
loc->localtm = localtime (&loc-
curtime) ;
loc->localtm->tm_year += 1900 ; //tm_year
starts from 1900
loc->localtm->tm_mon++ ; //tm_mon starts Jan
as 0 index but wMonth
gets Jan as 1
memcpy(&(loc->st->wYear),&(loc->localtm-
tm_year),sizeof(WORD));
memcpy(&(loc->st->wMonth),&(loc->localtm-
tm_mon),sizeof(WORD));
memcpy(&(loc->st->wDayOfWeek),&(loc->localtm-

tm_wday),sizeof(WORD));


memcpy(&(loc->st->wDay),&(loc->localtm-
tm_mday),sizeof(WORD));
memcpy(&(loc->st->wHour),&(loc->localtm-
tm_hour),sizeof(WORD));
memcpy(&(loc->st->wMinute),&(loc->localtm-
tm_min),sizeof(WORD));
memcpy(&(loc->st->wSecond),&(loc->localtm-
tm_sec),sizeof(WORD));
memcpy(&(loc->st-
wMilliseconds),&miliseconds,sizeof(WORD));
local_time = loc->st;
// *local_time = *(loc->st); //this one works
but i don't like this
way.

#endif



}


int test_osa_local_time (void)
{

int x = 88;
SYSTEMTIME st;
OSAGetLocaleTime(&st);
printf("\n!!!!!!!!!!!!!!!!!!!!!!OSA TIME
TESTYear!!!!!!!!!!!!!!!!!!!!!!");
printf("\nOSA Year : %d", st.wYear);
printf("\nOSA Day : %d", st.wDay);
printf("\nOSA Day of Week : %d",
st.wDayOfWeek);
printf("\nOSA Hour : %d", st.wHour);
printf("\nOSA Minute : %d", st.wMinute);
printf("\nOSA Seconds : %d", st.wSecond);
printf("\nOSA MiliSeconds : %d",
st.wMilliseconds);
printf("\nOSA Month : %d", st.wMonth);


return 0;



}


void OSAGetSystemTime( OSA_LOCAL_TIME *loc)
{


}


int main (void)
{

test_osa_local_time();
return 0;
 

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

No members online now.

Forum statistics

Threads
473,955
Messages
2,570,117
Members
46,705
Latest member
v_darius

Latest Threads

Top