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

Generated on Thu Jan 31 22:50:25 2008 for QOF by  doxygen 1.5.4