From 213236b472cd0981e91b4ddfe05ae3dd20abff57 Mon Sep 17 00:00:00 2001 From: Linas Vepstas Date: Wed, 8 Apr 1998 06:17:11 +0000 Subject: [PATCH] first round of changes to handle new date stuff git-svn-id: svn+ssh://svn.gnucash.org/repo/gnucash/trunk@793 57a11ea4-9604-0410-9ed3-97b8803252fd --- src/engine/FileIO.c | 59 +++++++++++++----------- src/engine/QIFIO.c | 1 + src/engine/Transaction.c | 95 ++++++++++++++++++++++++++------------- src/engine/Transaction.h | 8 ++-- src/engine/TransactionP.h | 20 ++++++--- src/engine/date.c | 66 +++++++++++++++++++++++++++ src/engine/date.h | 7 +++ 7 files changed, 187 insertions(+), 69 deletions(-) diff --git a/src/engine/FileIO.c b/src/engine/FileIO.c index f7443160ce..e43fc46da9 100644 --- a/src/engine/FileIO.c +++ b/src/engine/FileIO.c @@ -86,10 +86,12 @@ #include "Account.h" #include "AccountP.h" +#include "date.h" #include "FileIO.h" #include "Group.h" #include "GroupP.h" #include "messages.h" +#include "Transaction.h" #include "TransactionP.h" #include "util.h" @@ -116,14 +118,14 @@ static Account *readAccount( int fd, AccountGroup *, int token ); static Transaction *readTransaction( int fd, Account *, int token ); static Split *readSplit( int fd, int token ); static char *readString( int fd, int token ); -static Date *readDate( int fd, int token ); +static time_t readDate( int fd, int token ); static int writeGroup( int fd, AccountGroup *grp ); static int writeAccount( int fd, Account *account ); static int writeTransaction( int fd, Transaction *trans ); static int writeSplit( int fd, Split *split); static int writeString( int fd, char *str ); -static int writeDate( int fd, Date *date ); +static int writeDate( int fd, time_t secs ); /*******************************************************/ @@ -491,7 +493,6 @@ readTransaction( int fd, Account *acc, int token ) int err=0; int acc_id; int i; - Date *date; int dummy_category; int numSplits; Transaction *trans = 0x0; @@ -499,6 +500,7 @@ readTransaction( int fd, Account *acc, int token ) char recn; double num_shares = 0.0; double share_price = 0.0; + time_t secs; /* create a transaction structure */ trans = xaccMallocTransaction(); @@ -513,15 +515,14 @@ readTransaction( int fd, Account *acc, int token ) return NULL; } - date = readDate( fd, token ); - if( date == NULL ) + secs = readDate( fd, token ); + if( 0 == secs ) { PERR ("Premature end of Transaction at date"); xaccTransDestroy(trans); return NULL; } - trans->date = *date; - _free(date); + xaccTransSetDateSecs (trans, secs); trans->description = readString( fd, token ); if( trans->description == NULL ) @@ -869,37 +870,36 @@ readString( int fd, int token ) * token - the datafile version * * Return: the Date struct * \********************************************************************/ -static Date * +static time_t readDate( int fd, int token ) { int err=0; - Date *date = (Date *)_malloc(sizeof(Date)); + int day, month, year; + time_t secs; - err = read( fd, &(date->year), sizeof(int) ); + err = read( fd, &year, sizeof(int) ); if( err != sizeof(int) ) { - _free(date); - return NULL; + return 0; } - XACC_FLIP_INT (date->year); + XACC_FLIP_INT (year); - err = read( fd, &(date->month), sizeof(int) ); + err = read( fd, &month, sizeof(int) ); if( err != sizeof(int) ) { - _free(date); - return NULL; + return 0; } - XACC_FLIP_INT (date->month); + XACC_FLIP_INT (month); - err = read( fd, &(date->day), sizeof(int) ); + err = read( fd, &day, sizeof(int) ); if( err != sizeof(int) ) { - _free(date); - return NULL; + return 0; } - XACC_FLIP_INT (date->day); + XACC_FLIP_INT (day); - return date; + secs = xaccDMYToSec (day, month, year); + return secs; } /********************************************************************\ @@ -1149,6 +1149,7 @@ writeTransaction( int fd, Transaction *trans ) Split *s; int err=0; int i=0; + time_t secs; ENTER ("writeTransaction"); /* If we've already written this transaction, don't write @@ -1161,7 +1162,8 @@ writeTransaction( int fd, Transaction *trans ) err = writeString( fd, trans->num ); if( -1 == err ) return err; - err = writeDate( fd, &(trans->date) ); + secs = xaccTransGetDate (trans); + err = writeDate( fd, secs ); if( -1 == err ) return err; err = writeString( fd, trans->description ); @@ -1291,24 +1293,27 @@ writeString( int fd, char *str ) * Return: -1 on failure * \********************************************************************/ static int -writeDate( int fd, Date *date ) +writeDate( int fd, time_t secs ) { int err=0; int tmp; + struct tm *stm; + + stm = localtime (&secs); - tmp = date->year; + tmp = stm->tm_year +1900; XACC_FLIP_INT (tmp); err = write( fd, &tmp, sizeof(int) ); if( err != sizeof(int) ) return -1; - tmp = date->month; + tmp = stm->tm_mon+1; XACC_FLIP_INT (tmp); err = write( fd, &tmp, sizeof(int) ); if( err != sizeof(int) ) return -1; - tmp = date->day; + tmp = stm->tm_mday; XACC_FLIP_INT (tmp); err = write( fd, &tmp, sizeof(int) ); if( err != sizeof(int) ) diff --git a/src/engine/QIFIO.c b/src/engine/QIFIO.c index 4c23399313..64dbbbb4ac 100644 --- a/src/engine/QIFIO.c +++ b/src/engine/QIFIO.c @@ -39,6 +39,7 @@ #include "config.h" #include "Account.h" +#include "date.h" #include "Group.h" #include "FileIO.h" #include "Transaction.h" diff --git a/src/engine/Transaction.c b/src/engine/Transaction.c index 5f58a2b91b..a5629f1760 100644 --- a/src/engine/Transaction.c +++ b/src/engine/Transaction.c @@ -30,7 +30,6 @@ #include "Account.h" #include "AccountP.h" -#include "date.h" #include "Transaction.h" #include "TransactionP.h" #include "TransLog.h" @@ -70,6 +69,10 @@ xaccInitSplit( Split * split ) split->reconciled = NREC; split->damount = 0.0; split->share_price = 1.0; + + split->date_reconciled.tv_sec = 0; + split->date_reconciled.tv_nsec = 0; + split->balance = 0.0; split->cleared_balance = 0.0; split->reconciled_balance = 0.0; @@ -108,6 +111,9 @@ xaccFreeSplit( Split *split ) split->parent = NULL; split->acc = NULL; + split->date_reconciled.tv_sec = 0; + split->date_reconciled.tv_nsec = 0; + _free(split); } @@ -304,9 +310,12 @@ xaccInitTransaction( Transaction * trans ) trans->splits[2] = NULL; - trans->date.year = 1900; - trans->date.month = 1; - trans->date.day = 1; + trans->date_entered.tv_sec = 0; + trans->date_entered.tv_nsec = 0; + + trans->date_posted.tv_sec = 0; + trans->date_posted.tv_nsec = 0; + trans->open = 0; } @@ -351,9 +360,11 @@ xaccFreeTransaction( Transaction *trans ) trans->num = 0x0; trans->description = 0x0; - trans->date.year = 1900; - trans->date.month = 1; - trans->date.day = 1; + trans->date_entered.tv_sec = 0; + trans->date_entered.tv_nsec = 0; + + trans->date_posted.tv_sec = 0; + trans->date_posted.tv_nsec = 0; trans->open = 0; @@ -650,8 +661,24 @@ xaccTransOrder (Transaction **ta, Transaction **tb) if ( !(*ta) && !(*tb) ) return 0; /* if dates differ, return */ - retval = datecmp (&((*ta)->date), &((*tb)->date)); - if (retval) return retval; + if ( ((*ta)->date_posted.tv_sec) < + ((*tb)->date_posted.tv_sec)) { + return -1; + } else + if ( ((*ta)->date_posted.tv_sec) > + ((*tb)->date_posted.tv_sec)) { + return +1; + } + + /* else, seconds match. check nanoseconds */ + if ( ((*ta)->date_posted.tv_nsec) < + ((*tb)->date_posted.tv_nsec)) { + return -1; + } else + if ( ((*ta)->date_posted.tv_nsec) > + ((*tb)->date_posted.tv_nsec)) { + return +1; + } /* otherwise, sort on transaction strings */ da = (*ta)->num; @@ -709,15 +736,17 @@ xaccCountTransactions (Transaction **tarray) \********************************************************************/ void -xaccTransSetDate (Transaction *trans, int day, int mon, int year) +xaccTransSetDateSecs (Transaction *trans, time_t secs) { Split *split; Account *acc; int i=0; - trans->date.year = year; - trans->date.month = mon; - trans->date.day = day; + /* hack alert -- for right now, keep the posted and the entered + * dates in sync. Later, we'll have to split these up. */ + + trans->date_entered.tv_sec = secs; + trans->date_posted.tv_sec = secs; /* since the date has changed, we need to be careful to * make sure all associated splits are in proper order @@ -742,27 +771,37 @@ xaccTransSetDate (Transaction *trans, int day, int mon, int year) } void -xaccTransSetDateToday (Transaction *trans) +xaccTransSetDate (Transaction *trans, int day, int mon, int year) { - Date d; + struct tm date; + time_t secs; - todaysDate (&d); - xaccTransSetDate (trans, d.day, d.month, d.year); + date.tm_year = year - 1900; + date.tm_mon = mon - 1; + date.tm_mday = day; + date.tm_hour = 11; + date.tm_min = 0; + date.tm_sec = 0; + + /* compute number of seconds */ + secs = mktime (&date); + + xaccTransSetDateSecs (trans, secs); } void -xaccTransSetDateStr (Transaction *trans, char *str) +xaccTransSetDateToday (Transaction *trans) { - Date d; + time_t secs; - scanDate(str, &(d.day), &(d.month), &(d.year)); - xaccTransSetDate (trans, d.day, d.month, d.year); + secs = time (0); + xaccTransSetDateSecs (trans, secs); } + /********************************************************************\ \********************************************************************/ - void xaccTransSetNum (Transaction *trans, const char *xnum) { @@ -846,18 +885,10 @@ xaccTransGetDescription (Transaction *trans) return (trans->description); } -Date * +time_t xaccTransGetDate (Transaction *trans) { - return (&(trans->date)); -} - -char * -xaccTransGetDateStr (Transaction *trans) -{ - char buf [MAX_DATE_LENGTH]; - printDate(buf, trans->date.day, trans->date.month, trans->date.year%100); - return strdup (buf); + return (trans->date_posted.tv_sec); } int diff --git a/src/engine/Transaction.h b/src/engine/Transaction.h index 86ad17bcb0..150a53ac51 100644 --- a/src/engine/Transaction.h +++ b/src/engine/Transaction.h @@ -27,7 +27,8 @@ #define __XACC_TRANSACTION_H__ #include "config.h" -#include "date.h" /* for Date */ + +#include /* Values for the reconciled field in Transaction: */ #define CREC 'c' /* The transaction has been cleared */ @@ -75,7 +76,7 @@ void xaccTransBeginEdit (Transaction *); void xaccTransCommitEdit (Transaction *); void xaccTransSetDate (Transaction *, int day, int mon, int year); -void xaccTransSetDateStr (Transaction *, char *); +void xaccTransSetDateSecs (Transaction *, time_t); /* set the transaction date to the current system time. */ void xaccTransSetDateToday (Transaction *); @@ -117,8 +118,7 @@ Split * xaccTransGetSplit (Transaction *trans, int i); char * xaccTransGetNum (Transaction *); char * xaccTransGetDescription (Transaction *); -Date * xaccTransGetDate (Transaction *); -char * xaccTransGetDateStr (Transaction *); +time_t xaccTransGetDate (Transaction *); /* return the number of splits */ int xaccTransCountSplits (Transaction *trans); diff --git a/src/engine/TransactionP.h b/src/engine/TransactionP.h index 88c8f09fda..7715812642 100644 --- a/src/engine/TransactionP.h +++ b/src/engine/TransactionP.h @@ -45,8 +45,9 @@ #ifndef __XACC_TRANSACTION_P_H__ #define __XACC_TRANSACTION_P_H__ +#include + #include "config.h" -#include "date.h" /* for Date */ #include "Transaction.h" /* for typedefs */ @@ -66,18 +67,24 @@ * between "dining", "tips" and "taxes" categories. */ +typedef struct timespec Timespec; + struct _split { - Account *acc; /* back-pointer to debited/credited account */ - Transaction *parent; /* parent of split */ + Account *acc; /* back-pointer to debited/credited account */ + Transaction *parent; /* parent of split */ char * memo; char * action; /* Buy, Sell, Div, etc. */ char reconciled; double damount; /* num-shares; if > 0.0, deposit, else paymt */ double share_price; /* the share price, ==1.0 for bank account */ - /* the various "balances" are the sum of all of the values of - * all the splits in the account, up to and including this split */ + Timespec date_reconciled; /* date split was reconciled */ + + /* The various "balances" are the sum of all of the values of + * all the splits in the account, up to and including this split. + * These belances apply to a sorting order by date posted + * (not by date entered). */ double balance; double cleared_balance; double reconciled_balance; @@ -91,8 +98,9 @@ struct _split struct _transaction { + Timespec date_entered; /* date register entry was made */ + Timespec date_posted; /* date transaction was posted at bank */ char * num; /* transaction id */ - Date date; /* transaction date */ char * description; Split **splits; /* list of splits, null terminated */ diff --git a/src/engine/date.c b/src/engine/date.c index 62bd5498b0..9c8568e6cc 100644 --- a/src/engine/date.c +++ b/src/engine/date.c @@ -388,6 +388,72 @@ datecmp( Date *date1, Date *date2 ) } +/********************************************************************\ +\********************************************************************/ + +char * +xaccTransGetDateStr (Transaction *trans) +{ + char buf [MAX_DATE_LENGTH]; + time_t secs; + struct tm *date; + + secs = xaccTransGetDate (trans); + + date = localtime (&secs); + + printDate(buf, date->tm_mday, date->tm_mon+1, (date->tm_year)%100); + return strdup (buf); +} + +void +xaccTransSetDateStr (Transaction *trans, char *str) +{ + Date d; + + /* hack alert -- the date string should be parsed for time values */ + scanDate(str, &(d.day), &(d.month), &(d.year)); + xaccTransSetDate (trans, d.day, d.month, d.year); +} + +time_t +xaccDateToSec (Date *date) +{ + struct tm stm; + time_t secs; + + stm.tm_year = date->year - 1900; + stm.tm_mon = date->month - 1; + stm.tm_mday = date->day; + stm.tm_hour = 11; + stm.tm_min = 0; + stm.tm_sec = 0; + + /* compute number of seconds */ + secs = mktime (&stm); + + return (secs); +} + +time_t +xaccDMYToSec (int day, int month, int year) +{ + struct tm stm; + time_t secs; + + stm.tm_year = year - 1900; + stm.tm_mon = month - 1; + stm.tm_mday = day; + stm.tm_hour = 11; + stm.tm_min = 0; + stm.tm_sec = 0; + + /* compute number of seconds */ + secs = mktime (&stm); + + return (secs); +} + /********************** END OF FILE *********************************\ \********************************************************************/ diff --git a/src/engine/date.h b/src/engine/date.h index f38c9fad19..ff5e0874c7 100644 --- a/src/engine/date.h +++ b/src/engine/date.h @@ -90,6 +90,7 @@ #define __XACC_DATE_H__ #include "config.h" +#include "Transaction.h" typedef struct _date { int year; @@ -122,6 +123,12 @@ Date* todaysDate( Date *date ); int daysInMonth( int month , int year ); int datecmp( Date *date1, Date *date2 ); +char * xaccTransGetDateStr (Transaction *trans); +void xaccTransSetDateStr (Transaction *trans, char *str); + +time_t xaccDateToSec (Date *); +time_t xaccDMYToSec (int day, int month, int year); + /** GLOBALS *********************************************************/ extern DateFormat dateFormat;