/*
** pat_time.c for  in 
** 
** Made by 
** Login   <vianney@epita.fr>
** 
** Started on  Wed Sep  1 06:40:50 1999 
** Last update Thu Oct 28 20:19:27 1999 
*/
#include <time.h>
#include "pat.h"
#include "pat_time.h"
#include "pat_32.h"

char			*ctime_without_nl(t)
time_t			*t;
{
  char			*result;

  result = ctime(t);
  result[strlen(result) - 1] = 0;
  return (result);
}

char			*time_itmpl = "\n\
<table _name=time[%i%] bgcolor=\"%%timeColor%%\" width=100%%%%>\n\
<tr>\n\
<td align=center width=100%%%%>\n\
<small>\n\
<a href=\"set(time[%i%].Time)\">%%time[%i%].Time%%</a>(<a href=\"set(time[%i%].time)\">%%time[%i%].time%%</a>)\n\
</small>\n\
</td>\n\
</tr>\n\
</table>\n\
<br>\n\
";

t_field			time_fields[] = 
{
  {"time",    0,      &u32_pat,                  NULL},
  {"Time",    0,      &time_pat,                 NULL},
  NULL_FIELD
};

int	time_chan;

PAT_NAME_GENERIC(time_pat_name,
		 (VOID_PTR)&time_chan,
		 "time")

PAT_GET_FIELD_GENERIC(time_pat_get_field,
		      time_fields)

PAT_SET_FIELD_GENERIC(time_pat_set_field,
		      time_fields)

PAT_GET_FIELDS_GENERIC(time_pat_get_fields,
		       time_fields)

PAT_GET_TMPL_GENERIC(time_pat_get_tmpl,
		     (VOID_PTR)&time_chan,
		     time_itmpl)

PAT_GET_FIELD_PAT_GENERIC(time_pat_get_field_pat,
			  time_fields)

PAT_OFF_GENERIC(time_pat_off,
		4)

PAT_SUB_GENERIC(time_pat_sub,
		&time_pat)

PAT_EXTRACT_DECL(time_pat_extract)
{
  t_u32		u32;
  t_status	status;
  time_t	t;
	
  if (len < sizeof (t_u32))
    return (ERR_PAT_TRUNC_FIELD);
  FBCOPY(buf,&u32,sizeof (t_u32));
#ifdef NOTDEF
  u32 = UNSAFE_NTOHL(u32);
#endif
  t = u32;
  return (str_cat_str(str,
		      max_len,
		      ctime_without_nl(&t)));
}

PAT_INSERT_DECL(time_pat_insert)
{
  t_u32		u32;
  t_status	status;	
  time_t	t;

  if (len < sizeof (u32))
    return (ERR_PAT_TRUNC_FIELD);
  /*
  if ((t = get_date(value,
		    NULL)) < 0)
		    return (ERR_PAT_MALFORMED);*/
  return (ERR_PAT_NI);
  u32 = t;
#ifdef NOTDEF
  u32 = UNSAFE_HTONL(u32);
#endif
  FBCOPY(&u32,buf,sizeof (u32));
  return (0);
}

t_pat				time_pat = 
{
  time_pat_name,		/* t_pat_name_proc		*/
  time_pat_off,			/* t_pat_off_proc		*/
  time_pat_sub,			/* t_pat_sub_proc		*/
  NULL,				/* t_pat_sum_proc		*/
  time_pat_get_field,		/* t_pat_get_field_proc		*/
  time_pat_set_field,		/* t_pat_set_field_proc		*/
  time_pat_get_fields,		/* t_pat_get_fields_proc	*/
  time_pat_get_tmpl,		/* t_pat_get_tmpl_proc		*/
  NULL,				/* t_pat_get_tmpl2_proc		*/
  NULL,				/* t_pat_has_opt_proc		*/
  NULL,				/* t_pat_adapt_len_proc		*/
  time_pat_get_field_pat,	/* t_pat_get_field_pat_proc	*/
  time_pat_extract,		/* t_pat_extract_proc		*/
  time_pat_insert,		/* t_pat_insert_proc		*/
  NULL,				/* t_pat_get_choices_proc	*/
};
