mirror of
https://github.com/Gnucash/gnucash.git
synced 2025-02-25 18:55:30 -06:00
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:
parent
b9fa7389a7
commit
18c6477fa9
@ -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 *
|
static const char *
|
||||||
DateEnter (const char * curr)
|
DateEnter (const char * curr)
|
||||||
{
|
{
|
||||||
short day, month, year;
|
|
||||||
char * datestr;
|
|
||||||
char * sep;
|
char * sep;
|
||||||
|
struct tm celldate;
|
||||||
|
|
||||||
/* OK, we just entered a newval cell. Find out
|
/* OK, we just entered a newval cell. Find out
|
||||||
* what date that cell thinks it has.
|
* what date that cell thinks it has.
|
||||||
*/
|
*/
|
||||||
day = 1;
|
|
||||||
month = 1;
|
|
||||||
year = 1970;
|
|
||||||
|
|
||||||
printf ("curr is %p \n", curr);
|
xaccParseDate (&celldate, curr);
|
||||||
printf ("curr val is %s \n", curr);
|
|
||||||
sscanf (curr, "%d/%d/%d", day, month, year);
|
|
||||||
|
|
||||||
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;
|
return curr;
|
||||||
}
|
}
|
||||||
@ -104,11 +157,11 @@ printf ("leave parsed %d %d %d \n", day, month, year);
|
|||||||
|
|
||||||
/* ================================================ */
|
/* ================================================ */
|
||||||
|
|
||||||
SingleCell *
|
DateCell *
|
||||||
xaccMallocDateCell (void)
|
xaccMallocDateCell (void)
|
||||||
{
|
{
|
||||||
SingleCell *cell;
|
DateCell *cell;
|
||||||
cell = xaccMallocSingleCell();
|
cell = (DateCell *) malloc (sizeof (DateCell));
|
||||||
xaccInitDateCell (cell);
|
xaccInitDateCell (cell);
|
||||||
return cell;
|
return cell;
|
||||||
}
|
}
|
||||||
@ -116,21 +169,25 @@ xaccMallocDateCell (void)
|
|||||||
/* ================================================ */
|
/* ================================================ */
|
||||||
|
|
||||||
void
|
void
|
||||||
xaccInitDateCell (SingleCell *cell)
|
xaccInitDateCell (DateCell *cell)
|
||||||
{
|
{
|
||||||
time_t secs;
|
time_t secs;
|
||||||
struct tm *now;
|
struct tm *now;
|
||||||
char buff[30];
|
char buff[30];
|
||||||
|
|
||||||
|
xaccInitSingleCell (&(cell->cell));
|
||||||
|
|
||||||
|
/* default value is today's date */
|
||||||
time (&secs);
|
time (&secs);
|
||||||
now = localtime (&secs);
|
now = localtime (&secs);
|
||||||
|
cell->date = *now;
|
||||||
sprintf (buff, "%d/%d/%d", now->tm_mday, now->tm_mon+1, now->tm_year+1900);
|
sprintf (buff, "%d/%d/%d", now->tm_mday, now->tm_mon+1, now->tm_year+1900);
|
||||||
|
|
||||||
if (cell->value) free (cell->value);
|
if (cell->cell.value) free (cell->cell.value);
|
||||||
cell ->value = strdup (buff);
|
cell->cell.value = strdup (buff);
|
||||||
|
|
||||||
cell ->enter_cell = DateEnter;
|
cell->cell.enter_cell = DateEnter;
|
||||||
cell ->modify_verify = DateMV;
|
cell->cell.modify_verify = DateMV;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* --------------- end of file ---------------------- */
|
/* --------------- end of file ---------------------- */
|
||||||
|
@ -2,18 +2,17 @@
|
|||||||
#ifndef __XACC_DATE_CELL_C__
|
#ifndef __XACC_DATE_CELL_C__
|
||||||
#define __XACC_DATE_CELL_C__
|
#define __XACC_DATE_CELL_C__
|
||||||
|
|
||||||
|
#include <time.h>
|
||||||
#include "single.h"
|
#include "single.h"
|
||||||
|
|
||||||
/*
|
|
||||||
typedef struct _DateCell {
|
typedef struct _DateCell {
|
||||||
SingleCell cell;
|
SingleCell cell;
|
||||||
double amount;
|
struct tm date;
|
||||||
} DateCell;
|
} DateCell;
|
||||||
*/
|
|
||||||
|
|
||||||
/* installs a callback to handle date recording */
|
/* installs a callback to handle date recording */
|
||||||
SingleCell * xaccMallocDateCell (void);
|
DateCell * xaccMallocDateCell (void);
|
||||||
void xaccInitDateCell (SingleCell *);
|
void xaccInitDateCell (DateCell *);
|
||||||
|
|
||||||
#endif /* __XACC_DATE_CELL_C__ */
|
#endif /* __XACC_DATE_CELL_C__ */
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user