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, QofLogModule b, gncLogLevel c, const char *d,
00094     const char *e, ...)
00095 {
00096 }
00097 void
00098 gnc_report_clock (int a, QofLogModule b, gncLogLevel c, const char *d,
00099     const char *e, ...)
00100 {
00101 }
00102 void
00103 gnc_report_clock_total (int a, QofLogModule b, gncLogLevel c,
00104     const char *d, const char *e, ...)
00105 {
00106 }
00107 
00108 gboolean
00109 gnc_should_log (QofLogModule log_module, gncLogLevel log_level)
00110 {
00111     return qof_log_check (log_module, log_level);
00112 }
00113 
00114 gint
00115 gnc_engine_register_event_handler (GNCEngineEventHandler handler,
00116     gpointer user_data)
00117 {
00118     return qof_event_register_old_handler (handler, user_data);
00119 }
00120 
00121 void
00122 gnc_engine_unregister_event_handler (gint handler_id)
00123 {
00124     qof_event_unregister_handler (handler_id);
00125 }
00126 
00127 void
00128 gnc_engine_suspend_events (void)
00129 {
00130     qof_event_suspend ();
00131 }
00132 
00133 void
00134 gnc_engine_resume_events (void)
00135 {
00136     qof_event_resume ();
00137 }
00138 
00139 void
00140 gnc_engine_gen_event (QofEntity * entity, GNCEngineEventType event_type)
00141 {
00142     qof_event_gen (entity, event_type, NULL);
00143 }
00144 
00145 QofBookMergeData *
00146 qof_book_mergeInit (QofBook * importBook, QofBook * targetBook)
00147 {
00148     return qof_book_merge_init (importBook, targetBook);
00149 }
00150 
00151 QofBookMergeData *
00152 qof_book_mergeUpdateResult (QofBookMergeData * mergeData,
00153     QofBookMergeResult tag)
00154 {
00155     return qof_book_merge_update_result (mergeData, tag);
00156 }
00157 
00158 gint
00159 qof_book_mergeCommit (QofBookMergeData * mergeData)
00160 {
00161     return qof_book_merge_commit (mergeData);
00162 }
00163 
00164 void
00165 qof_book_mergeRuleForeach (QofBookMergeData * mergeData,
00166     QofBookMergeRuleForeachCB cb, QofBookMergeResult mergeResult)
00167 {
00168     qof_book_merge_rule_foreach (mergeData, cb, mergeResult);
00169 }
00170 
00171 gpointer
00172 gnc_string_cache_insert (gconstpointer key)
00173 {
00174     return (gpointer) qof_util_string_cache_insert (key);
00175 }
00176 
00177 gchar *
00178 gnc_stpcpy (gchar * dest, const gchar * src)
00179 {
00180     return g_stpcpy (dest, src);
00181 }
00182 
00183 GCache *
00184 gnc_engine_get_string_cache (void)
00185 {
00186     return qof_util_get_string_cache ();
00187 }
00188 
00189 void
00190 gnc_engine_string_cache_destroy (void)
00191 {
00192     qof_util_string_cache_destroy ();
00193 }
00194 
00195 void
00196 gnc_string_cache_remove (gconstpointer key)
00197 {
00198     qof_util_string_cache_remove (key);
00199 }
00200 
00201 gboolean
00202 gnc_date_string_to_dateformat (const gchar * format_string,
00203     QofDateFormat * format)
00204 {
00205     QofDateFormat df;
00206 
00207     df = qof_date_format_from_name (format_string);
00208     if (df < 0)
00209         return TRUE;
00210     *format = df;
00211     return FALSE;
00212 }
00213 
00214 gboolean
00215 gnc_date_string_to_monthformat (const gchar * format_string,
00216     GNCDateMonthFormat * format)
00217 {
00218     if (!format_string)
00219         return TRUE;
00220 
00221     if (!strcmp (format_string, "number"))
00222         *format = GNCDATE_MONTH_NUMBER;
00223     else if (!strcmp (format_string, "abbrev"))
00224         *format = GNCDATE_MONTH_ABBREV;
00225     else if (!strcmp (format_string, "name"))
00226         *format = GNCDATE_MONTH_NAME;
00227     else
00228         return TRUE;
00229 
00230     return FALSE;
00231 }
00232 static inline QofTime *
00233 timespecToQofTime (Timespec ts)
00234 {
00235     QofTime *time;
00236 
00237     time = qof_time_new ();
00238     qof_time_set_secs (time, ts.tv_sec);
00239     qof_time_set_nanosecs (time, ts.tv_nsec);
00240     return time;
00241 }
00242 static inline Timespec
00243 qof_time_to_Timespec (QofTime * time)
00244 {
00245     Timespec ts;
00246 
00247     ts.tv_sec = qof_time_get_secs (time);
00248     ts.tv_nsec = qof_time_get_nanosecs (time);
00249     return ts;
00250 }
00251 static inline Timespec
00252 null_timespec (void)
00253 {
00254     Timespec ts;
00255 
00256     ts.tv_sec = 0;
00257     ts.tv_nsec = 0;
00258     return ts;
00259 }
00260 
00261 gboolean
00262 timespec_equal (const Timespec * ta, const Timespec * tb)
00263 {
00264     QofTime *qta, *qtb;
00265     gboolean retval;
00266 
00267     qta = timespecToQofTime (*ta);
00268     qtb = timespecToQofTime (*tb);
00269     retval = qof_time_equal (qta, qtb);
00270     qof_time_free (qta);
00271     qof_time_free (qtb);
00272     return retval;
00273 }
00274 
00275 gint
00276 timespec_cmp (const Timespec * ta, const Timespec * tb)
00277 {
00278     QofTime *qta, *qtb;
00279     gint retval;
00280 
00281     qta = timespecToQofTime (*ta);
00282     qtb = timespecToQofTime (*tb);
00283     retval = qof_time_cmp (qta, qtb);
00284     qof_time_free (qta);
00285     qof_time_free (qtb);
00286     return retval;
00287 }
00288 
00289 void
00290 timespecFromTime_t (Timespec * ts, time_t t)
00291 {
00292     QofTime *time;
00293 
00294     time = qof_time_new ();
00295     time = qof_time_from_time_t (t, 0);
00296     *ts = qof_time_to_Timespec (time);
00297     qof_time_free (time);
00298 }
00299 
00300 time_t
00301 timespecToTime_t (Timespec ts)
00302 {
00303     return ts.tv_sec;
00304 }
00305 
00306 Timespec
00307 timespec_diff (const Timespec * ta, const Timespec * tb)
00308 {
00309     Timespec ts;
00310     QofTime *qta, *qtb, *qt;
00311 
00312     qta = timespecToQofTime (*ta);
00313     qtb = timespecToQofTime (*tb);
00314     qt = qof_time_diff (qta, qtb);
00315     ts = qof_time_to_Timespec (qt);
00316     qof_time_free (qta);
00317     qof_time_free (qtb);
00318     qof_time_free (qt);
00319     return ts;
00320 }
00321 
00322 Timespec
00323 timespec_abs (const Timespec * t)
00324 {
00325     Timespec ts;
00326     QofTime *qt = timespecToQofTime (*t);
00327     qof_time_abs (qt);
00328     ts = qof_time_to_Timespec (qt);
00329     qof_time_free (qt);
00330     return ts;
00331 }
00332 
00333 Timespec
00334 timespecCanonicalDayTime (Timespec t)
00335 {
00336     struct tm tm, *result;
00337     Timespec retval;
00338 
00339     time_t t_secs = t.tv_sec + (t.tv_nsec / QOF_NSECS);
00340     result = localtime(&t_secs);
00341     tm = *result;
00342     gnc_tm_set_day_middle(&tm);
00343     retval.tv_sec = mktime(&tm);
00344     retval.tv_nsec = 0;
00345     return retval;
00346 }
00347 
00348 time_t
00349 qof_date_dmy_to_sec (gint day, gint month, gint year)
00350 {
00351     QofTime *qt;
00352     QofDate *qd;
00353     time_t retval;
00354 
00355     qd = qof_date_new ();
00356     qd->qd_mday = day;
00357     qd->qd_mon  = month;
00358     qd->qd_year = year;
00359     qt = qof_date_to_qtime (qd);
00360     retval = qof_time_get_secs (qt);
00361     qof_time_free (qt);
00362     qof_date_free (qd);
00363     return retval;
00364 }
00365 
00366 size_t
00367 qof_print_hours_elapsed_buff (char *buff, size_t len, int secs,
00368     gboolean show_secs)
00369 {
00370     size_t flen;
00371     if (0 <= secs)
00372     {
00373         if (show_secs)
00374         {
00375             flen = g_snprintf (buff, len,
00376                 "%02d:%02d:%02d", (int) (secs / 3600),
00377                 (int) ((secs % 3600) / 60), (int) (secs % 60));
00378         }
00379         else
00380         {
00381             flen = g_snprintf (buff, len,
00382                 "%02d:%02d", (int) (secs / 3600),
00383                 (int) ((secs % 3600) / 60));
00384         }
00385     }
00386     else
00387     {
00388         if (show_secs)
00389         {
00390             flen = g_snprintf (buff, len,
00391                 "-%02d:%02d:%02d", (int) (-secs / 3600),
00392                 (int) ((-secs % 3600) / 60), (int) (-secs % 60));
00393         }
00394         else
00395         {
00396             flen = g_snprintf (buff, len,
00397                 "-%02d:%02d", (int) (-secs / 3600),
00398                 (int) ((-secs % 3600) / 60));
00399         }
00400     }
00401     return flen;
00402 }
00403 
00404 size_t
00405 qof_print_minutes_elapsed_buff (char *buff, size_t len, int secs,
00406     gboolean show_secs)
00407 {
00408     size_t flen;
00409     if (0 <= secs)
00410     {
00411         if (show_secs)
00412         {
00413             flen = g_snprintf (buff, len,
00414                 "%02d:%02d", (int) (secs / 60), (int) (secs % 60));
00415         }
00416         else
00417         {
00418             flen = g_snprintf (buff, len, "%02d", (int) (secs / 60));
00419         }
00420     }
00421     else
00422     {
00423         if (show_secs)
00424         {
00425             flen = g_snprintf (buff, len,
00426                 "-%02d:%02d", (int) (-secs / 60), (int) (-secs % 60));
00427         }
00428         else
00429         {
00430             flen = g_snprintf (buff, len, "-%02d", (int) (-secs / 60));
00431         }
00432     }
00433     return flen;
00434 }
00435 
00436 size_t
00437 qof_print_date_time_buff (char *buff, size_t len, time_t secs)
00438 {
00439     int flen;
00440     int day, month, year, hour, min, sec;
00441     struct tm ltm, gtm;
00442 
00443     if (!buff)
00444         return 0;
00445     ltm = *localtime (&secs);
00446     day = ltm.tm_mday;
00447     month = ltm.tm_mon + 1;
00448     year = ltm.tm_year + 1900;
00449     hour = ltm.tm_hour;
00450     min = ltm.tm_min;
00451     sec = ltm.tm_sec;
00452     switch (qof_date_format_get_current ())
00453     {
00454     case QOF_DATE_FORMAT_UK:
00455         flen =
00456             g_snprintf (buff, len, "%2d/%2d/%-4d %2d:%02d", day, month,
00457             year, hour, min);
00458         break;
00459     case QOF_DATE_FORMAT_CE:
00460         flen =
00461             g_snprintf (buff, len, "%2d.%2d.%-4d %2d:%02d", day, month,
00462             year, hour, min);
00463         break;
00464     case QOF_DATE_FORMAT_ISO:
00465         flen =
00466             g_snprintf (buff, len, "%04d-%02d-%02d %02d:%02d", year, month,
00467             day, hour, min);
00468         break;
00469     case QOF_DATE_FORMAT_UTC:
00470         {
00471             gtm = *gmtime (&secs);
00472             flen = strftime (buff, len, QOF_UTC_DATE_FORMAT, &gtm);
00473             break;
00474         }
00475     case QOF_DATE_FORMAT_LOCALE:
00476         {
00477             flen = strftime (buff, len, QOF_D_T_FMT, &ltm);
00478         }
00479         break;
00480 
00481     case QOF_DATE_FORMAT_US:
00482     default:
00483         flen =
00484             g_snprintf (buff, len, "%2d/%2d/%-4d %2d:%02d", month, day,
00485             year, hour, min);
00486         break;
00487     }
00488     return flen;
00489 }
00490 
00491 size_t
00492 qof_print_time_buff (gchar * buff, size_t len, time_t secs)
00493 {
00494     gint flen;
00495     struct tm ltm, gtm;
00496 
00497     if (!buff)
00498         return 0;
00499     if (qof_date_format_get_current () == QOF_DATE_FORMAT_UTC)
00500     {
00501         gtm = *gmtime (&secs);
00502         flen = strftime (buff, len, QOF_UTC_DATE_FORMAT, &gtm);
00503         return flen;
00504     }
00505     ltm = *localtime (&secs);
00506     flen = strftime (buff, len, QOF_T_FMT, &ltm);
00507 
00508     return flen;
00509 }
00510 
00511 gboolean
00512 qof_is_same_day (time_t ta, time_t tb)
00513 {
00514     gboolean retval;
00515     GDate *da, *db;
00516 
00517     da = g_date_new ();
00518     db = g_date_new ();
00519     g_date_set_time_t (da, ta);
00520     g_date_set_time_t (db, tb);
00521     retval = g_date_compare (da, db);
00522     g_date_free (da);
00523     g_date_free (db);
00524     return retval;
00525 }
00526 
00527 void
00528 gnc_tm_set_day_start (struct tm *tm)
00529 {
00530     tm->tm_hour = 0;
00531     tm->tm_min = 0;
00532     tm->tm_sec = 0;
00533     tm->tm_isdst = -1;
00534 }
00535 
00536 void
00537 gnc_tm_get_day_start (struct tm *tm, time_t time_val)
00538 {
00539     tm = localtime_r (&time_val, tm);
00540     gnc_tm_set_day_start (tm);
00541 }
00542 
00543 void
00544 gnc_tm_set_day_middle (struct tm *tm)
00545 {
00546     tm->tm_hour = 12;
00547     tm->tm_min = 0;
00548     tm->tm_sec = 0;
00549     tm->tm_isdst = -1;
00550 }
00551 
00552 void
00553 gnc_tm_set_day_end (struct tm *tm)
00554 {
00555     tm->tm_hour = 23;
00556     tm->tm_min = 59;
00557     tm->tm_sec = 59;
00558     tm->tm_isdst = -1;
00559 }
00560 
00561 void
00562 gnc_tm_get_day_end (struct tm *tm, time_t time_val)
00563 {
00564     tm = localtime_r (&time_val, tm);
00565     gnc_tm_set_day_end (tm);
00566 }
00567 
00568 time_t
00569 gnc_timet_get_day_start (time_t time_val)
00570 {
00571     struct tm tm;
00572 
00573     gnc_tm_get_day_start (&tm, time_val);
00574     return mktime (&tm);
00575 }
00576 
00577 time_t
00578 gnc_timet_get_day_end (time_t time_val)
00579 {
00580     struct tm tm;
00581 
00582     gnc_tm_get_day_end (&tm, time_val);
00583     return mktime (&tm);
00584 }
00585 
00586 #ifndef GNUCASH_MAJOR_VERSION
00587 time_t
00588 gnc_timet_get_day_start_gdate (GDate * date)
00589 {
00590     struct tm stm;
00591     time_t secs;
00592 
00593     stm.tm_year = g_date_get_year (date) - 1900;
00594     stm.tm_mon = g_date_get_month (date) - 1;
00595     stm.tm_mday = g_date_get_day (date);
00596     gnc_tm_set_day_start (&stm);
00597     secs = mktime (&stm);
00598     return secs;
00599 }
00600 
00601 time_t
00602 gnc_timet_get_day_end_gdate (GDate * date)
00603 {
00604     struct tm stm;
00605     time_t secs;
00606 
00607     stm.tm_year = g_date_get_year (date) - 1900;
00608     stm.tm_mon = g_date_get_month (date) - 1;
00609     stm.tm_mday = g_date_get_day (date);
00610     gnc_tm_set_day_end (&stm);
00611     secs = mktime (&stm);
00612     return secs;
00613 }
00614 #endif
00615 int
00616 gnc_date_my_last_mday (int month, int year)
00617 {
00618     return g_date_get_days_in_month (month, year);
00619 }
00620 
00621 int
00622 date_get_last_mday (struct tm *tm)
00623 {
00624     return g_date_get_days_in_month (tm->tm_mon + 1, tm->tm_year + 1900);
00625 }
00626 
00627 gboolean
00628 date_is_last_mday (struct tm * tm)
00629 {
00630     return (tm->tm_mday ==
00631         g_date_get_days_in_month (tm->tm_mon + 1, tm->tm_year + 1900));
00632 }
00633 
00634 int
00635 gnc_timespec_last_mday (Timespec t)
00636 {
00637     /* Replacement code should not use localtime */
00638     struct tm *result;
00639     time_t t_secs = t.tv_sec + (t.tv_nsec / QOF_NSECS);
00640     result = localtime (&t_secs);
00641     return date_get_last_mday (result);
00642 }
00643 
00644 void
00645 gnc_tm_get_today_start (struct tm *tm)
00646 {
00647     gnc_tm_get_day_start (tm, time (NULL));
00648 }
00649 
00650 void
00651 gnc_tm_get_today_end (struct tm *tm)
00652 {
00653     gnc_tm_get_day_end (tm, time (NULL));
00654 }
00655 
00656 time_t
00657 gnc_timet_get_today_start (void)
00658 {
00659     struct tm tm;
00660 
00661     gnc_tm_get_day_start (&tm, time (NULL));
00662     return mktime (&tm);
00663 }
00664 
00665 time_t
00666 gnc_timet_get_today_end (void)
00667 {
00668     struct tm tm;
00669 
00670     gnc_tm_get_day_end (&tm, time (NULL));
00671     return mktime (&tm);
00672 }
00673 
00674 char *
00675 xaccDateUtilGetStamp (time_t thyme)
00676 {
00677     struct tm *stm;
00678 
00679     stm = localtime (&thyme);
00680     return g_strdup_printf ("%04d%02d%02d%02d%02d%02d",
00681         (stm->tm_year + 1900),
00682         (stm->tm_mon + 1),
00683         stm->tm_mday, stm->tm_hour, stm->tm_min, stm->tm_sec);
00684 }
00685 
00686 size_t
00687 qof_print_date_dmy_buff (char *buff, size_t len, int day, int month,
00688     int year)
00689 {
00690     int flen;
00691     if (!buff)
00692         return 0;
00693     switch (qof_date_format_get_current ())
00694     {
00695     case QOF_DATE_FORMAT_UK:
00696         flen = g_snprintf (buff, len, "%2d/%2d/%-4d", day, month, year);
00697         break;
00698     case QOF_DATE_FORMAT_CE:
00699         flen = g_snprintf (buff, len, "%2d.%2d.%-4d", day, month, year);
00700         break;
00701     case QOF_DATE_FORMAT_LOCALE:
00702         {
00703             struct tm tm_str;
00704             time_t t;
00705             tm_str.tm_mday = day;
00706             tm_str.tm_mon = month - 1;
00707             tm_str.tm_year = year - 1900;
00708             gnc_tm_set_day_start (&tm_str);
00709             t = mktime (&tm_str);
00710             localtime_r (&t, &tm_str);
00711             flen = strftime (buff, len, QOF_D_FMT, &tm_str);
00712             if (flen != 0)
00713                 break;
00714         }
00715     case QOF_DATE_FORMAT_ISO:
00716     case QOF_DATE_FORMAT_UTC:
00717         flen = g_snprintf (buff, len, "%04d-%02d-%02d", year, month, day);
00718         break;
00719     case QOF_DATE_FORMAT_US:
00720     default:
00721         flen = g_snprintf (buff, len, "%2d/%2d/%-4d", month, day, year);
00722         break;
00723     }
00724     return flen;
00725 }
00726 
00727 size_t
00728 qof_print_date_buff (char *buff, size_t len, time_t t)
00729 {
00730     struct tm *theTime;
00731     if (!buff)
00732         return 0;
00733     theTime = localtime (&t);
00734     return qof_print_date_dmy_buff (buff, len,
00735         theTime->tm_mday, theTime->tm_mon + 1, theTime->tm_year + 1900);
00736 }
00737 
00738 size_t
00739 qof_print_gdate (gchar * buf, size_t len, GDate * gd)
00740 {
00741     QofDateFormat df;
00742     QofDate *qd;
00743     gchar *str;
00744 
00745     df = qof_date_format_get_current ();
00746     qd = qof_date_from_gdate (gd);
00747     str = qof_date_print (qd, df);
00748     qof_date_free (qd);
00749     g_stpcpy (buf, str);
00750     g_free (str);
00751     return strlen (buf);
00752 }
00753 
00754 gchar *
00755 qof_print_date (time_t t)
00756 {
00757     QofDateFormat df;
00758     QofTime *time;
00759     gchar *str;
00760 
00761     df = qof_date_format_get_current ();
00762     time = qof_time_from_time_t (t, 0);
00763     str = qof_date_print (qof_date_from_qtime (time), df);
00764     qof_time_free (time);
00765     return str;
00766 }
00767 const gchar *
00768 gnc_print_date (Timespec ts)
00769 {
00770     static gchar buff[MAX_DATE_LENGTH];
00771     QofDateFormat df;
00772     QofTime *time;
00773     gchar *str;
00774 
00775     df = qof_date_format_get_current ();
00776     ENTER (" using date format %d", df);
00777     time = timespecToQofTime (ts);
00778     str = qof_date_print (qof_date_from_qtime (time), df);
00779     qof_time_free (time);
00780     g_stpcpy (buff, str);
00781     g_free (str);
00782     LEAVE (" printing %s", buff);
00783     return buff;
00784 }
00785 
00786 /* This is now user configured through the gnome options system() */
00787 static QofDateFormat dateFormat = QOF_DATE_FORMAT_LOCALE;
00788 static QofDateFormat prevQofDateFormat = QOF_DATE_FORMAT_LOCALE;
00789 
00790 static gboolean
00791 qof_scan_date_internal (const char *buff, int *day, int *month, int *year,
00792                   QofDateFormat which_format)
00793 {
00794    char *dupe, *tmp, *first_field, *second_field, *third_field;
00795    int iday, imonth, iyear;
00796    struct tm *now, utc;
00797    time_t secs;
00798 
00799    if (!buff) return(FALSE);
00800 
00801     if(which_format == QOF_DATE_FORMAT_UTC)
00802     {
00803         if(strptime(buff, QOF_UTC_DATE_FORMAT, &utc)) {
00804             *day = utc.tm_mday;
00805             *month = utc.tm_mon + 1;
00806             *year = utc.tm_year + 1900;
00807             return TRUE;
00808         }
00809         else { return FALSE; }
00810     }
00811    dupe = g_strdup (buff);
00812 
00813    tmp = dupe;
00814    first_field = NULL;
00815    second_field = NULL;
00816    third_field = NULL;
00817 
00818    /* Use strtok to find delimiters */
00819    if (tmp) {
00820      static char *delims = ".,-+/\\() ";
00821 
00822       first_field = strtok (tmp, delims);
00823       if (first_field) {
00824          second_field = strtok (NULL, delims);
00825          if (second_field) {
00826             third_field = strtok (NULL, delims);
00827          }
00828       }
00829    }
00830 
00831    /* If any fields appear to be blank, use today's date */
00832    time (&secs);
00833    now = localtime (&secs);
00834    iday = now->tm_mday; 
00835    imonth = now->tm_mon+1;
00836    iyear = now->tm_year+1900;
00837 
00838    /* get numeric values */
00839    switch (which_format)
00840    {
00841      case QOF_DATE_FORMAT_LOCALE:
00842        if (buff[0] != '\0')
00843        {
00844          struct tm thetime;
00845 
00846          /* Parse time string. */
00847          memset(&thetime, -1, sizeof(struct tm));
00848          strptime (buff, QOF_D_FMT, &thetime);
00849 
00850          if (third_field) {
00851            /* Easy.  All three values were parsed. */
00852            iyear = thetime.tm_year + 1900;
00853            iday = thetime.tm_mday;
00854            imonth = thetime.tm_mon + 1;
00855          } else if (second_field) {
00856            /* Hard. Two values parsed.  Figure out the ordering. */
00857            if (thetime.tm_year == -1) {
00858              /* %m-%d or %d-%m. Don't care. Already parsed correctly. */
00859              iday = thetime.tm_mday;
00860              imonth = thetime.tm_mon + 1;
00861            } else if (thetime.tm_mon != -1) {
00862              /* Must be %Y-%m-%d. Reparse as %m-%d.*/
00863              imonth = atoi(first_field);
00864              iday = atoi(second_field);
00865            } else {
00866              /* Must be %Y-%d-%m. Reparse as %d-%m. */
00867              iday = atoi(first_field);
00868              imonth = atoi(second_field);
00869            }
00870          } else if (first_field) {
00871            iday = atoi(first_field);
00872          }
00873        }
00874        break;
00875      case QOF_DATE_FORMAT_UK:
00876      case QOF_DATE_FORMAT_CE:
00877        if (third_field) {
00878          iday = atoi(first_field);
00879          imonth = atoi(second_field);
00880          iyear = atoi(third_field);
00881        } else if (second_field) {
00882          iday = atoi(first_field);
00883          imonth = atoi(second_field);
00884        } else if (first_field) {
00885          iday = atoi(first_field);
00886        }
00887        break;
00888      case QOF_DATE_FORMAT_ISO:
00889        if (third_field) {
00890          iyear = atoi(first_field);
00891          imonth = atoi(second_field);
00892          iday = atoi(third_field);
00893        } else if (second_field) {
00894          imonth = atoi(first_field);
00895          iday = atoi(second_field);
00896        } else if (first_field) {
00897          iday = atoi(first_field);
00898        }
00899        break;
00900     case QOF_DATE_FORMAT_US:
00901     default:
00902        if (third_field) {
00903          imonth = atoi(first_field);
00904          iday = atoi(second_field);
00905          iyear = atoi(third_field);
00906        } else if (second_field) {
00907          imonth = atoi(first_field);
00908          iday = atoi(second_field);
00909        } else if (first_field) {
00910          iday = atoi(first_field);
00911        }
00912        break;
00913    }
00914 
00915    g_free (dupe);
00916 
00917    if ((12 < imonth) || (31 < iday)) 
00918    {
00919      /* 
00920       * Ack! Thppfft!  Someone just fed this routine a string in the
00921       * wrong date format.  This is known to happen if a register
00922       * window is open when changing the date format.  Try the
00923       * previous date format.  If that doesn't work, see if we can
00924       * exchange month and day. If that still doesn't work,
00925       * bail and give the caller what they asked for (garbage) 
00926       * parsed in the new format.
00927       *
00928       * Note: This test cannot detect any format change that only
00929       * swaps month and day field, if the day is 12 or less.  This is
00930       * deemed acceptable given the obscurity of this bug.
00931       */
00932      if ((which_format != prevQofDateFormat) &&
00933          qof_scan_date_internal(buff, day, month, year, prevQofDateFormat))
00934      {
00935        return(TRUE);
00936      }
00937      if ((12 < imonth) && (12 >= iday))
00938      {
00939         int tmp = imonth; imonth = iday; iday = tmp;
00940      } 
00941       else
00942       {
00943         return FALSE;
00944       }
00945    }
00946 
00947    /* If the year entered is smaller than 100, assume we mean the current
00948       century (and are not revising some roman emperor's books) */
00949    if (iyear < 100)
00950      iyear += ((int) ((now->tm_year+1950-iyear)/100)) * 100;
00951 
00952    if (year) *year=iyear;
00953    if (month) *month=imonth;
00954    if (day) *day=iday;
00955    return(TRUE);
00956 }
00957 
00958 gboolean
00959 qof_scan_date (const char *buff, int *day, int *month, int *year)
00960 {
00961   return qof_scan_date_internal(buff, day, month, year, dateFormat);
00962 }
00963 
00964 gboolean
00965 qof_scan_date_secs (const gchar * buff, time_t * secs)
00966 {
00967   gboolean rc;
00968   int day, month, year;
00969   
00970   rc = qof_scan_date_internal(buff, &day, &month, &year, dateFormat);
00971   if (secs) *secs = xaccDMYToSec (day, month, year);
00972 
00973   return rc;
00974 }
00975 
00976 Timespec
00977 gnc_dmy2timespec (gint day, gint month, gint year)
00978 {
00979     Timespec ts;
00980     QofTime *qt;
00981     QofDate *qd;
00982 
00983     if (!g_date_valid_dmy (day, month, year))
00984         return null_timespec ();
00985     qd = qof_date_new ();
00986     qd->qd_mday = day;
00987     qd->qd_mon  = month;
00988     qd->qd_year = year;
00989     qof_date_valid (qd);
00990     qt = qof_date_to_qtime (qd);
00991     ts = qof_time_to_Timespec (qt);
00992     qof_time_free (qt);
00993     qof_date_free (qd);
00994     return ts;
00995 }
00996 
00997 Timespec
00998 gnc_dmy2timespec_end (gint day, gint month, gint year)
00999 {
01000     Timespec ts;
01001     QofTime *qt;
01002     QofDate *qd;
01003 
01004     if (!g_date_valid_dmy (day, month, year))
01005         return null_timespec ();
01006     qd = qof_date_new ();
01007     qd->qd_mday = day;
01008     qd->qd_mon  = month;
01009     qd->qd_year = year;
01010     qof_date_set_day_end (qd);
01011     qt = qof_date_to_qtime (qd);
01012     ts = qof_time_to_Timespec (qt);
01013     qof_time_free (qt);
01014     qof_date_free (qd);
01015     return ts;
01016 }
01017 
01018 Timespec
01019 gnc_iso8601_to_timespec_gmt (const gchar * str)
01020 {
01021     gchar buf[4];
01022     gchar *dupe;
01023     Timespec ts;
01024     struct tm stm;
01025     glong nsec = 0;
01026 
01027     ts.tv_sec = 0;
01028     ts.tv_nsec = 0;
01029     if (!str)
01030         return ts;
01031     dupe = g_strdup (str);
01032     stm.tm_year = atoi (str) - 1900;
01033     str = strchr (str, '-');
01034     if (str)
01035     {
01036         str++;
01037     }
01038     else
01039     {
01040         return ts;
01041     }
01042     stm.tm_mon = atoi (str) - 1;
01043     str = strchr (str, '-');
01044     if (str)
01045     {
01046         str++;
01047     }
01048     else
01049     {
01050         return ts;
01051     }
01052     stm.tm_mday = atoi (str);
01053 
01054     str = strchr (str, ' ');
01055     if (str)
01056     {
01057         str++;
01058     }
01059     else
01060     {
01061         return ts;
01062     }
01063     stm.tm_hour = atoi (str);
01064     str = strchr (str, ':');
01065     if (str)
01066     {
01067         str++;
01068     }
01069     else
01070     {
01071         return ts;
01072     }
01073     stm.tm_min = atoi (str);
01074     str = strchr (str, ':');
01075     if (str)
01076     {
01077         str++;
01078     }
01079     else
01080     {
01081         return ts;
01082     }
01083     stm.tm_sec = atoi (str);
01084 
01085     if (strchr (str, '.'))
01086     {
01087         gint decimals, i, multiplier = 1000000000;
01088         str = strchr (str, '.') + 1;
01089         decimals = strcspn (str, "+- ");
01090         for (i = 0; i < decimals; i++)
01091             multiplier /= 10;
01092         nsec = atoi (str) * multiplier;
01093     }
01094     stm.tm_isdst = -1;
01095 
01096     str += strcspn (str, "+-");
01097     if (str)
01098     {
01099         buf[0] = str[0];
01100         buf[1] = str[1];
01101         buf[2] = str[2];
01102         buf[3] = 0;
01103         stm.tm_hour -= atoi (buf);
01104 
01105         str += 3;
01106         if ('.' == *str)
01107             str++;
01108         if (isdigit ((guchar) * str) && isdigit ((guchar) * (str + 1)))
01109         {
01110             gint cyn;
01111             if ('+' == buf[0])
01112             {
01113                 cyn = -1;
01114             }
01115             else
01116             {
01117                 cyn = +1;
01118             }
01119             buf[0] = str[0];
01120             buf[1] = str[1];
01121             buf[2] = str[2];
01122             buf[3] = 0;
01123             stm.tm_min += cyn * atoi (buf);
01124         }
01125     }
01126 
01127     {
01128         struct tm tmp_tm;
01129         struct tm tm;
01130         glong tz;
01131         gint tz_hour;
01132         time_t secs;
01133 
01134         tmp_tm = stm;
01135         tmp_tm.tm_isdst = -1;
01136 
01137         secs = mktime (&tmp_tm);
01138 
01139         if (secs < 0)
01140         {
01141             PWARN (" mktime failed to handle daylight saving: "
01142                 "tm_hour=%d tm_year=%d tm_min=%d tm_sec=%d tm_isdst=%d for string=%s",
01143                 stm.tm_hour, stm.tm_year, stm.tm_min,
01144                 stm.tm_sec, stm.tm_isdst, dupe);
01145             tmp_tm.tm_hour++;
01146             secs = mktime (&tmp_tm);
01147             if (secs < 0)
01148             {
01149                 tmp_tm.tm_hour -= 2;
01150                 secs = mktime (&tmp_tm);
01151             }
01152             if (secs < 0)
01153             {
01154                 PERR (" unable to recover from buggy mktime ");
01155                 g_free (dupe);
01156                 return ts;
01157             }
01158         }
01159 
01160         tm = *localtime_r (&secs, &tm);
01161 
01162         tz = gnc_timezone (&tmp_tm);
01163 
01164         tz_hour = tz / 3600;
01165         stm.tm_hour -= tz_hour;
01166         stm.tm_min -= (tz % 3600) / 60;
01167         stm.tm_isdst = tmp_tm.tm_isdst;
01168         ts.tv_sec = mktime (&stm);
01169         if (ts.tv_sec < 0)
01170         {
01171             PWARN (" mktime failed to adjust calculated time:"
01172                 " tm_hour=%d tm_year=%d tm_min=%d tm_sec=%d tm_isdst=%d",
01173                 stm.tm_hour, stm.tm_year, stm.tm_min,
01174                 stm.tm_sec, stm.tm_isdst);
01175             ts.tv_sec = secs - tz;
01176         }
01177         ts.tv_nsec = nsec;
01178     }
01179     g_free (dupe);
01180     return ts;
01181 }
01182 
01183 gchar *
01184 gnc_timespec_to_iso8601_buff (Timespec ts, gchar * buff)
01185 {
01186     gint len, tz_hour, tz_min;
01187     glong secs;
01188     gchar cyn;
01189     time_t tmp;
01190     struct tm parsed;
01191 
01192     tmp = ts.tv_sec;
01193     localtime_r (&tmp, &parsed);
01194 
01195     secs = gnc_timezone (&parsed);
01196     tz_hour = secs / 3600;
01197     tz_min = (secs % 3600) / 60;
01198 
01199     cyn = '-';
01200     if (0 > tz_hour)
01201     {
01202         cyn = '+';
01203         tz_hour = -tz_hour;
01204     }
01205 
01206     len = sprintf (buff, "%4d-%02d-%02d %02d:%02d:%02d.%06ld %c%02d%02d",
01207         parsed.tm_year + 1900,
01208         parsed.tm_mon + 1,
01209         parsed.tm_mday,
01210         parsed.tm_hour,
01211         parsed.tm_min,
01212         parsed.tm_sec, ts.tv_nsec / 1000, cyn, tz_hour, tz_min);
01213 
01214     buff += len;
01215     return buff;
01216 }
01217 
01218 void
01219 gnc_timespec2dmy (Timespec ts, gint * day, gint * month, gint * year)
01220 {
01221     QofTime *time;
01222     QofDate *qd;
01223 
01224     time = timespecToQofTime (ts);
01225     qd = qof_date_from_qtime (time);
01226     qof_time_free (time);
01227     if (day)
01228         *day = qd->qd_mday;
01229     if (month)
01230         *month = qd->qd_mon;
01231     if ((year) && (qd->qd_year < 0) && (qd->qd_year > G_MAXINT))
01232         *year = (gint)qd->qd_year;
01233 }
01234 
01235 glong
01236 gnc_timezone (struct tm *tm)
01237 {
01238     g_return_val_if_fail (tm != NULL, 0);
01239 
01240 #ifdef HAVE_STRUCT_TM_GMTOFF
01241     return -(tm->tm_gmtoff);
01242 #else
01243     return (glong) (timezone - (tm->tm_isdst > 0 ? 3600 : 0));
01244 #endif
01245 }
01246 
01247 Timespec
01248 qof_instance_get_last_update (QofInstance * inst)
01249 {
01250     Timespec ts;
01251     ts = inst->last_update;
01252     inst->update_time = timespecToQofTime (ts);
01253     return ts;
01254 }
01255 
01256 void
01257 qof_instance_set_last_update (QofInstance * inst, Timespec ts)
01258 {
01259     QofTime *time;
01260     g_return_if_fail (inst);
01261     inst->last_update = ts;
01262     time = timespecToQofTime (ts);
01263     qof_instance_set_update_time (inst, time);
01264 }
01265 time_t 
01266 xaccDMYToSec (int day, int month, int year)
01267 {
01268   struct tm stm;
01269   time_t secs;
01270 
01271   stm.tm_year = year - 1900;
01272   stm.tm_mon = month - 1;
01273   stm.tm_mday = day;
01274   gnc_tm_set_day_start(&stm);
01275 
01276   secs = mktime (&stm);
01277 
01278   return secs;
01279 }
01280 void date_add_months (struct tm *tm, int months, gboolean track_last_day)
01281 {
01282   gboolean was_last_day;
01283   int new_last_mday;
01284 
01285   was_last_day = date_is_last_mday(tm);
01286 
01287   tm->tm_mon += months;
01288   while (tm->tm_mon > 11) {
01289     tm->tm_mon -= 12;
01290     tm->tm_year++;
01291   }
01292 
01293   if (!track_last_day)
01294     return;
01295 
01296   new_last_mday = date_get_last_mday(tm);
01297   if (was_last_day || (tm->tm_mday > new_last_mday))
01298     tm->tm_mday = new_last_mday;
01299 }
01300 char dateSeparator (void)
01301 {
01302     return qof_date_format_get_date_separator (qof_date_format_get_current());
01303 }
01304 
01305 const char*
01306 gnc_date_dateformat_to_string(QofDateFormat format)
01307 {
01308   switch (format) {
01309   case QOF_DATE_FORMAT_US:
01310     return "us";
01311   case QOF_DATE_FORMAT_UK:
01312     return "uk";
01313   case QOF_DATE_FORMAT_CE:
01314     return "ce";
01315   case QOF_DATE_FORMAT_ISO:
01316     return "iso";
01317   case QOF_DATE_FORMAT_UTC:
01318    return "utc";
01319   case QOF_DATE_FORMAT_LOCALE:
01320     return "locale";
01321   case QOF_DATE_FORMAT_CUSTOM:
01322     return "custom";
01323   default:
01324     return NULL;    
01325   }
01326 }
01327 const char*
01328 gnc_date_monthformat_to_string(GNCDateMonthFormat format)
01329 {
01330   switch (format) {
01331   case GNCDATE_MONTH_NUMBER:
01332     return "number";
01333   case GNCDATE_MONTH_ABBREV:
01334     return "abbrev";
01335   case GNCDATE_MONTH_NAME:
01336     return "name";
01337   default:
01338     return NULL;
01339   }
01340 }
01341 gboolean
01342 qof_date_add_days(Timespec *ts, gint days)
01343 {
01344     struct tm tm;
01345     time_t    tt;
01346 
01347     g_return_val_if_fail(ts, FALSE);
01348     tt = timespecToTime_t(*ts);
01349 #ifdef HAVE_GMTIME_R
01350     tm = *gmtime_r(&tt, &tm);
01351 #else
01352     tm = *gmtime(&tt);
01353 #endif
01354     tm.tm_mday += days;
01355     tt = mktime(&tm);
01356     if(tt < 0) { return FALSE; }
01357     timespecFromTime_t(ts, tt);
01358     return TRUE;
01359 }
01360 
01361 gboolean
01362 qof_date_add_months(Timespec *ts, gint months, gboolean track_last_day)
01363 {
01364     struct tm tm;
01365     time_t    tt;
01366     gint new_last_mday;
01367     gboolean was_last_day;
01368 
01369     g_return_val_if_fail(ts, FALSE);
01370     tt = timespecToTime_t(*ts);
01371 #ifdef HAVE_GMTIME_R
01372     tm = *gmtime_r(&tt, &tm);
01373 #else
01374     tm = *gmtime(&tt);
01375 #endif
01376     was_last_day = date_is_last_mday(&tm);
01377     tm.tm_mon += months;
01378     while (tm.tm_mon > 11) {
01379         tm.tm_mon -= 12;
01380         tm.tm_year++;
01381     }
01382     if (track_last_day) {
01383         new_last_mday = date_get_last_mday(&tm);
01384         if (was_last_day || (tm.tm_mday > new_last_mday)) {
01385             tm.tm_mday = new_last_mday;
01386         }
01387     }
01388     tt = mktime(&tm);
01389     if(tt < 0) { return FALSE; }
01390     timespecFromTime_t(ts, tt);
01391     return TRUE;
01392 }
01393 
01394 QofDateFormat qof_date_format_get (void)
01395 {
01396   return qof_date_format_get_current ();
01397 }
01398 
01399 const gchar *qof_date_format_get_string(QofDateFormat df)
01400 {
01401   switch(df) {
01402    case QOF_DATE_FORMAT_US:
01403     return "%m/%d/%y";
01404    case QOF_DATE_FORMAT_UK:
01405     return "%d/%m/%y";
01406    case QOF_DATE_FORMAT_CE:
01407     return "%d.%m.%y";
01408    case QOF_DATE_FORMAT_UTC:
01409     return "%Y-%m-%dT%H:%M:%SZ";
01410    case QOF_DATE_FORMAT_ISO:
01411     return "%y-%m-%d";
01412    case QOF_DATE_FORMAT_LOCALE:
01413    default:
01414     return QOF_D_FMT;
01415   };
01416 }
01417 
01418 void qof_date_format_set(QofDateFormat df)
01419 {
01420     if(!qof_date_format_set_current (df))
01421         PERR (" unable to set current format, %d", df);
01422 }
01423 
01424 const gchar *qof_date_text_format_get_string(QofDateFormat df)
01425 {
01426     return qof_date_format_get_format (df);
01427 }
01428 
01429 char *
01430 xaccDateUtilGetStampNow (void)
01431 {
01432    return qof_time_stamp_now ();
01433 }
01434 
01435 void
01436 kvp_frame_set_timespec (KvpFrame * frame, const char *path, Timespec ts)
01437 {
01438     KvpValue *value;
01439     value = kvp_value_new_timespec (ts);
01440     frame = kvp_frame_set_value_nc (frame, path, value);
01441     if (!frame)
01442         kvp_value_delete (value);
01443 }
01444 
01445 void
01446 kvp_frame_add_timespec (KvpFrame * frame, const char *path, Timespec ts)
01447 {
01448     KvpValue *value;
01449     value = kvp_value_new_timespec (ts);
01450     frame = kvp_frame_add_value_nc (frame, path, value);
01451     if (!frame)
01452         kvp_value_delete (value);
01453 }
01454 
01455 Timespec
01456 kvp_frame_get_timespec (const KvpFrame * frame, const char *path)
01457 {
01458     QofTime *qt;
01459     Timespec ts;
01460     char *key;
01461 
01462     key = NULL;
01463     ts.tv_sec = 0;
01464     ts.tv_nsec = 0;
01465     qt = kvp_value_get_time (kvp_frame_get_slot (frame, key));
01466     if(!qt)
01467         return ts;
01468     return qof_time_to_Timespec (qt);
01469 }
01470 
01471 KvpValue *
01472 kvp_value_new_timespec (Timespec value)
01473 {
01474     QofTime *qt;
01475     KvpValue *retval;
01476 
01477     qt = timespecToQofTime (value);
01478     retval = kvp_value_new_time (qt);
01479     return retval;
01480 }
01481 
01482 Timespec
01483 kvp_value_get_timespec (const KvpValue * value)
01484 {
01485     Timespec ts;
01486     QofTime *qt;
01487     ts.tv_sec = 0;
01488     ts.tv_nsec = 0;
01489     if (!value)
01490         return ts;
01491     qt = kvp_value_get_time (value);
01492     ts = qof_time_to_Timespec (qt);
01493     return ts;
01494 }
01495 #define NUM_CLOCKS 10
01496 static struct timeval qof_clock[NUM_CLOCKS] = {
01497     {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0},
01498     {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0},
01499 };
01500 
01501 static struct timeval qof_clock_total[NUM_CLOCKS] = {
01502     {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0},
01503     {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0},
01504 };
01505 
01506 void
01507 qof_start_clock (gint clockno, QofLogModule log_module,
01508     QofLogLevel log_level, const gchar * function_name,
01509     const gchar * format, ...)
01510 {
01511     va_list ap;
01512 
01513     if ((0 > clockno) || (NUM_CLOCKS <= clockno))
01514         return;
01515 #ifdef HAVE_GETTIMEOFDAY
01516     gettimeofday (&qof_clock[clockno], NULL);
01517 #else
01518     time (&(qof_clock[clockno].tv_sec));
01519     qof_clock[clockno].tv_usec = 0;
01520 #endif
01521 
01522     if (!fout)
01523         qof_log_init ();
01524 
01525     fprintf (fout, "Clock %d Start: %s: ",
01526         clockno, qof_log_prettify (function_name));
01527 
01528     va_start (ap, format);
01529 
01530     vfprintf (fout, format, ap);
01531 
01532     va_end (ap);
01533 
01534     fprintf (fout, "\n");
01535     fflush (fout);
01536 }
01537 
01538 void
01539 qof_report_clock (gint clockno, QofLogModule log_module,
01540     QofLogLevel log_level, const gchar * function_name,
01541     const gchar * format, ...)
01542 {
01543     struct timeval now;
01544     va_list ap;
01545 
01546     if ((0 > clockno) || (NUM_CLOCKS <= clockno))
01547         return;
01548 #ifdef HAVE_GETTIMEOFDAY
01549     gettimeofday (&now, NULL);
01550 #else
01551     time (&(now.tv_sec));
01552     now.tv_usec = 0;
01553 #endif
01554 
01555     if (now.tv_usec < qof_clock[clockno].tv_usec)
01556     {
01557         now.tv_sec--;
01558         now.tv_usec += 1000000;
01559     }
01560     now.tv_sec -= qof_clock[clockno].tv_sec;
01561     now.tv_usec -= qof_clock[clockno].tv_usec;
01562 
01563     qof_clock_total[clockno].tv_sec += now.tv_sec;
01564     qof_clock_total[clockno].tv_usec += now.tv_usec;
01565 
01566     if (!fout)
01567         qof_log_init ();
01568 
01569     fprintf (fout, "Clock %d Elapsed: %ld.%06lds %s: ",
01570         clockno, (long int) now.tv_sec, (long int) now.tv_usec,
01571         qof_log_prettify (function_name));
01572 
01573     va_start (ap, format);
01574 
01575     vfprintf (fout, format, ap);
01576 
01577     va_end (ap);
01578 
01579     fprintf (fout, "\n");
01580     fflush (fout);
01581 }
01582 
01583 void
01584 qof_report_clock_total (gint clockno,
01585     QofLogModule log_module, QofLogLevel log_level,
01586     const gchar * function_name, const gchar * format, ...)
01587 {
01588     va_list ap;
01589 
01590     if ((0 > clockno) || (NUM_CLOCKS <= clockno))
01591         return;
01592 
01593     while (qof_clock_total[clockno].tv_usec >= 1000000)
01594     {
01595         qof_clock_total[clockno].tv_sec++;
01596         qof_clock_total[clockno].tv_usec -= 1000000;
01597     }
01598 
01599     if (!fout)
01600         qof_log_init ();
01601 
01602     fprintf (fout, "Clock %d Total Elapsed: %ld.%06lds  %s: ",
01603         clockno,
01604         (long int) qof_clock_total[clockno].tv_sec,
01605         (long int) qof_clock_total[clockno].tv_usec,
01606         qof_log_prettify (function_name));
01607 
01608     va_start (ap, format);
01609 
01610     vfprintf (fout, format, ap);
01611 
01612     va_end (ap);
01613 
01614     fprintf (fout, "\n");
01615     fflush (fout);
01616 }
01617 static QofSession *current_session = NULL;
01618 
01619 QofSession *
01620 qof_session_get_current_session (void)
01621 {
01622     if (!current_session)
01623     {
01624         qof_event_suspend ();
01625         current_session = qof_session_new ();
01626         qof_event_resume ();
01627     }
01628 
01629     return current_session;
01630 }
01631 
01632 void
01633 qof_session_set_current_session (QofSession * session)
01634 {
01635     current_session = session;
01636 }
01637 
01638 void
01639 qof_session_clear_current_session (void)
01640 {
01641     current_session = NULL;
01642 }
01643 
01644 gboolean
01645 gnc_strisnum (const guchar * s)
01646 {
01647     return qof_util_string_isnum (s);
01648 }
01649 
01650 KvpFrame *
01651 gnc_kvp_bag_add (KvpFrame * pwd, const char *path,
01652     time_t secs, const char *first_name, ...)
01653 {
01654     QofTime *qt;
01655     KvpFrame *cwd;
01656     va_list ap;
01657 
01658     qt = qof_time_from_time_t (secs, 0);
01659     va_start (ap, first_name);
01660     cwd = qof_kvp_bag_add(pwd, path, qt, first_name, ap);
01661     va_end (ap);
01662     return cwd;
01663 }
01664 
01665 KvpFrame *
01666 gnc_kvp_bag_find_by_guid (KvpFrame * root, const gchar *path,
01667     const gchar *guid_name, GUID * desired_guid)
01668 {
01669     return qof_kvp_bag_find_by_guid (root, path, 
01670         guid_name, desired_guid);
01671 }
01672 
01673 void
01674 gnc_kvp_bag_remove_frame (KvpFrame * root, const char *path, 
01675     KvpFrame * fr)
01676 {
01677     qof_kvp_bag_remove_frame (root, path, fr);
01678 }
01679 
01680 void
01681 gnc_kvp_bag_merge (KvpFrame * kvp_into, const char *intopath,
01682     KvpFrame * kvp_from, const char *frompath)
01683 {
01684     qof_kvp_bag_merge (kvp_into, intopath, kvp_from, frompath);
01685 }
01686 
01687 /* ==================================================================== */
01688 #endif /* QOF_DISABLE_DEPRECATED */

Generated on Fri Sep 1 15:13:11 2006 for QOF by  doxygen 1.4.7