deprecated.c

00001 /* *****************************************************************\
00002  * deprecated.c -- QOF deprecated function replacements            *
00003  * Copyright (c) 2005 Neil Williams <linux@codehelp.co.uk>          *
00004  *                                                                  *
00005  * This program is free software; you can redistribute it and/or    *
00006  * modify it under the terms of the GNU General Public License as   *
00007  * published by the Free Software Foundation; either version 2 of   *
00008  * the License, or (at your option) any later version.              *
00009  *                                                                  *
00010  * This program is distributed in the hope that it will be useful,  *
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
00013  * GNU General Public License for more details.                     *
00014  *                                                                  *
00015  * You should have received a copy of the GNU General Public License*
00016  * along with this program; if not, contact:                        *
00017  *                                                                  *
00018  * Free Software Foundation           Voice:  +1-617-542-5942       *
00019  * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
00020  * Boston, MA  02110-1301,  USA       gnu@gnu.org                   *
00021  *                                                                  *
00022 \********************************************************************/
00023 
00024 #include "config.h"
00025 #ifndef QOF_DISABLE_DEPRECATED
00026 
00027 /* keep the deprecated functions close to the originals -
00028 including use of localtime - that's why these are deprecated! */
00029 #include <stdlib.h>
00030 #include <ctype.h>
00031 #include <sys/time.h>
00032 
00033 #ifdef HAVE_LANGINFO_H
00034 #define HAVE_LANGINFO_D_FMT 1
00035 #include <langinfo.h>
00036 #endif
00037 #ifdef HAVE_LANGINFO_D_FMT
00038 #  define QOF_D_FMT (nl_langinfo (D_FMT))
00039 #  define QOF_D_T_FMT (nl_langinfo (D_T_FMT))
00040 #  define QOF_T_FMT (nl_langinfo (T_FMT))
00041 #else
00042 #  define QOF_D_FMT   "%F"
00043 #  define QOF_D_T_FMT "%F %r"
00044 #  define QOF_T_FMT   "%i"
00045 #endif
00046 
00047 #include <glib.h>
00048 #include "qof.h"
00049 static QofLogModule log_module = "deprecated";
00050 static FILE *fout = NULL;
00051 
00052 /* Don't be fooled: gnc_trace_num_spaces has external linkage and
00053    static storage, but can't be defined with 'extern' because it has
00054    an initializer, and can't be declared with 'static' because that
00055    would give it internal linkage. (this is why it is deprecated) */
00056 gint __attribute__ ((unused)) gnc_trace_num_spaces = 0;
00057 void
00058 gnc_log_init (void)
00059 {
00060     qof_log_init ();
00061 }
00062 
00063 void
00064 gnc_set_log_level (QofLogModule log_module, gncLogLevel level)
00065 {
00066     qof_log_set_level (log_module, (QofLogLevel) level);
00067 }
00068 
00069 void
00070 gnc_set_log_level_global (gncLogLevel level)
00071 {
00072     qof_log_set_level_registered ((QofLogLevel) level);
00073 }
00074 
00075 void
00076 qof_log_set_level_global (QofLogLevel level)
00077 {
00078     qof_log_set_level_registered ((QofLogLevel) level);
00079 }
00080 
00081 void
00082 gnc_set_logfile (FILE * outfile)
00083 {
00084     qof_log_set_file (outfile);
00085 }
00086 const char *
00087 gnc_log_prettify (const char *name)
00088 {
00089     return qof_log_prettify (name);
00090 }
00091 
00092 void
00093 gnc_start_clock (int a __attribute__ ((unused)), 
00094     QofLogModule b __attribute__ ((unused)), 
00095     gncLogLevel c __attribute__ ((unused)), 
00096     const char *d __attribute__ ((unused)),
00097     const char *e __attribute__ ((unused)), ...)
00098 {
00099 }
00100 void
00101 gnc_report_clock (int a __attribute__ ((unused)), 
00102     QofLogModule b __attribute__ ((unused)), 
00103     gncLogLevel c __attribute__ ((unused)), 
00104     const char *d __attribute__ ((unused)),
00105     const char *e __attribute__ ((unused)), ...)
00106 {
00107 }
00108 void
00109 gnc_report_clock_total (int a __attribute__ ((unused)), 
00110     QofLogModule b __attribute__ ((unused)), 
00111     gncLogLevel c __attribute__ ((unused)),
00112     const char *d __attribute__ ((unused)), 
00113     const char *e __attribute__ ((unused)), ...)
00114 {
00115 }
00116 
00117 gboolean
00118 gnc_should_log (QofLogModule log_module, gncLogLevel log_level)
00119 {
00120     return qof_log_check (log_module, log_level);
00121 }
00122 
00123 gint
00124 gnc_engine_register_event_handler (GNCEngineEventHandler handler,
00125     gpointer user_data)
00126 {
00127     return qof_event_register_old_handler (handler, user_data);
00128 }
00129 
00130 void
00131 gnc_engine_unregister_event_handler (gint handler_id)
00132 {
00133     qof_event_unregister_handler (handler_id);
00134 }
00135 
00136 void
00137 gnc_engine_suspend_events (void)
00138 {
00139     qof_event_suspend ();
00140 }
00141 
00142 void
00143 gnc_engine_resume_events (void)
00144 {
00145     qof_event_resume ();
00146 }
00147 
00148 void
00149 gnc_engine_gen_event (QofEntity * entity, GNCEngineEventType event_type)
00150 {
00151     qof_event_gen (entity, event_type, NULL);
00152 }
00153 
00154 QofBookMergeData *
00155 qof_book_mergeInit (QofBook * importBook, QofBook * targetBook)
00156 {
00157     return qof_book_merge_init (importBook, targetBook);
00158 }
00159 
00160 QofBookMergeData *
00161 qof_book_mergeUpdateResult (QofBookMergeData * mergeData,
00162     QofBookMergeResult tag)
00163 {
00164     return qof_book_merge_update_result (mergeData, tag);
00165 }
00166 
00167 gint
00168 qof_book_mergeCommit (QofBookMergeData * mergeData)
00169 {
00170     return qof_book_merge_commit (mergeData);
00171 }
00172 
00173 void
00174 qof_book_mergeRuleForeach (QofBookMergeData * mergeData,
00175     QofBookMergeRuleForeachCB cb, QofBookMergeResult mergeResult)
00176 {
00177     qof_book_merge_rule_foreach (mergeData, cb, mergeResult);
00178 }
00179 
00180 gpointer
00181 gnc_string_cache_insert (gconstpointer key)
00182 {
00183     return (gpointer) qof_util_string_cache_insert (key);
00184 }
00185 
00186 gchar *
00187 gnc_stpcpy (gchar * dest, const gchar * src)
00188 {
00189     return g_stpcpy (dest, src);
00190 }
00191 
00192 GCache *
00193 gnc_engine_get_string_cache (void)
00194 {
00195     return qof_util_get_string_cache ();
00196 }
00197 
00198 void
00199 gnc_engine_string_cache_destroy (void)
00200 {
00201     qof_util_string_cache_destroy ();
00202 }
00203 
00204 void
00205 gnc_string_cache_remove (gconstpointer key)
00206 {
00207     qof_util_string_cache_remove (key);
00208 }
00209 
00210 gboolean
00211 gnc_date_string_to_dateformat (const gchar * format_string,
00212     QofDateFormat * format)
00213 {
00214     QofDateFormat df;
00215 
00216     df = qof_date_format_from_name (format_string);
00217     if (df < 0)
00218         return TRUE;
00219     *format = df;
00220     return FALSE;
00221 }
00222 
00223 gboolean
00224 gnc_date_string_to_monthformat (const gchar * format_string,
00225     GNCDateMonthFormat * format)
00226 {
00227     if (!format_string)
00228         return TRUE;
00229 
00230     if (!strcmp (format_string, "number"))
00231         *format = GNCDATE_MONTH_NUMBER;
00232     else if (!strcmp (format_string, "abbrev"))
00233         *format = GNCDATE_MONTH_ABBREV;
00234     else if (!strcmp (format_string, "name"))
00235         *format = GNCDATE_MONTH_NAME;
00236     else
00237         return TRUE;
00238 
00239     return FALSE;
00240 }
00241 static inline QofTime *
00242 timespecToQofTime (Timespec ts)
00243 {
00244     QofTime *time;
00245 
00246     time = qof_time_new ();
00247     qof_time_set_secs (time, ts.tv_sec);
00248     qof_time_set_nanosecs (time, ts.tv_nsec);
00249     return time;
00250 }
00251 static inline Timespec
00252 qof_time_to_Timespec (QofTime * time)
00253 {
00254     Timespec ts;
00255 
00256     ts.tv_sec = qof_time_get_secs (time);
00257     ts.tv_nsec = qof_time_get_nanosecs (time);
00258     return ts;
00259 }
00260 static inline Timespec
00261 null_timespec (void)
00262 {
00263     Timespec ts;
00264 
00265     ts.tv_sec = 0;
00266     ts.tv_nsec = 0;
00267     return ts;
00268 }
00269 
00270 gboolean
00271 timespec_equal (const Timespec * ta, const Timespec * tb)
00272 {
00273     QofTime *qta, *qtb;
00274     gboolean retval;
00275 
00276     qta = timespecToQofTime (*ta);
00277     qtb = timespecToQofTime (*tb);
00278     retval = qof_time_equal (qta, qtb);
00279     qof_time_free (qta);
00280     qof_time_free (qtb);
00281     return retval;
00282 }
00283 
00284 gint
00285 timespec_cmp (const Timespec * ta, const Timespec * tb)
00286 {
00287     QofTime *qta, *qtb;
00288     gint retval;
00289 
00290     qta = timespecToQofTime (*ta);
00291     qtb = timespecToQofTime (*tb);
00292     retval = qof_time_cmp (qta, qtb);
00293     qof_time_free (qta);
00294     qof_time_free (qtb);
00295     return retval;
00296 }
00297 
00298 void
00299 timespecFromTime_t (Timespec * ts, time_t t)
00300 {
00301     QofTime *time;
00302 
00303     time = qof_time_new ();
00304     time = qof_time_from_time_t (t, 0);
00305     *ts = qof_time_to_Timespec (time);
00306     qof_time_free (time);
00307 }
00308 
00309 time_t
00310 timespecToTime_t (Timespec ts)
00311 {
00312     return ts.tv_sec;
00313 }
00314 
00315 Timespec
00316 timespec_diff (const Timespec * ta, const Timespec * tb)
00317 {
00318     Timespec ts;
00319     QofTime *qta, *qtb, *qt;
00320 
00321     qta = timespecToQofTime (*ta);
00322     qtb = timespecToQofTime (*tb);
00323     qt = qof_time_diff (qta, qtb);
00324     ts = qof_time_to_Timespec (qt);
00325     qof_time_free (qta);
00326     qof_time_free (qtb);
00327     qof_time_free (qt);
00328     return ts;
00329 }
00330 
00331 Timespec
00332 timespec_abs (const Timespec * t)
00333 {
00334     Timespec ts;
00335     QofTime *qt = timespecToQofTime (*t);
00336     qof_time_abs (qt);
00337     ts = qof_time_to_Timespec (qt);
00338     qof_time_free (qt);
00339     return ts;
00340 }
00341 
00342 Timespec
00343 timespecCanonicalDayTime (Timespec t)
00344 {
00345     struct tm tm, *result;
00346     Timespec retval;
00347 
00348     time_t t_secs = t.tv_sec + (t.tv_nsec / QOF_NSECS);
00349     result = localtime(&t_secs);
00350     tm = *result;
00351     gnc_tm_set_day_middle(&tm);
00352     retval.tv_sec = mktime(&tm);
00353     retval.tv_nsec = 0;
00354     return retval;
00355 }
00356 
00357 time_t
00358 qof_date_dmy_to_sec (gint day, gint month, gint year)
00359 {
00360     QofTime *qt;
00361     QofDate *qd;
00362     time_t retval;
00363 
00364     qd = qof_date_new ();
00365     qd->qd_mday = day;
00366     qd->qd_mon  = month;
00367     qd->qd_year = year;
00368     qt = qof_date_to_qtime (qd);
00369     retval = qof_time_get_secs (qt);
00370     qof_time_free (qt);
00371     qof_date_free (qd);
00372     return retval;
00373 }
00374 
00375 size_t
00376 qof_print_hours_elapsed_buff (char *buff, size_t len, int secs,
00377     gboolean show_secs)
00378 {
00379     size_t flen;
00380     if (0 <= secs)
00381     {
00382         if (show_secs)
00383         {
00384             flen = g_snprintf (buff, len,
00385                 "%02d:%02d:%02d", (int) (secs / 3600),
00386                 (int) ((secs % 3600) / 60), (int) (secs % 60));
00387         }
00388         else
00389         {
00390             flen = g_snprintf (buff, len,
00391                 "%02d:%02d", (int) (secs / 3600),
00392                 (int) ((secs % 3600) / 60));
00393         }
00394     }
00395     else
00396     {
00397         if (show_secs)
00398         {
00399             flen = g_snprintf (buff, len,
00400                 "-%02d:%02d:%02d", (int) (-secs / 3600),
00401                 (int) ((-secs % 3600) / 60), (int) (-secs % 60));
00402         }
00403         else
00404         {
00405             flen = g_snprintf (buff, len,
00406                 "-%02d:%02d", (int) (-secs / 3600),
00407                 (int) ((-secs % 3600) / 60));
00408         }
00409     }
00410     return flen;
00411 }
00412 
00413 size_t
00414 qof_print_minutes_elapsed_buff (char *buff, size_t len, int secs,
00415     gboolean show_secs)
00416 {
00417     size_t flen;
00418     if (0 <= secs)
00419     {
00420         if (show_secs)
00421         {
00422             flen = g_snprintf (buff, len,
00423                 "%02d:%02d", (int) (secs / 60), (int) (secs % 60));
00424         }
00425         else
00426         {
00427             flen = g_snprintf (buff, len, "%02d", (int) (secs / 60));
00428         }
00429     }
00430     else
00431     {
00432         if (show_secs)
00433         {
00434             flen = g_snprintf (buff, len,
00435                 "-%02d:%02d", (int) (-secs / 60), (int) (-secs % 60));
00436         }
00437         else
00438         {
00439             flen = g_snprintf (buff, len, "-%02d", (int) (-secs / 60));
00440         }
00441     }
00442     return flen;
00443 }
00444 
00445 size_t
00446 qof_print_date_time_buff (char *buff, size_t len, time_t secs)
00447 {
00448     int flen;
00449     int day, month, year, hour, min, sec;
00450     struct tm ltm, gtm;
00451 
00452     if (!buff)
00453         return 0;
00454     ltm = *localtime (&secs);
00455     day = ltm.tm_mday;
00456     month = ltm.tm_mon + 1;
00457     year = ltm.tm_year + 1900;
00458     hour = ltm.tm_hour;
00459     min = ltm.tm_min;
00460     sec = ltm.tm_sec;
00461     switch (qof_date_format_get_current ())
00462     {
00463     case QOF_DATE_FORMAT_UK:
00464         flen =
00465             g_snprintf (buff, len, "%2d/%2d/%-4d %2d:%02d", day, month,
00466             year, hour, min);
00467         break;
00468     case QOF_DATE_FORMAT_CE:
00469         flen =
00470             g_snprintf (buff, len, "%2d.%2d.%-4d %2d:%02d", day, month,
00471             year, hour, min);
00472         break;
00473     case QOF_DATE_FORMAT_ISO:
00474         flen =
00475             g_snprintf (buff, len, "%04d-%02d-%02d %02d:%02d", year, month,
00476             day, hour, min);
00477         break;
00478     case QOF_DATE_FORMAT_UTC:
00479         {
00480             gtm = *gmtime (&secs);
00481             flen = strftime (buff, len, QOF_UTC_DATE_FORMAT, &gtm);
00482             break;
00483         }
00484     case QOF_DATE_FORMAT_LOCALE:
00485         {
00486             flen = strftime (buff, len, QOF_D_T_FMT, &ltm);
00487         }
00488         break;
00489 
00490     case QOF_DATE_FORMAT_US:
00491     default:
00492         flen =
00493             g_snprintf (buff, len, "%2d/%2d/%-4d %2d:%02d", month, day,
00494             year, hour, min);
00495         break;
00496     }
00497     return flen;
00498 }
00499 
00500 size_t
00501 qof_print_time_buff (gchar * buff, size_t len, time_t secs)
00502 {
00503     gint flen;
00504     struct tm ltm, gtm;
00505 
00506     if (!buff)
00507         return 0;
00508     if (qof_date_format_get_current () == QOF_DATE_FORMAT_UTC)
00509     {
00510         gtm = *gmtime (&secs);
00511         flen = strftime (buff, len, QOF_UTC_DATE_FORMAT, &gtm);
00512         return flen;
00513     }
00514     ltm = *localtime (&secs);
00515     flen = strftime (buff, len, QOF_T_FMT, &ltm);
00516 
00517     return flen;
00518 }
00519 
00520 gboolean
00521 qof_is_same_day (time_t ta, time_t tb)
00522 {
00523     gboolean retval;
00524     GDate *da, *db;
00525 
00526     da = g_date_new ();
00527     db = g_date_new ();
00528     g_date_set_time_t (da, ta);
00529     g_date_set_time_t (db, tb);
00530     retval = g_date_compare (da, db);
00531     g_date_free (da);
00532     g_date_free (db);
00533     return retval;
00534 }
00535 
00536 void
00537 gnc_tm_set_day_start (struct tm *tm)
00538 {
00539     tm->tm_hour = 0;
00540     tm->tm_min = 0;
00541     tm->tm_sec = 0;
00542     tm->tm_isdst = -1;
00543 }
00544 
00545 void
00546 gnc_tm_get_day_start (struct tm *tm, time_t time_val)
00547 {
00548     tm = localtime_r (&time_val, tm);
00549     gnc_tm_set_day_start (tm);
00550 }
00551 
00552 void
00553 gnc_tm_set_day_middle (struct tm *tm)
00554 {
00555     tm->tm_hour = 12;
00556     tm->tm_min = 0;
00557     tm->tm_sec = 0;
00558     tm->tm_isdst = -1;
00559 }
00560 
00561 void
00562 gnc_tm_set_day_end (struct tm *tm)
00563 {
00564     tm->tm_hour = 23;
00565     tm->tm_min = 59;
00566     tm->tm_sec = 59;
00567     tm->tm_isdst = -1;
00568 }
00569 
00570 void
00571 gnc_tm_get_day_end (struct tm *tm, time_t time_val)
00572 {
00573     tm = localtime_r (&time_val, tm);
00574     gnc_tm_set_day_end (tm);
00575 }
00576 
00577 time_t
00578 gnc_timet_get_day_start (time_t time_val)
00579 {
00580     struct tm tm;
00581 
00582     gnc_tm_get_day_start (&tm, time_val);
00583     return mktime (&tm);
00584 }
00585 
00586 time_t
00587 gnc_timet_get_day_end (time_t time_val)
00588 {
00589     struct tm tm;
00590 
00591     gnc_tm_get_day_end (&tm, time_val);
00592     return mktime (&tm);
00593 }
00594 
00595 #ifndef GNUCASH_MAJOR_VERSION
00596 time_t
00597 gnc_timet_get_day_start_gdate (GDate * date)
00598 {
00599     struct tm stm;
00600     time_t secs;
00601 
00602     stm.tm_year = g_date_get_year (date) - 1900;
00603     stm.tm_mon = g_date_get_month (date) - 1;
00604     stm.tm_mday = g_date_get_day (date);
00605     gnc_tm_set_day_start (&stm);
00606     secs = mktime (&stm);
00607     return secs;
00608 }
00609 
00610 time_t
00611 gnc_timet_get_day_end_gdate (GDate * date)
00612 {
00613     struct tm stm;
00614     time_t secs;
00615 
00616     stm.tm_year = g_date_get_year (date) - 1900;
00617     stm.tm_mon = g_date_get_month (date) - 1;
00618     stm.tm_mday = g_date_get_day (date);
00619     gnc_tm_set_day_end (&stm);
00620     secs = mktime (&stm);
00621     return secs;
00622 }
00623 #endif
00624 int
00625 gnc_date_my_last_mday (int month, int year)
00626 {
00627     return g_date_get_days_in_month (month, year);
00628 }
00629 
00630 int
00631 date_get_last_mday (struct tm *tm)
00632 {
00633     return g_date_get_days_in_month (tm->tm_mon + 1, tm->tm_year + 1900);
00634 }
00635 
00636 gboolean
00637 date_is_last_mday (struct tm * tm)
00638 {
00639     return (tm->tm_mday ==
00640         g_date_get_days_in_month (tm->tm_mon + 1, tm->tm_year + 1900));
00641 }
00642 
00643 int
00644 gnc_timespec_last_mday (Timespec t)
00645 {
00646     /* Replacement code should not use localtime */
00647     struct tm *result;
00648     time_t t_secs = t.tv_sec + (t.tv_nsec / QOF_NSECS);
00649     result = localtime (&t_secs);
00650     return date_get_last_mday (result);
00651 }
00652 
00653 void
00654 gnc_tm_get_today_start (struct tm *tm)
00655 {
00656     gnc_tm_get_day_start (tm, time (NULL));
00657 }
00658 
00659 void
00660 gnc_tm_get_today_end (struct tm *tm)
00661 {
00662     gnc_tm_get_day_end (tm, time (NULL));
00663 }
00664 
00665 time_t
00666 gnc_timet_get_today_start (void)
00667 {
00668     struct tm tm;
00669 
00670     gnc_tm_get_day_start (&tm, time (NULL));
00671     return mktime (&tm);
00672 }
00673 
00674 time_t
00675 gnc_timet_get_today_end (void)
00676 {
00677     struct tm tm;
00678 
00679     gnc_tm_get_day_end (&tm, time (NULL));
00680     return mktime (&tm);
00681 }
00682 
00683 char *
00684 xaccDateUtilGetStamp (time_t thyme)
00685 {
00686     struct tm *stm;
00687 
00688     stm = localtime (&thyme);
00689     return g_strdup_printf ("%04d%02d%02d%02d%02d%02d",
00690         (stm->tm_year + 1900),
00691         (stm->tm_mon + 1),
00692         stm->tm_mday, stm->tm_hour, stm->tm_min, stm->tm_sec);
00693 }
00694 
00695 size_t
00696 qof_print_date_dmy_buff (char *buff, size_t len, int day, int month,
00697     int year)
00698 {
00699     int flen;
00700     if (!buff)
00701         return 0;
00702     switch (qof_date_format_get_current ())
00703     {
00704     case QOF_DATE_FORMAT_UK:
00705         flen = g_snprintf (buff, len, "%2d/%2d/%-4d", day, month, year);
00706         break;
00707     case QOF_DATE_FORMAT_CE:
00708         flen = g_snprintf (buff, len, "%2d.%2d.%-4d", day, month, year);
00709         break;
00710     case QOF_DATE_FORMAT_LOCALE:
00711         {
00712             struct tm tm_str;
00713             time_t t;
00714             tm_str.tm_mday = day;
00715             tm_str.tm_mon = month - 1;
00716             tm_str.tm_year = year - 1900;
00717             gnc_tm_set_day_start (&tm_str);
00718             t = mktime (&tm_str);
00719             localtime_r (&t, &tm_str);
00720             flen = strftime (buff, len, QOF_D_FMT, &tm_str);
00721             if (flen != 0)
00722                 break;
00723         }
00724     case QOF_DATE_FORMAT_ISO:
00725     case QOF_DATE_FORMAT_UTC:
00726         flen = g_snprintf (buff, len, "%04d-%02d-%02d", year, month, day);
00727         break;
00728     case QOF_DATE_FORMAT_US:
00729     default:
00730         flen = g_snprintf (buff, len, "%2d/%2d/%-4d", month, day, year);
00731         break;
00732     }
00733     return flen;
00734 }
00735 
00736 size_t
00737 qof_print_date_buff (char *buff, size_t len, time_t t)
00738 {
00739     struct tm *theTime;
00740     if (!buff)
00741         return 0;
00742     theTime = localtime (&t);
00743     return qof_print_date_dmy_buff (buff, len,
00744         theTime->tm_mday, theTime->tm_mon + 1, theTime->tm_year + 1900);
00745 }
00746 
00747 size_t
00748 qof_print_gdate (gchar * buf, size_t len __attribute__ ((unused)), 
00749     GDate * gd)
00750 {
00751     QofDateFormat df;
00752     QofDate *qd;
00753     gchar *str;
00754 
00755     df = qof_date_format_get_current ();
00756     qd = qof_date_from_gdate (gd);
00757     str = qof_date_print (qd, df);
00758     qof_date_free (qd);
00759     g_stpcpy (buf, str);
00760     g_free (str);
00761     return strlen (buf);
00762 }
00763 
00764 gchar *
00765 qof_print_date (time_t t)
00766 {
00767     QofDateFormat df;
00768     QofTime *time;
00769     gchar *str;
00770 
00771     df = qof_date_format_get_current ();
00772     time = qof_time_from_time_t (t, 0);
00773     str = qof_date_print (qof_date_from_qtime (time), df);
00774     qof_time_free (time);
00775     return str;
00776 }
00777 const gchar *
00778 gnc_print_date (Timespec ts)
00779 {
00780     static gchar buff[MAX_DATE_LENGTH];
00781     QofDateFormat df;
00782     QofTime *time;
00783     gchar *str;
00784 
00785     df = qof_date_format_get_current ();
00786     ENTER (" using date format %d", df);
00787     time = timespecToQofTime (ts);
00788     str = qof_date_print (qof_date_from_qtime (time), df);
00789     qof_time_free (time);
00790     g_stpcpy (buff, str);
00791     g_free (str);
00792     LEAVE (" printing %s", buff);
00793     return buff;
00794 }
00795 
00796 /* This is now user configured through the gnome options system() */
00797 static QofDateFormat dateFormat = QOF_DATE_FORMAT_LOCALE;
00798 static QofDateFormat prevQofDateFormat = QOF_DATE_FORMAT_LOCALE;
00799 
00800 static gboolean
00801 qof_scan_date_internal (const char *buff, int *day, int *month, int *year,
00802                   QofDateFormat which_format)
00803 {
00804    char *dupe, *tmp, *first_field, *second_field, *third_field;
00805    int iday, imonth, iyear;
00806    struct tm *now, utc;
00807    time_t secs;
00808 
00809    if (!buff) return(FALSE);
00810 
00811     if(which_format == QOF_DATE_FORMAT_UTC)
00812     {
00813         if(strptime(buff, QOF_UTC_DATE_FORMAT, &utc)) {
00814             *day = utc.tm_mday;
00815             *month = utc.tm_mon + 1;
00816             *year = utc.tm_year + 1900;
00817             return TRUE;
00818         }
00819         else { return FALSE; }
00820     }
00821    dupe = g_strdup (buff);
00822 
00823    tmp = dupe;
00824    first_field = NULL;
00825    second_field = NULL;
00826    third_field = NULL;
00827 
00828    /* Use strtok to find delimiters */
00829    if (tmp) {
00830      static char *delims = ".,-+/\\() ";
00831 
00832       first_field = strtok (tmp, delims);
00833       if (first_field) {
00834          second_field = strtok (NULL, delims);
00835          if (second_field) {
00836             third_field = strtok (NULL, delims);
00837          }
00838       }
00839    }
00840 
00841    /* If any fields appear to be blank, use today's date */
00842    time (&secs);
00843    now = localtime (&secs);
00844    iday = now->tm_mday; 
00845    imonth = now->tm_mon+1;
00846    iyear = now->tm_year+1900;
00847 
00848    /* get numeric values */
00849    switch (which_format)
00850    {
00851      case QOF_DATE_FORMAT_LOCALE:
00852        if (buff[0] != '\0')
00853        {
00854          struct tm thetime;
00855 
00856          /* Parse time string. */
00857          memset(&thetime, -1, sizeof(struct tm));
00858          strptime (buff, QOF_D_FMT, &thetime);
00859 
00860          if (third_field) {
00861            /* Easy.  All three values were parsed. */
00862            iyear = thetime.tm_year + 1900;
00863            iday = thetime.tm_mday;
00864            imonth = thetime.tm_mon + 1;
00865          } else if (second_field) {
00866            /* Hard. Two values parsed.  Figure out the ordering. */
00867            if (thetime.tm_year == -1) {
00868              /* %m-%d or %d-%m. Don't care. Already parsed correctly. */
00869              iday = thetime.tm_mday;
00870              imonth = thetime.tm_mon + 1;
00871            } else if (thetime.tm_mon != -1) {
00872              /* Must be %Y-%m-%d. Reparse as %m-%d.*/
00873              imonth = atoi(first_field);
00874              iday = atoi(second_field);
00875            } else {
00876              /* Must be %Y-%d-%m. Reparse as %d-%m. */
00877              iday = atoi(first_field);
00878              imonth = atoi(second_field);
00879            }
00880          } else if (first_field) {
00881            iday = atoi(first_field);
00882          }
00883        }
00884        break;
00885      case QOF_DATE_FORMAT_UK:
00886      case QOF_DATE_FORMAT_CE:
00887        if (third_field) {
00888          iday = atoi(first_field);
00889          imonth = atoi(second_field);
00890          iyear = atoi(third_field);
00891        } else if (second_field) {
00892          iday = atoi(first_field);
00893          imonth = atoi(second_field);
00894        } else if (first_field) {
00895          iday = atoi(first_field);
00896        }
00897        break;
00898      case QOF_DATE_FORMAT_ISO:
00899        if (third_field) {
00900          iyear = atoi(first_field);
00901          imonth = atoi(second_field);
00902          iday = atoi(third_field);
00903        } else if (second_field) {
00904          imonth = atoi(first_field);
00905          iday = atoi(second_field);
00906        } else if (first_field) {
00907          iday = atoi(first_field);
00908        }
00909        break;
00910     case QOF_DATE_FORMAT_US:
00911     default:
00912        if (third_field) {
00913          imonth = atoi(first_field);
00914          iday = atoi(second_field);
00915          iyear = atoi(third_field);
00916        } else if (second_field) {
00917          imonth = atoi(first_field);
00918          iday = atoi(second_field);
00919        } else if (first_field) {
00920          iday = atoi(first_field);
00921        }
00922        break;
00923    }
00924 
00925    g_free (dupe);
00926 
00927    if ((12 < imonth) || (31 < iday)) 
00928    {
00929      /* 
00930       * Ack! Thppfft!  Someone just fed this routine a string in the
00931       * wrong date format.  This is known to happen if a register
00932       * window is open when changing the date format.  Try the
00933       * previous date format.  If that doesn't work, see if we can
00934       * exchange month and day. If that still doesn't work,
00935       * bail and give the caller what they asked for (garbage) 
00936       * parsed in the new format.
00937       *
00938       * Note: This test cannot detect any format change that only
00939       * swaps month and day field, if the day is 12 or less.  This is
00940       * deemed acceptable given the obscurity of this bug.
00941       */
00942      if ((which_format != prevQofDateFormat) &&
00943          qof_scan_date_internal(buff, day, month, year, prevQofDateFormat))
00944      {
00945        return(TRUE);
00946      }
00947      if ((12 < imonth) && (12 >= iday))
00948      {
00949         int tmp = imonth; imonth = iday; iday = tmp;
00950      } 
00951       else
00952       {
00953         return FALSE;
00954       }
00955    }
00956 
00957    /* If the year entered is smaller than 100, assume we mean the current
00958       century (and are not revising some roman emperor's books) */
00959    if (iyear < 100)
00960      iyear += ((int) ((now->tm_year+1950-iyear)/100)) * 100;
00961 
00962    if (year) *year=iyear;
00963    if (month) *month=imonth;
00964    if (day) *day=iday;
00965    return(TRUE);
00966 }
00967 
00968 gboolean
00969 qof_scan_date (const char *buff, int *day, int *month, int *year)
00970 {
00971   return qof_scan_date_internal(buff, day, month, year, dateFormat);
00972 }
00973 
00974 gboolean
00975 qof_scan_date_secs (const gchar * buff, time_t * secs)
00976 {
00977   gboolean rc;
00978   int day, month, year;
00979   
00980   rc = qof_scan_date_internal(buff, &day, &month, &year, dateFormat);
00981   if (secs) *secs = xaccDMYToSec (day, month, year);
00982 
00983   return rc;
00984 }
00985 
00986 Timespec
00987 gnc_dmy2timespec (gint day, gint month, gint year)
00988 {
00989     Timespec ts;
00990     QofTime *qt;
00991     QofDate *qd;
00992 
00993     if (!g_date_valid_dmy (day, month, year))
00994         return null_timespec ();
00995     qd = qof_date_new ();
00996     qd->qd_mday = day;
00997     qd->qd_mon  = month;
00998     qd->qd_year = year;
00999     qof_date_valid (qd);
01000     qt = qof_date_to_qtime (qd);
01001     ts = qof_time_to_Timespec (qt);
01002     qof_time_free (qt);
01003     qof_date_free (qd);
01004     return ts;
01005 }
01006 
01007 Timespec
01008 gnc_dmy2timespec_end (gint day, gint month, gint year)
01009 {
01010     Timespec ts;
01011     QofTime *qt;
01012     QofDate *qd;
01013 
01014     if (!g_date_valid_dmy (day, month, year))
01015         return null_timespec ();
01016     qd = qof_date_new ();
01017     qd->qd_mday = day;
01018     qd->qd_mon  = month;
01019     qd->qd_year = year;
01020     qof_date_set_day_end (qd);
01021     qt = qof_date_to_qtime (qd);
01022     ts = qof_time_to_Timespec (qt);
01023     qof_time_free (qt);
01024     qof_date_free (qd);
01025     return ts;
01026 }
01027 
01028 Timespec
01029 gnc_iso8601_to_timespec_gmt (const gchar * str)
01030 {
01031     gchar buf[4];
01032     gchar *dupe;
01033     Timespec ts;
01034     struct tm stm;
01035     glong nsec = 0;
01036 
01037     ts.tv_sec = 0;
01038     ts.tv_nsec = 0;
01039     if (!str)
01040         return ts;
01041     dupe = g_strdup (str);
01042     stm.tm_year = atoi (str) - 1900;
01043     str = strchr (str, '-');
01044     if (str)
01045     {
01046         str++;
01047     }
01048     else
01049     {
01050         return ts;
01051     }
01052     stm.tm_mon = atoi (str) - 1;
01053     str = strchr (str, '-');
01054     if (str)
01055     {
01056         str++;
01057     }
01058     else
01059     {
01060         return ts;
01061     }
01062     stm.tm_mday = atoi (str);
01063 
01064     str = strchr (str, ' ');
01065     if (str)
01066     {
01067         str++;
01068     }
01069     else
01070     {
01071         return ts;
01072     }
01073     stm.tm_hour = atoi (str);
01074     str = strchr (str, ':');
01075     if (str)
01076     {
01077         str++;
01078     }
01079     else
01080     {
01081         return ts;
01082     }
01083     stm.tm_min = atoi (str);
01084     str = strchr (str, ':');
01085     if (str)
01086     {
01087         str++;
01088     }
01089     else
01090     {
01091         return ts;
01092     }
01093     stm.tm_sec = atoi (str);
01094 
01095     if (strchr (str, '.'))
01096     {
01097         gint decimals, i, multiplier = 1000000000;
01098         str = strchr (str, '.') + 1;
01099         decimals = strcspn (str, "+- ");
01100         for (i = 0; i < decimals; i++)
01101             multiplier /= 10;
01102         nsec = atoi (str) * multiplier;
01103     }
01104     stm.tm_isdst = -1;
01105 
01106     str += strcspn (str, "+-");
01107     if (str)
01108     {
01109         buf[0] = str[0];
01110         buf[1] = str[1];
01111         buf[2] = str[2];
01112         buf[3] = 0;
01113         stm.tm_hour -= atoi (buf);
01114 
01115         str += 3;
01116         if ('.' == *str)
01117             str++;
01118         if (isdigit ((guchar) * str) && isdigit ((guchar) * (str + 1)))
01119         {
01120             gint cyn;
01121             if ('+' == buf[0])
01122             {
01123                 cyn = -1;
01124             }
01125             else
01126             {
01127                 cyn = +1;
01128             }
01129             buf[0] = str[0];
01130             buf[1] = str[1];
01131             buf[2] = str[2];
01132             buf[3] = 0;
01133             stm.tm_min += cyn * atoi (buf);
01134         }
01135     }
01136 
01137     {
01138         struct tm tmp_tm;
01139         struct tm tm;
01140         glong tz;
01141         gint tz_hour;
01142         time_t secs;
01143 
01144         tmp_tm = stm;
01145         tmp_tm.tm_isdst = -1;
01146 
01147         secs = mktime (&tmp_tm);
01148 
01149         if (secs < 0)
01150         {
01151             PWARN (" mktime failed to handle daylight saving: "
01152                 "tm_hour=%d tm_year=%d tm_min=%d tm_sec=%d tm_isdst=%d for string=%s",
01153                 stm.tm_hour, stm.tm_year, stm.tm_min,
01154                 stm.tm_sec, stm.tm_isdst, dupe);
01155             tmp_tm.tm_hour++;
01156             secs = mktime (&tmp_tm);
01157             if (secs < 0)
01158             {
01159                 tmp_tm.tm_hour -= 2;
01160                 secs = mktime (&tmp_tm);
01161             }
01162             if (secs < 0)
01163             {
01164                 PERR (" unable to recover from buggy mktime ");
01165                 g_free (dupe);
01166                 return ts;
01167             }
01168         }
01169 
01170         tm = *localtime_r (&secs, &tm);
01171 
01172         tz = gnc_timezone (&tmp_tm);
01173 
01174         tz_hour = tz / 3600;
01175         stm.tm_hour -= tz_hour;
01176         stm.tm_min -= (tz % 3600) / 60;
01177         stm.tm_isdst = tmp_tm.tm_isdst;
01178         ts.tv_sec = mktime (&stm);
01179         /* unreachable code */
01180 /*      if (ts.tv_sec < 0)
01181         {
01182             PWARN (" mktime failed to adjust calculated time:"
01183                 " tm_hour=%d tm_year=%d tm_min=%d tm_sec=%d tm_isdst=%d",
01184                 stm.tm_hour, stm.tm_year, stm.tm_min,
01185                 stm.tm_sec, stm.tm_isdst);
01186             ts.tv_sec = secs - tz;
01187         }*/
01188         ts.tv_nsec = nsec;
01189     }
01190     g_free (dupe);
01191     return ts;
01192 }
01193 
01194 gchar *
01195 gnc_timespec_to_iso8601_buff (Timespec ts, gchar * buff)
01196 {
01197     gint len, tz_hour, tz_min;
01198     glong secs;
01199     gchar cyn;
01200     time_t tmp;
01201     struct tm parsed;
01202 
01203     tmp = ts.tv_sec;
01204     localtime_r (&tmp, &parsed);
01205 
01206     secs = gnc_timezone (&parsed);
01207     tz_hour = secs / 3600;
01208     tz_min = (secs % 3600) / 60;
01209 
01210     cyn = '-';
01211     if (0 > tz_hour)
01212     {
01213         cyn = '+';
01214         tz_hour = -tz_hour;
01215     }
01216 
01217     len = sprintf (buff, "%4d-%02d-%02d %02d:%02d:%02d.%06ld %c%02d%02d",
01218         parsed.tm_year + 1900,
01219         parsed.tm_mon + 1,
01220         parsed.tm_mday,
01221         parsed.tm_hour,
01222         parsed.tm_min,
01223         parsed.tm_sec, ts.tv_nsec / 1000, cyn, tz_hour, tz_min);
01224 
01225     buff += len;
01226     return buff;
01227 }
01228 
01229 void
01230 gnc_timespec2dmy (Timespec ts, gint * day, gint * month, gint * year)
01231 {
01232     QofTime *time;
01233     QofDate *qd;
01234 
01235     time = timespecToQofTime (ts);
01236     qd = qof_date_from_qtime (time);
01237     qof_time_free (time);
01238     if (day)
01239         *day = qd->qd_mday;
01240     if (month)
01241         *month = qd->qd_mon;
01242     if ((year) && (qd->qd_year < 0) && (qd->qd_year > G_MAXINT))
01243         *year = (gint)qd->qd_year;
01244 }
01245 
01246 glong
01247 gnc_timezone (struct tm *tm)
01248 {
01249     g_return_val_if_fail (tm != NULL, 0);
01250 
01251 #ifdef HAVE_STRUCT_TM_GMTOFF
01252     return -(tm->tm_gmtoff);
01253 #else
01254     return (glong) (timezone - (tm->tm_isdst > 0 ? 3600 : 0));
01255 #endif
01256 }
01257 
01258 Timespec
01259 qof_instance_get_last_update (QofInstance * inst)
01260 {
01261     Timespec ts;
01262     ts = inst->last_update;
01263     inst->update_time = timespecToQofTime (ts);
01264     return ts;
01265 }
01266 
01267 void
01268 qof_instance_set_last_update (QofInstance * inst, Timespec ts)
01269 {
01270     QofTime *time;
01271     g_return_if_fail (inst);
01272     inst->last_update = ts;
01273     time = timespecToQofTime (ts);
01274     qof_instance_set_update_time (inst, time);
01275 }
01276 time_t 
01277 xaccDMYToSec (int day, int month, int year)
01278 {
01279   struct tm stm;
01280   time_t secs;
01281 
01282   stm.tm_year = year - 1900;
01283   stm.tm_mon = month - 1;
01284   stm.tm_mday = day;
01285   gnc_tm_set_day_start(&stm);
01286 
01287   secs = mktime (&stm);
01288 
01289   return secs;
01290 }
01291 void date_add_months (struct tm *tm, int months, gboolean track_last_day)
01292 {
01293   gboolean was_last_day;
01294   int new_last_mday;
01295 
01296   was_last_day = date_is_last_mday(tm);
01297 
01298   tm->tm_mon += months;
01299   while (tm->tm_mon > 11) {
01300     tm->tm_mon -= 12;
01301     tm->tm_year++;
01302   }
01303 
01304   if (!track_last_day)
01305     return;
01306 
01307   new_last_mday = date_get_last_mday(tm);
01308   if (was_last_day || (tm->tm_mday > new_last_mday))
01309     tm->tm_mday = new_last_mday;
01310 }
01311 char dateSeparator (void)
01312 {
01313     return qof_date_format_get_date_separator (qof_date_format_get_current());
01314 }
01315 
01316 const char*
01317 gnc_date_dateformat_to_string(QofDateFormat format)
01318 {
01319   switch (format) {
01320   case QOF_DATE_FORMAT_US:
01321     return "us";
01322   case QOF_DATE_FORMAT_UK:
01323     return "uk";
01324   case QOF_DATE_FORMAT_CE:
01325     return "ce";
01326   case QOF_DATE_FORMAT_ISO:
01327     return "iso";
01328   case QOF_DATE_FORMAT_UTC:
01329    return "utc";
01330   case QOF_DATE_FORMAT_LOCALE:
01331     return "locale";
01332   case QOF_DATE_FORMAT_CUSTOM:
01333     return "custom";
01334   default:
01335     return NULL;    
01336   }
01337 }
01338 const char*
01339 gnc_date_monthformat_to_string(GNCDateMonthFormat format)
01340 {
01341   switch (format) {
01342   case GNCDATE_MONTH_NUMBER:
01343     return "number";
01344   case GNCDATE_MONTH_ABBREV:
01345     return "abbrev";
01346   case GNCDATE_MONTH_NAME:
01347     return "name";
01348   default:
01349     return NULL;
01350   }
01351 }
01352 gboolean
01353 qof_date_add_days(Timespec *ts, gint days)
01354 {
01355     struct tm tm;
01356     time_t    tt;
01357 
01358     g_return_val_if_fail(ts, FALSE);
01359     tt = timespecToTime_t(*ts);
01360 #ifdef HAVE_GMTIME_R
01361     tm = *gmtime_r(&tt, &tm);
01362 #else
01363     tm = *gmtime(&tt);
01364 #endif
01365     tm.tm_mday += days;
01366     tt = mktime(&tm);
01367     if(tt < 0) { return FALSE; }
01368     timespecFromTime_t(ts, tt);
01369     return TRUE;
01370 }
01371 
01372 gboolean
01373 qof_date_add_months(Timespec *ts, gint months, gboolean track_last_day)
01374 {
01375     struct tm tm;
01376     time_t    tt;
01377     gint new_last_mday;
01378     gboolean was_last_day;
01379 
01380     g_return_val_if_fail(ts, FALSE);
01381     tt = timespecToTime_t(*ts);
01382 #ifdef HAVE_GMTIME_R
01383     tm = *gmtime_r(&tt, &tm);
01384 #else
01385     tm = *gmtime(&tt);
01386 #endif
01387     was_last_day = date_is_last_mday(&tm);
01388     tm.tm_mon += months;
01389     while (tm.tm_mon > 11) {
01390         tm.tm_mon -= 12;
01391         tm.tm_year++;
01392     }
01393     if (track_last_day) {
01394         new_last_mday = date_get_last_mday(&tm);
01395         if (was_last_day || (tm.tm_mday > new_last_mday)) {
01396             tm.tm_mday = new_last_mday;
01397         }
01398     }
01399     tt = mktime(&tm);
01400     if(tt < 0) { return FALSE; }
01401     timespecFromTime_t(ts, tt);
01402     return TRUE;
01403 }
01404 
01405 QofDateFormat qof_date_format_get (void)
01406 {
01407   return qof_date_format_get_current ();
01408 }
01409 
01410 const gchar *qof_date_format_get_string(QofDateFormat df)
01411 {
01412   switch(df) {
01413    case QOF_DATE_FORMAT_US:
01414     return "%m/%d/%y";
01415    case QOF_DATE_FORMAT_UK:
01416     return "%d/%m/%y";
01417    case QOF_DATE_FORMAT_CE:
01418     return "%d.%m.%y";
01419    case QOF_DATE_FORMAT_UTC:
01420     return "%Y-%m-%dT%H:%M:%SZ";
01421    case QOF_DATE_FORMAT_ISO:
01422     return "%y-%m-%d";
01423    case QOF_DATE_FORMAT_LOCALE:
01424    default:
01425     return QOF_D_FMT;
01426   };
01427 }
01428 
01429 void qof_date_format_set(QofDateFormat df)
01430 {
01431     if(!qof_date_format_set_current (df))
01432         PERR (" unable to set current format, %d", df);
01433 }
01434 
01435 const gchar *qof_date_text_format_get_string(QofDateFormat df)
01436 {
01437     return qof_date_format_get_format (df);
01438 }
01439 
01440 char *
01441 xaccDateUtilGetStampNow (void)
01442 {
01443    return qof_time_stamp_now ();
01444 }
01445 
01446 void
01447 kvp_frame_set_timespec (KvpFrame * frame, const char *path, Timespec ts)
01448 {
01449     KvpValue *value;
01450     value = kvp_value_new_timespec (ts);
01451     frame = kvp_frame_set_value_nc (frame, path, value);
01452     if (!frame)
01453         kvp_value_delete (value);
01454 }
01455 
01456 void
01457 kvp_frame_add_timespec (KvpFrame * frame, const char *path, Timespec ts)
01458 {
01459     KvpValue *value;
01460     value = kvp_value_new_timespec (ts);
01461     frame = kvp_frame_add_value_nc (frame, path, value);
01462     if (!frame)
01463         kvp_value_delete (value);
01464 }
01465 
01466 Timespec
01467 kvp_frame_get_timespec (const KvpFrame * frame, 
01468     const char *path __attribute__ ((unused)))
01469 {
01470     QofTime *qt;
01471     Timespec ts;
01472     char *key;
01473 
01474     key = NULL;
01475     ts.tv_sec = 0;
01476     ts.tv_nsec = 0;
01477     qt = kvp_value_get_time (kvp_frame_get_slot (frame, key));
01478     if(!qt)
01479         return ts;
01480     return qof_time_to_Timespec (qt);
01481 }
01482 
01483 KvpValue *
01484 kvp_value_new_timespec (Timespec value)
01485 {
01486     QofTime *qt;
01487     KvpValue *retval;
01488 
01489     qt = timespecToQofTime (value);
01490     retval = kvp_value_new_time (qt);
01491     return retval;
01492 }
01493 
01494 Timespec
01495 kvp_value_get_timespec (const KvpValue * value)
01496 {
01497     Timespec ts;
01498     QofTime *qt;
01499     ts.tv_sec = 0;
01500     ts.tv_nsec = 0;
01501     if (!value)
01502         return ts;
01503     qt = kvp_value_get_time (value);
01504     ts = qof_time_to_Timespec (qt);
01505     return ts;
01506 }
01507 #define NUM_CLOCKS 10
01508 static struct timeval qof_clock[NUM_CLOCKS] = {
01509     {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0},
01510     {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0},
01511 };
01512 
01513 static struct timeval qof_clock_total[NUM_CLOCKS] = {
01514     {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0},
01515     {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0},
01516 };
01517 
01518 void
01519 qof_start_clock (gint clockno, 
01520     QofLogModule log_module __attribute__ ((unused)),
01521     QofLogLevel log_level __attribute__ ((unused)), 
01522     const gchar * function_name, const gchar * format, ...)
01523 {
01524     va_list ap;
01525 
01526     if ((0 > clockno) || (NUM_CLOCKS <= clockno))
01527         return;
01528 #ifdef HAVE_GETTIMEOFDAY
01529     gettimeofday (&qof_clock[clockno], NULL);
01530 #else
01531     time (&(qof_clock[clockno].tv_sec));
01532     qof_clock[clockno].tv_usec = 0;
01533 #endif
01534 
01535     if (!fout)
01536         qof_log_init ();
01537 
01538     fprintf (fout, "Clock %d Start: %s: ",
01539         clockno, qof_log_prettify (function_name));
01540 
01541     va_start (ap, format);
01542 
01543     vfprintf (fout, format, ap);
01544 
01545     va_end (ap);
01546 
01547     fprintf (fout, "\n");
01548     fflush (fout);
01549 }
01550 
01551 void
01552 qof_report_clock (gint clockno, 
01553     QofLogModule log_module __attribute__ ((unused)),
01554     QofLogLevel log_level __attribute__ ((unused)), 
01555     const gchar * function_name, const gchar * format, ...)
01556 {
01557     struct timeval now;
01558     va_list ap;
01559 
01560     if ((0 > clockno) || (NUM_CLOCKS <= clockno))
01561         return;
01562 #ifdef HAVE_GETTIMEOFDAY
01563     gettimeofday (&now, NULL);
01564 #else
01565     time (&(now.tv_sec));
01566     now.tv_usec = 0;
01567 #endif
01568 
01569     if (now.tv_usec < qof_clock[clockno].tv_usec)
01570     {
01571         now.tv_sec--;
01572         now.tv_usec += 1000000;
01573     }
01574     now.tv_sec -= qof_clock[clockno].tv_sec;
01575     now.tv_usec -= qof_clock[clockno].tv_usec;
01576 
01577     qof_clock_total[clockno].tv_sec += now.tv_sec;
01578     qof_clock_total[clockno].tv_usec += now.tv_usec;
01579 
01580     if (!fout)
01581         qof_log_init ();
01582 
01583     fprintf (fout, "Clock %d Elapsed: %ld.%06lds %s: ",
01584         clockno, (long int) now.tv_sec, (long int) now.tv_usec,
01585         qof_log_prettify (function_name));
01586 
01587     va_start (ap, format);
01588 
01589     vfprintf (fout, format, ap);
01590 
01591     va_end (ap);
01592 
01593     fprintf (fout, "\n");
01594     fflush (fout);
01595 }
01596 
01597 void
01598 qof_report_clock_total (gint clockno,
01599     QofLogModule log_module __attribute__ ((unused)), 
01600     QofLogLevel log_level __attribute__ ((unused)),
01601     const gchar * function_name, const gchar * format, ...)
01602 {
01603     va_list ap;
01604 
01605     if ((0 > clockno) || (NUM_CLOCKS <= clockno))
01606         return;
01607 
01608     while (qof_clock_total[clockno].tv_usec >= 1000000)
01609     {
01610         qof_clock_total[clockno].tv_sec++;
01611         qof_clock_total[clockno].tv_usec -= 1000000;
01612     }
01613 
01614     if (!fout)
01615         qof_log_init ();
01616 
01617     fprintf (fout, "Clock %d Total Elapsed: %ld.%06lds  %s: ",
01618         clockno,
01619         (long int) qof_clock_total[clockno].tv_sec,
01620         (long int) qof_clock_total[clockno].tv_usec,
01621         qof_log_prettify (function_name));
01622 
01623     va_start (ap, format);
01624 
01625     vfprintf (fout, format, ap);
01626 
01627     va_end (ap);
01628 
01629     fprintf (fout, "\n");
01630     fflush (fout);
01631 }
01632 static QofSession *current_session = NULL;
01633 
01634 QofSession *
01635 qof_session_get_current_session (void)
01636 {
01637     if (!current_session)
01638     {
01639         qof_event_suspend ();
01640         current_session = qof_session_new ();
01641         qof_event_resume ();
01642     }
01643 
01644     return current_session;
01645 }
01646 
01647 void
01648 qof_session_set_current_session (QofSession * session)
01649 {
01650     current_session = session;
01651 }
01652 
01653 void
01654 qof_session_clear_current_session (void)
01655 {
01656     current_session = NULL;
01657 }
01658 
01659 gboolean
01660 gnc_strisnum (const guchar * s)
01661 {
01662     return qof_util_string_isnum (s);
01663 }
01664 
01665 KvpFrame *
01666 gnc_kvp_bag_add (KvpFrame * pwd, const char *path,
01667     time_t secs, const char *first_name, ...)
01668 {
01669     QofTime *qt;
01670     KvpFrame *cwd;
01671     va_list ap;
01672 
01673     qt = qof_time_from_time_t (secs, 0);
01674     va_start (ap, first_name);
01675     cwd = qof_kvp_bag_add(pwd, path, qt, first_name, ap);
01676     va_end (ap);
01677     return cwd;
01678 }
01679 
01680 KvpFrame *
01681 gnc_kvp_bag_find_by_guid (KvpFrame * root, const gchar *path,
01682     const gchar *guid_name, GUID * desired_guid)
01683 {
01684     return qof_kvp_bag_find_by_guid (root, path, 
01685         guid_name, desired_guid);
01686 }
01687 
01688 void
01689 gnc_kvp_bag_remove_frame (KvpFrame * root, const char *path, 
01690     KvpFrame * fr)
01691 {
01692     qof_kvp_bag_remove_frame (root, path, fr);
01693 }
01694 
01695 void
01696 gnc_kvp_bag_merge (KvpFrame * kvp_into, const char *intopath,
01697     KvpFrame * kvp_from, const char *frompath)
01698 {
01699     qof_kvp_bag_merge (kvp_into, intopath, kvp_from, frompath);
01700 }
01701 
01702 gboolean qof_begin_edit (QofInstance * inst)
01703 {
01704     return qof_util_param_edit (inst, NULL);
01705 }
01706 
01707 gboolean
01708 qof_commit_edit (QofInstance * inst)
01709 {
01710     QofBackend *be;
01711 
01712     if (!inst)
01713         return FALSE;
01714     (inst->editlevel)--;
01715     if (0 < inst->editlevel)
01716         return FALSE;
01717     if ((-1 == inst->editlevel) && inst->dirty)
01718     {
01719         be = qof_book_get_backend ((inst)->book);
01720         if (be && qof_backend_begin_exists (be))
01721             qof_backend_run_begin (be, inst);
01722         inst->editlevel = 0;
01723     }
01724     if (0 > inst->editlevel)
01725         inst->editlevel = 0;
01726     return TRUE;
01727 }
01728 
01729 
01730 gboolean
01731 qof_commit_edit_part2 (QofInstance * inst,
01732     void (*on_error) (QofInstance *, QofBackendError),
01733     void (*on_done) (QofInstance *), void (*on_free) (QofInstance *))
01734 {
01735     QofBackend *be;
01736 
01737     /* See if there's a backend.  If there is, invoke it. */
01738     be = qof_book_get_backend (inst->book);
01739     if (be && qof_backend_commit_exists (be))
01740     {
01741         QofBackendError errcode;
01742 
01743         do
01744         {
01745             errcode = qof_backend_get_error (be);
01746         }
01747         while (ERR_BACKEND_NO_ERR != errcode);
01748 
01749         qof_backend_run_commit (be, inst);
01750         errcode = qof_backend_get_error (be);
01751         if (ERR_BACKEND_NO_ERR != errcode)
01752         {
01753             inst->do_free = FALSE;
01754 
01755             qof_backend_set_error (be, errcode);
01756             if (on_error)
01757                 on_error (inst, errcode);
01758             return FALSE;
01759         }
01760         inst->dirty = FALSE;
01761     }
01762     if (inst->do_free)
01763     {
01764         if (on_free)
01765             on_free (inst);
01766         return TRUE;
01767     }
01768     if (on_done)
01769         on_done (inst);
01770     return TRUE;
01771 }
01772 
01773 gchar *
01774 qof_util_param_as_string (QofEntity * ent, QofParam * param)
01775 {
01776     return qof_util_param_to_string (ent, param);
01777 }
01778 gnc_numeric 
01779 double_to_gnc_numeric (double in, gint64 denom, gint how)
01780 {
01781     return qof_numeric_from_double (in, denom, how);
01782 }
01783 gboolean 
01784 string_to_gnc_numeric (const gchar * str, gnc_numeric * n)
01785 {
01786     return qof_numeric_from_string (str, n);
01787 }
01788 gnc_numeric 
01789 gnc_numeric_error (GNCNumericErrorCode error_code)
01790 {
01791     return qof_numeric_error (error_code);
01792 }
01793 gdouble 
01794 gnc_numeric_to_double (gnc_numeric in)
01795 {
01796     return qof_numeric_to_double (in);
01797 }
01798 gchar *
01799 gnc_numeric_to_string (gnc_numeric n)
01800 {
01801     return qof_numeric_to_string (n);
01802 }
01803 gchar *
01804 gnc_num_dbg_to_string (gnc_numeric n)
01805 {
01806     return qof_numeric_dbg_to_string (n);
01807 }
01808 GNCNumericErrorCode 
01809 gnc_numeric_check (gnc_numeric a)
01810 {
01811     return qof_numeric_check (a);
01812 }
01813 gint 
01814 gnc_numeric_compare (gnc_numeric a, gnc_numeric b)
01815 {
01816     return qof_numeric_compare (a, b);
01817 }
01818 gboolean 
01819 gnc_numeric_zero_p (gnc_numeric a)
01820 {
01821     return qof_numeric_zero_p (a);
01822 }
01823 gboolean 
01824 gnc_numeric_negative_p (gnc_numeric a)
01825 {
01826     return qof_numeric_negative_p (a);
01827 }
01828 gboolean 
01829 gnc_numeric_positive_p (gnc_numeric a)
01830 {
01831     return qof_numeric_positive_p (a);
01832 }
01833 gboolean 
01834 gnc_numeric_eq (gnc_numeric a, gnc_numeric b)
01835 {
01836     return qof_numeric_eq (a, b);
01837 }
01838 gboolean 
01839 gnc_numeric_equal (gnc_numeric a, gnc_numeric b)
01840 {
01841     return qof_numeric_equal (a, b);
01842 }
01843 gint 
01844 gnc_numeric_same (gnc_numeric a, gnc_numeric b, 
01845                   gint64 denom, gint how)
01846 {
01847     return qof_numeric_same (a, b, denom, how);
01848 }
01849 gnc_numeric 
01850 gnc_numeric_add (gnc_numeric a, gnc_numeric b,
01851                  gint64 denom, gint how)
01852 {
01853     return qof_numeric_add (a, b, denom, how);
01854 }
01855 gnc_numeric 
01856 gnc_numeric_sub (gnc_numeric a, gnc_numeric b,
01857                  gint64 denom, gint how)
01858 {
01859     return qof_numeric_sub (a, b, denom, how);
01860 }
01861 gnc_numeric 
01862 gnc_numeric_mul (gnc_numeric a, gnc_numeric b,
01863                  gint64 denom, gint how)
01864 {
01865     return qof_numeric_mul (a, b, denom, how);
01866 }
01867 gnc_numeric 
01868 gnc_numeric_div (gnc_numeric x, gnc_numeric y,
01869                  gint64 denom, gint how)
01870 {
01871     return qof_numeric_div (x, y, denom, how);
01872 }
01873 gnc_numeric gnc_numeric_neg (gnc_numeric a)
01874 {
01875     return qof_numeric_neg (a);
01876 }
01877 gnc_numeric gnc_numeric_abs (gnc_numeric a)
01878 {
01879     return qof_numeric_abs (a);
01880 }
01881 gnc_numeric 
01882 gnc_numeric_add_with_error (gnc_numeric a, gnc_numeric b,
01883                             gint64 denom, gint how,
01884                             gnc_numeric * error)
01885 {
01886     return qof_numeric_add_with_error (a, b, denom, how, error);
01887 }
01888 gnc_numeric 
01889 gnc_numeric_sub_with_error (gnc_numeric a, gnc_numeric b,
01890                             gint64 denom, gint how,
01891                             gnc_numeric * error)
01892 {
01893     return qof_numeric_sub_with_error (a, b, denom, how, error);
01894 }
01895 gnc_numeric 
01896 gnc_numeric_mul_with_error (gnc_numeric a, gnc_numeric b,
01897                             gint64 denom, gint how,
01898                             gnc_numeric * error)
01899 {
01900     return qof_numeric_mul_with_error (a, b, denom, how, error);
01901 }
01902 gnc_numeric 
01903 gnc_numeric_div_with_error (gnc_numeric a, gnc_numeric b,
01904                             gint64 denom, gint how,
01905                             gnc_numeric * error)
01906 {
01907     return qof_numeric_div_with_error (a, b, denom, how, error);
01908 }
01909 
01910 gnc_numeric 
01911 gnc_numeric_convert (gnc_numeric in, gint64 denom, gint how)
01912 {
01913     return qof_numeric_convert (in, denom, how);
01914 }
01915 
01916 gnc_numeric gnc_numeric_reduce (gnc_numeric in)
01917 {
01918     return qof_numeric_reduce (in);
01919 }
01920 
01921 /* ==================================================================== */
01922 #endif /* QOF_DISABLE_DEPRECATED */

Generated on Fri Sep 15 14:24:56 2006 for QOF by  doxygen 1.4.7