/*     Bed a menu-driven multi dataformat binary editor for Linux            */
/*     Copyright (C) 1998 Jaap Korthals Altes <jkaltes@hetnet.nl>       */
/*                                                                           */
/*     This program is free software; you can redistribute it and/or modify  */
/*     it under the terms of the GNU General Public License as published by  */
/*     the Free Software Foundation; either version 2 of the License, or     */
/*     (at your option) any later version.                                   */
/*                                                                           */
/*     This program is distributed in the hope that it will be useful,       */
/*     but WITHOUT ANY WARRANTY; without even the implied warranty of        */
/*     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         */
/*     GNU General Public License for more details.                          */
/*                                                                           */
/*     You should have received a copy of the GNU General Public License     */
/*     along with this program; if not, write to the Free Software           */
/*     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.             */
/* Fri Dec  8 22:14:27 2000                                                  */

#ifdef GLOBAL
#include <stdio.h>
#include "../screenpart.h"
#include <time.h>
#include <limits.h>
#include <time.h>
#include <string.h>
#include <ctype.h>
static char timeformat[]="%a %b %e %H:%M:%S %C%y";
#ifndef HAS_GMTIME_R
#define	gmtime_r(t, tmbuf) memcpy(tmbuf,gmtime(t),sizeof(struct tm))
#endif
class  Time_t : public ScreenPart {	
	public:	 Time_t ( Editor *ed,int x1=0, int y1=0): ScreenPart( ed, 24 ,10, sizeof(time_t) ,x1, y1,1)	{
	struct tm tmbuf;
	time_t t=947585553;
#ifndef OWNTRANSTIME
	gmtime_r(&t, &tmbuf);
	char formal[50]="";
 	strftime (formal,50,timeformat,  &tmbuf);
	maxres=strlen(formal);
#endif
	};	

		declareparent 
//"Wed Oct 12 11:29:38 1988"
	int formal2bin(char *formal, unsigned char *bin, int len) {	
	time_t t;
	struct tm tim;	
#ifdef OWNTRANSTIME
	char months[][4]= { "Jan","Feb","Mar","Apr","May", "Jun", "Jul", "Aug","Sep","Oct","Nov" ,"Dec" };
	char *ptr=formal+4;
	int timtm_year, timtm_hour;
	for(int j=0;j<12;j++) {
		if(!strncasecmp(months[j],	ptr,3)) {
				tim.tm_mon=j;
				tim.tm_isdst=0;
				tim.tm_wday=0;
				ptr+=4;
sscanf(ptr,"%d %d:%d:%d %d", &tim.tm_mday,&timtm_hour,&tim.tm_min,&tim.tm_sec,&timtm_year);
/*				if(timtm_hour>24||timtm_hour<0||tim.tm_min<0||tim.tm_min>60||tim.tm_mday<0||tim.tm_mday>32||timtm_year<1901||timtm_year>2037)
					return -1;
*/
				timtm_year-=1900;
				tim.tm_year=timtm_year;
				tim.tm_hour=timtm_hour;

				t=mktime(&tim);
	//			t+=tim.tm_gmtoff;
//	t-=timezone;
				memmove(bin,(char *)&t,4);
				return 0;
				}

		}
	return -1;	
#else
	memset(&tim,'\0',sizeof(tim));
	strptime (formal, timeformat, &tim);
	t=mktime(&tim);
#ifdef TIMEZONENEEDED /* TO get GMT */
	t-=timezone;
#else
	t+=(tim.tm_gmtoff-(tim.tm_isdst>0?(tim.tm_isdst*60*60):0));
#endif

	memmove(bin,(char *)&t,4);
	return 0; 
#endif
	}

#ifndef OWNTRANSTIME
	int bin2formal(unsigned char *bin, char *formal, int len) {	
	  struct tm tmbuf;
	  gmtime_r((time_t *)bin, &tmbuf);
 	  strftime (formal,maxres+1,timeformat,  &tmbuf);
	return 0;
	}
#else

	int bin2formal(unsigned char *bin, char *formal, int len) {	
		char *tim=ctime((time_t*)bin);
		char *end=index(tim,'\n');
		if(*end) {
			short l=end-tim;
			memmove(formal,tim,l);
			formal[l]=' ';
			}
		else
			strcpy(formal,tim);
		return 0;	
		}
#endif
	int isel(int ch) {
			return (isalnum(ch)||ch==':'||ch==' ');
			}
	ScreenPart *newone(void) {return new Time_t(*this);}
	};

parentd(ScreenPart,Time_t) 
#include "../typelist.h"
datatype(Time_t)
#endif



