#include <azprolog.h>
#include <time.h>

extern pred P1_local_time();

extern int initiate_mytime(void)
{
	put_bltn("local_time\0",1,P1_local_time);

  return 1;
}

pred P1_local_time(Env)
register Frame *Env;
{
        struct tm *s_time;
        time_t the_time;

	LINK0(Env);
	LINK1(Env,21,7);

	if(!GetFlatListE(Env,Env->Local-1,(unsigned int)7,Env->Global,Env->Global+1,Env->Global+2,Env->Global+3,Env->Global+4,Env->Global+5,Env->Global+6)) YIELD(FAIL);

	(void) time(&the_time);
        s_time=localtime(&the_time);

	if( !UnifyIntE(Env,Env->Global,  (BASEINT)(1900+s_time->tm_year)) ) YIELD(FAIL);
	if( !UnifyIntE(Env,Env->Global+1,(BASEINT)(1+s_time->tm_mon))     ) YIELD(FAIL);
	if( !UnifyIntE(Env,Env->Global+2,(BASEINT)(s_time->tm_wday))      ) YIELD(FAIL);
	if( !UnifyIntE(Env,Env->Global+3,(BASEINT)(s_time->tm_mday))      ) YIELD(FAIL);
	if( !UnifyIntE(Env,Env->Global+4,(BASEINT)(s_time->tm_hour))      ) YIELD(FAIL);
	if( !UnifyIntE(Env,Env->Global+5,(BASEINT)(s_time->tm_min))       ) YIELD(FAIL);
	if( !UnifyIntE(Env,Env->Global+6,(BASEINT)(s_time->tm_sec))       ) YIELD(FAIL);
	FROZEN_GOAL_CALL(Env,YIELD(FAIL));
	YIELD(DET_SUCC);
}

