implement date parsing

git-svn-id: svn+ssh://svn.gnucash.org/repo/gnucash/trunk@420 57a11ea4-9604-0410-9ed3-97b8803252fd
This commit is contained in:
Linas Vepstas 1998-01-13 19:47:33 +00:00
parent b9fa7389a7
commit 18c6477fa9
2 changed files with 78 additions and 22 deletions

View File

@ -9,25 +9,78 @@
/* ================================================ */
static
void xaccParseDate (struct tm *parsed, const char * datestr)
{
char *dupe, *tmp, *day, *month, *year;
int iday, imonth, iyear;
time_t secs;
struct tm *now;
dupe = strdup (datestr);
tmp = dupe;
day = 0x0;
month = 0x0;
year = 0x0;
/* use strtok to find delimiters */
if (tmp) {
day = strtok (tmp, ".,-+/\\()");
if (day) {
month = strtok (NULL, ".,-+/\\()");
if (month) {
year = strtok (NULL, ".,-+/\\()");
}
}
}
/* if any fields appear blank, use today's date */
time (&secs);
now = localtime (&secs);
iday = now->tm_mday;
imonth = now->tm_mon+1;
iyear = now->tm_year+1900;
/* get numeric values */
if (day) iday = atoi (day);
if (month) imonth = atoi (month);
if (year) iyear = atoi (year);
/* check to see if day & month are reversed */
/* only works for some dates */
if (12 < imonth) {
int itmp = imonth;
imonth = iday;
iday = itmp;
}
free (dupe);
if (parsed) {
parsed->tm_mday = iday;
parsed->tm_mon = imonth-1;
parsed->tm_year = iyear-1900;
}
return;
}
/* ================================================ */
static const char *
DateEnter (const char * curr)
{
short day, month, year;
char * datestr;
char * sep;
struct tm celldate;
/* OK, we just entered a newval cell. Find out
* what date that cell thinks it has.
*/
day = 1;
month = 1;
year = 1970;
printf ("curr is %p \n", curr);
printf ("curr val is %s \n", curr);
sscanf (curr, "%d/%d/%d", day, month, year);
xaccParseDate (&celldate, curr);
printf ("enter parsed %d %d %d \n", day, month, year);
printf ("parse %d %d %d \n", celldate.tm_mday, celldate.tm_mon+1,
celldate.tm_year+1900);
return curr;
}
@ -104,11 +157,11 @@ printf ("leave parsed %d %d %d \n", day, month, year);
/* ================================================ */
SingleCell *
DateCell *
xaccMallocDateCell (void)
{
SingleCell *cell;
cell = xaccMallocSingleCell();
DateCell *cell;
cell = (DateCell *) malloc (sizeof (DateCell));
xaccInitDateCell (cell);
return cell;
}
@ -116,21 +169,25 @@ xaccMallocDateCell (void)
/* ================================================ */
void
xaccInitDateCell (SingleCell *cell)
xaccInitDateCell (DateCell *cell)
{
time_t secs;
struct tm *now;
char buff[30];
xaccInitSingleCell (&(cell->cell));
/* default value is today's date */
time (&secs);
now = localtime (&secs);
cell->date = *now;
sprintf (buff, "%d/%d/%d", now->tm_mday, now->tm_mon+1, now->tm_year+1900);
if (cell->value) free (cell->value);
cell ->value = strdup (buff);
if (cell->cell.value) free (cell->cell.value);
cell->cell.value = strdup (buff);
cell ->enter_cell = DateEnter;
cell ->modify_verify = DateMV;
cell->cell.enter_cell = DateEnter;
cell->cell.modify_verify = DateMV;
}
/* --------------- end of file ---------------------- */

View File

@ -2,18 +2,17 @@
#ifndef __XACC_DATE_CELL_C__
#define __XACC_DATE_CELL_C__
#include <time.h>
#include "single.h"
/*
typedef struct _DateCell {
SingleCell cell;
double amount;
struct tm date;
} DateCell;
*/
/* installs a callback to handle date recording */
SingleCell * xaccMallocDateCell (void);
void xaccInitDateCell (SingleCell *);
DateCell * xaccMallocDateCell (void);
void xaccInitDateCell (DateCell *);
#endif /* __XACC_DATE_CELL_C__ */