Convert python bindings from timespec to time64.

This commit is contained in:
John Ralls 2018-01-20 12:32:42 -08:00
parent cc097a3f35
commit 91f4b19039
7 changed files with 98 additions and 83 deletions

View File

@ -4,7 +4,7 @@ ADD_SUBDIRECTORY(tests)
SET(PYEXEC_FILES __init__.py function_class.py gnucash_business.py gnucash_core.py)
IF (BUILDING_FROM_VCS)
SET(SWIG_FILES ${CMAKE_CURRENT_SOURCE_DIR}/gnucash_core.i ${CMAKE_CURRENT_SOURCE_DIR}/timespec.i)
SET(SWIG_FILES ${CMAKE_CURRENT_SOURCE_DIR}/gnucash_core.i ${CMAKE_CURRENT_SOURCE_DIR}/time64.i)
SET(GNUCASH_CORE_C_INCLUDES
${CONFIG_H}
${CMAKE_SOURCE_DIR}/libgnucash/engine/qofsession.h
@ -104,7 +104,7 @@ ENDIF()
SET(python_bindings_DATA ${PYEXEC_FILES}
gnucash_core.i
sqlite3test.c
timespec.i)
time64.i)
SET_LOCAL_DIST(python_bindings_DIST_local CMakeLists.txt Makefile.am
${python_bindings_DATA})

View File

@ -85,7 +85,7 @@
#include "Scrub3.h"
%}
%include <timespec.i>
%include <time64.i>
%include <base-typemaps.i>

View File

@ -34,8 +34,8 @@ class TestAccount( AccountSession ):
tx = Transaction(self.book)
tx.BeginEdit()
tx.SetCurrency(self.currency)
tx.SetDateEnteredTS(datetime.now())
tx.SetDatePostedTS(datetime.now())
tx.SetDateEnteredSecs(datetime.now())
tx.SetDatePostedSecs(datetime.now())
s1a = Split(self.book)
s1a.SetParent(tx)

View File

@ -1,6 +1,6 @@
from unittest import main
from datetime import datetime
from datetime import datetime, timedelta
from gnucash import Account, \
ACCT_TYPE_RECEIVABLE, ACCT_TYPE_INCOME, ACCT_TYPE_BANK, \
@ -56,6 +56,8 @@ class TestBusiness( BusinessSession ):
self.assertEqual( NAME, self.employee.GetUsername() )
def test_post(self):
self.assertEqual(self.today - timedelta(0, 0, self.today.microsecond),
self.invoice.GetDatePosted())
self.assertTrue( self.invoice.IsPosted() )
def test_owner(self):

83
bindings/python/time64.i Normal file
View File

@ -0,0 +1,83 @@
/*
* time64.i -- SWIG interface file for type translation of time64 types
*
* Copyright (C) 2008 ParIT Worker Co-operative <paritinfo@parit.ca>
*
* 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, contact:
*
* Free Software Foundation Voice: +1-617-542-5942
* 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652
* Boston, MA 02110-1301, USA gnu@gnu.org
*
* @author Mark Jenkins, ParIT Worker Co-operative <mark@parit.ca>
*/
/** @file
@brief SWIG interface file for type translation of time64 types
@author Mark Jenkins, ParIT Worker Co-operative <mark@parit.ca>
@author Jeff Green, ParIT Worker Co-operative <jeff@parit.ca>
@ingroup python_bindings */
// A typemap for converting python dates to time64 in functions that
// require time64 as an argument
%typemap(in) time64 {
PyDateTime_IMPORT;
struct tm time = {PyDateTime_DATE_GET_SECOND($input),
PyDateTime_DATE_GET_MINUTE($input),
PyDateTime_DATE_GET_HOUR($input),
PyDateTime_GET_DAY($input),
PyDateTime_GET_MONTH($input) - 1,
PyDateTime_GET_YEAR($input) - 1900};
$1 = gnc_mktime(&time);
}
// A typemap for converting python dates to time64 *, for functions that
// requires a time64 * as an argument. BIG ASSUMPTION, the function
// receiving this pointer is going to make a copy of the data. After the
// function call, the memory for the time64 used to perform this conversion
// is going to be lost, so make damn sure that the recipient of this pointer
// is NOT going dereference it sometime after this function call takes place.
//
// As far as I know, the xaccTransSetDate[Posted|Entered|Due]TS functions
// from Transaction.h are the only functions with time64 * that we re
// actually using. I have personally verified in the source that the pointer
// being produced by this typemap is being dereferenced, and the data copied
// in all three functions.
//
// The memory for the time64 used for this conversion is allocated on the
// stack. (SWIG will name the variables ts1, ts2, ts3...)
//
// Mark Jenkins <mark@parit.ca>
%typemap(in) time64 * (time64 secs) {
PyDateTime_IMPORT;
struct tm time = {PyDateTime_DATE_GET_SECOND($input),
PyDateTime_DATE_GET_MINUTE($input),
PyDateTime_DATE_GET_HOUR($input),
PyDateTime_GET_DAY($input),
PyDateTime_GET_MONTH($input) - 1,
PyDateTime_GET_YEAR($input) - 1900};
time64 secs = gnc_mktime(&time);
$1 = &secs;
}
// A typemap for converting time64 values returned from functions to
// python dates. Note that we can't use Python DateTime's fromtimestamp function because it relies upon libc's localtime. Note also that while we create times with timegm we retrieve it with localtime
%typemap(out) time64 {
PyDateTime_IMPORT;
struct tm t;
gnc_localtime_r(&$1, &t);
$result = PyDateTime_FromDateAndTime(t.tm_year + 1900, t.tm_mon + 1,
t.tm_mday, t.tm_hour, t.tm_min,
t.tm_sec, 0);
}

View File

@ -1,73 +0,0 @@
/*
* timespec.i -- SWIG interface file for type translation of Timespec types
*
* Copyright (C) 2008 ParIT Worker Co-operative <paritinfo@parit.ca>
*
* 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, contact:
*
* Free Software Foundation Voice: +1-617-542-5942
* 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652
* Boston, MA 02110-1301, USA gnu@gnu.org
*
* @author Mark Jenkins, ParIT Worker Co-operative <mark@parit.ca>
*/
/** @file
@brief SWIG interface file for type translation of Timespec types
@author Mark Jenkins, ParIT Worker Co-operative <mark@parit.ca>
@author Jeff Green, ParIT Worker Co-operative <jeff@parit.ca>
@ingroup python_bindings */
// A typemap for converting python dates to Timespec in functions that
// require Timespec as an argument
%typemap(in) Timespec {
PyDateTime_IMPORT;
$1 = gnc_dmy2timespec(PyDateTime_GET_DAY($input),
PyDateTime_GET_MONTH($input),
PyDateTime_GET_YEAR($input) );
}
// A typemap for converting python dates to Timespec *, for functions that
// requires a Timespec * as an argument. BIG ASSUMPTION, the function
// receiving this pointer is going to make a copy of the data. After the
// function call, the memory for the Timespec used to perform this conversion
// is going to be lost, so make damn sure that the recipient of this pointer
// is NOT going dereference it sometime after this function call takes place.
//
// As far as I know, the xaccTransSetDate[Posted|Entered|Due]TS functions
// from Transaction.h are the only functions with Timespec * that we re
// actually using. I have personally verified in the source that the pointer
// being produced by this typemap is being dereferenced, and the data copied
// in all three functions.
//
// The memory for the Timespec used for this conversion is allocated on the
// stack. (SWIG will name the variables ts1, ts2, ts3...)
//
// Mark Jenkins <mark@parit.ca>
%typemap(in) Timespec * (Timespec ts) {
PyDateTime_IMPORT;
ts = gnc_dmy2timespec(PyDateTime_GET_DAY($input),
PyDateTime_GET_MONTH($input),
PyDateTime_GET_YEAR($input) );
$1 = &ts;
}
// A typemap for converting Timespec values returned from functions to
// python dates.
%typemap(out) Timespec {
int year, month, day;
gnc_timespec2dmy($1, &day, &month, &year);
PyDateTime_IMPORT;
$result = PyDate_FromDate(year, month, day);
}

View File

@ -29,6 +29,7 @@
#include <config.h>
#include <stdint.h>
#include <glib.h>
#include <glib/gi18n.h>
#include <qofinstance-p.h>
@ -136,6 +137,8 @@ G_DEFINE_TYPE(GncInvoice, gnc_invoice, QOF_TYPE_INSTANCE);
static void
gnc_invoice_init(GncInvoice* inv)
{
inv->date_posted = INT64_MAX;
inv->date_opened = INT64_MAX;
}
static void
@ -807,13 +810,13 @@ qofInvoiceGetBillTo (GncInvoice *invoice)
time64 gncInvoiceGetDateOpened (const GncInvoice *invoice)
{
if (!invoice) return 0;
if (!invoice) return INT64_MAX;
return invoice->date_opened;
}
time64 gncInvoiceGetDatePosted (const GncInvoice *invoice)
{
if (!invoice) return 0;
if (!invoice) return INT64_MAX;
return invoice->date_posted;
}
@ -1718,7 +1721,7 @@ gncInvoiceUnpost (GncInvoice *invoice, gboolean reset_tax_tables)
invoice->posted_acc = NULL;
invoice->posted_txn = NULL;
invoice->posted_lot = NULL;
invoice->date_posted = 0;
invoice->date_posted = INT64_MAX;
/* if we've been asked to reset the tax tables, then do so */
if (reset_tax_tables)
@ -1855,7 +1858,7 @@ gncInvoiceApplyPayment (const GncInvoice *invoice, Transaction *txn,
static gboolean gncInvoiceDateExists (time64 date)
{
return date;
return date != INT64_MAX;
}
gboolean gncInvoiceIsPosted (const GncInvoice *invoice)