Date: Date and Time Printing, Parsing and Manipulation
[Query Object Framework]


Detailed Description

Utility functions to handle date and time (adjusting, getting the current date, printing the date and time, etc.)

Overall, this file is quite a mess. Note, however, that other applications, besides just GnuCash, use this file. In particular, GnoTime (gttr.sourcefore.net) uses this file, and this file is formally a part of QOF (qof.sourceforge.net).

An important note about time-keeping: The general goal of any program that works with numeric time values SHOULD BE to always stores and use UNIVERSAL TIME internally. Universal time is the 'one true time' that is independent of one's location on planet Earth. It is measured in seconds from midnight January 1, 1970 in localtime-Grenwich (GMT). If one wants to display the local time, then the display-print routine should make all final tweaks to print the local time. The local time *must not* be kept as a numeric value anywhere in the program. If one wants to parse a user's input string as if it were local time, then the output of the parse routine MUST BE universal time. A sane program must never ever store (to file or db) a time that is not Universal Time. Break these rules, and you will rue the day...

Warning:
HACK ALERT -- the scan and print routines should probably be moved to somewhere else. The engine really isn't involved with things like printing formats. This is needed mostly by the GUI and so on. If a file-io backend needs date handling, it should do it itself, instead of depending on the routines here.
(to be renamed qofdate.h in libqof2.)

Author:
Copyright (C) 1997 Robin D. Clark <rclark@cs.hmc.edu>

Copyright (C) 1998-2001,2003 Linas Vepstas <linas@linas.org>


Files

file  gnc-date.h
 Date and Time handling routines.

Data Structures

struct  timespec64
 Use a 64-bit unsigned int timespec. More...

String / DateFormat conversion.



const gchar * gnc_date_dateformat_to_string (QofDateFormat format)
 The string->value versions return FALSE on success and TRUE on failure.
gboolean gnc_date_string_to_dateformat (const gchar *format_string, QofDateFormat *format)
 Converts the date format to a printable string.
const gchar * gnc_date_monthformat_to_string (GNCDateMonthFormat format)
gboolean gnc_date_string_to_monthformat (const gchar *format_string, GNCDateMonthFormat *format)
 Converts the month format to a printable string.

Timespec functions



gboolean timespec_equal (const Timespec *ta, const Timespec *tb)
gint timespec_cmp (const Timespec *ta, const Timespec *tb)
Timespec timespec_diff (const Timespec *ta, const Timespec *tb)
Timespec timespec_abs (const Timespec *t)
Timespec timespecCanonicalDayTime (Timespec t)
void timespecFromTime_t (Timespec *ts, time_t t)
time_t timespecToTime_t (Timespec ts)
Timespec gnc_dmy2timespec (gint day, gint month, gint year)
Timespec gnc_dmy2timespec_end (gint day, gint month, gint year)
Timespec gnc_iso8601_to_timespec_gmt (const gchar *)
gchar * gnc_timespec_to_iso8601_buff (Timespec ts, gchar *buff)
void gnc_timespec2dmy (Timespec ts, gint *day, gint *month, gint *year)
time_t xaccDMYToSec (gint day, gint month, gint year)
glong gnc_timezone (const struct tm *tm)

QofDateFormat functions



QofDateFormat qof_date_format_get (void)
void qof_date_format_set (QofDateFormat df)
const gchar * qof_date_format_get_string (QofDateFormat df)
const gchar * qof_date_text_format_get_string (QofDateFormat df)

Date Printing/Scanning functions

gchar * qof_format_time (const gchar *format, const struct tm *tm)
gsize qof_strftime (gchar *buf, gsize max, const gchar *format, const struct tm *tm)
size_t qof_print_date_dmy_buff (gchar *buff, size_t buflen, int day, int month, int year)
size_t qof_print_date_buff (char *buff, size_t buflen, time_t secs)
size_t qof_print_gdate (char *buf, size_t bufflen, const GDate *gd)
char * qof_print_date (time_t secs)
const char * gnc_print_date (Timespec ts)
size_t qof_print_time_buff (char *buff, size_t len, time_t secs)
size_t qof_print_date_time_buff (char *buff, size_t len, time_t secs)
char * xaccDateUtilGetStamp (time_t thyme)
gboolean qof_scan_date (const char *buff, int *day, int *month, int *year)
gboolean qof_scan_date_secs (const char *buff, time_t *secs)

Date Start/End Adjustment routines

Given a time value, adjust it to be the beginning or end of that day.

void gnc_tm_get_day_start (struct tm *tm, time_t time_val)
void gnc_tm_get_day_end (struct tm *tm, time_t time_val)
time_t gnc_timet_get_day_start (time_t time_val)
time_t gnc_timet_get_day_end (time_t time_val)
int date_get_last_mday (const struct tm *tm)
gboolean date_is_last_mday (const struct tm *tm)
int gnc_date_my_last_mday (int month, int year)
int gnc_timespec_last_mday (Timespec ts)

Today's Date



void gnc_tm_get_today_start (struct tm *tm)
void gnc_tm_get_today_end (struct tm *tm)
time_t gnc_timet_get_today_start (void)
time_t gnc_timet_get_today_end (void)
char * xaccDateUtilGetStampNow (void)
void gnc_dow_abbrev (gchar *buf, int buf_len, int dow)
#define MIN_BUF_LEN   10

Defines

#define MAX_DATE_LENGTH   31
#define QOF_UTC_DATE_FORMAT   "%Y-%m-%dT%H:%M:%SZ"
 UTC date format string.
#define DATE_FORMAT_FIRST   QOF_DATE_FORMAT_US
#define DATE_FORMAT_LAST   QOF_DATE_FORMAT_LOCALE
#define qof_date_format_get_format   qof_date_text_format_get_string

Typedefs

typedef timespec64 Timespec

Enumerations

enum  QofDateFormat {
  QOF_DATE_FORMAT_US, QOF_DATE_FORMAT_UK, QOF_DATE_FORMAT_CE, QOF_DATE_FORMAT_ISO,
  QOF_DATE_FORMAT_UTC, QOF_DATE_FORMAT_LOCALE, QOF_DATE_FORMAT_CUSTOM
}
enum  GNCDateMonthFormat { GNCDATE_MONTH_NUMBER, GNCDATE_MONTH_ABBREV, GNCDATE_MONTH_NAME }

Functions

gchar dateSeparator (void)

Variables

const char * gnc_default_strftime_date_format


Define Documentation

#define MAX_DATE_LENGTH   31

The maximum length of a string created by the date printers

Definition at line 76 of file gnc-date.h.

#define qof_date_format_get_format   qof_date_text_format_get_string

Deprecated:
qof_date_format_get_format has been replaced by qof_date_text_format_get_string

Definition at line 107 of file gnc-date.h.

#define QOF_UTC_DATE_FORMAT   "%Y-%m-%dT%H:%M:%SZ"

UTC date format string.

Timezone independent, date and time inclusive, as used in the QSF backend. The T and Z characters are from xsd:dateTime format in coordinated universal time, UTC. You can reproduce the string from the GNU/Linux command line using the date utility: date -u +Y-m-dTH:M:SZ = 2004-12-12T23:39:11Z The datestring must be timezone independent and include all specified fields. Remember to use gmtime() NOT localtime()!

Definition at line 88 of file gnc-date.h.


Typedef Documentation

typedef struct timespec64 Timespec

The Timespec is just like the unix 'struct timespec' except that we use a 64-bit unsigned int to store the seconds. This should adequately cover dates in the distant future as well as the distant past, as long as they're not more than a couple dozen times the age of the universe Values of this type can range from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.

Definition at line 174 of file gnc-date.h.


Enumeration Type Documentation

enum GNCDateMonthFormat

This is how to format the month, as a number, an abbreviated string, or the full name.

Enumerator:
GNCDATE_MONTH_NUMBER 
GNCDATE_MONTH_ABBREV 
GNCDATE_MONTH_NAME 

Definition at line 113 of file gnc-date.h.

enum QofDateFormat

Enum for determining a date format

Enumerator:
QOF_DATE_FORMAT_US  United states: mm/dd/yyyy
QOF_DATE_FORMAT_UK  Britain: dd/mm/yyyy
QOF_DATE_FORMAT_CE  Continental Europe: dd.mm.yyyy
QOF_DATE_FORMAT_ISO  ISO: yyyy-mm-dd
QOF_DATE_FORMAT_UTC  UTC: 2004-12-12T23:39:11Z
QOF_DATE_FORMAT_LOCALE  Take from locale information
QOF_DATE_FORMAT_CUSTOM  Used by the check printing code

Definition at line 91 of file gnc-date.h.


Function Documentation

int date_get_last_mday ( const struct tm *  tm  ) 

Get the numerical last date of the month. (28, 29, 30, 31)

Definition at line 292 of file gnc-date.c.

00293 {
00294   return gnc_date_my_last_mday (tm->tm_mon+1, tm->tm_year+1900);
00295 }

gboolean date_is_last_mday ( const struct tm *  tm  ) 

Is the mday field the last day of the specified month.

Definition at line 304 of file gnc-date.c.

00305 {
00306   return(tm->tm_mday == date_get_last_mday(tm));
00307 }

gchar dateSeparator ( void   ) 

dateSeparator Return the field separator for the current date format

Args: none

Return: date character

Globals: global dateFormat value

Definition at line 804 of file gnc-date.c.

00805 {
00806   static char locale_separator = '\0';
00807 
00808   switch (dateFormat)
00809   {
00810     case QOF_DATE_FORMAT_CE:
00811       return '.';
00812     case QOF_DATE_FORMAT_ISO:
00813     case QOF_DATE_FORMAT_UTC:
00814       return '-';
00815     case QOF_DATE_FORMAT_US:
00816     case QOF_DATE_FORMAT_UK:
00817     default:
00818       return '/';
00819     case QOF_DATE_FORMAT_LOCALE:
00820       if (locale_separator != '\0')
00821         return locale_separator;
00822       else
00823       { /* Make a guess */
00824         unsigned char string[256];
00825         struct tm tm;
00826         time_t secs;
00827         unsigned char *s;
00828 
00829         secs = time(NULL);
00830         localtime_r(&secs, &tm);
00831         qof_strftime(string, sizeof(string), GNC_D_FMT, &tm);
00832 
00833         for (s = string; s != '\0'; s++)
00834           if (!isdigit(*s))
00835             return (locale_separator = *s);
00836       }
00837   }
00838 
00839   return '\0';
00840 }

int gnc_date_my_last_mday ( int  month,
int  year 
)

Deprecated:
Use date_get_last_mday()

Definition at line 270 of file gnc-date.c.

00271 {
00272   static int last_day_of_month[2][12] = {
00273   /* non leap */ {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
00274   /*   leap   */ {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
00275   };
00276 
00277   /* Is this a leap year? */
00278   if (year % 2000 == 0) return last_day_of_month[1][month-1];
00279   if (year % 400 == 0 ) return last_day_of_month[0][month-1];
00280   if (year % 4   == 0 ) return last_day_of_month[1][month-1];
00281   return last_day_of_month[0][month-1];
00282 }

gboolean gnc_date_string_to_dateformat ( const gchar *  format_string,
QofDateFormat format 
)

Converts the date format to a printable string.

Note the reversed return values!

Returns:
FALSE on success, TRUE on failure.

gboolean gnc_date_string_to_monthformat ( const gchar *  format_string,
GNCDateMonthFormat format 
)

Converts the month format to a printable string.

Note the reversed return values!

Returns:
FALSE on success, TRUE on failure.

Timespec gnc_dmy2timespec ( gint  day,
gint  month,
gint  year 
)

Convert a day, month, and year to a Timespec

Timespec gnc_dmy2timespec_end ( gint  day,
gint  month,
gint  year 
)

Same as gnc_dmy2timespec, but last second of the day

void gnc_dow_abbrev ( gchar *  buf,
int  buf_len,
int  dow 
)

Localized DOW abbreviation.

Parameters:
buf_len at least MIN_BUF_LEN
dow struct tm semantics: 0=sunday .. 6=saturday

Definition at line 1404 of file gnc-date.c.

01405 {
01406     struct tm my_tm;
01407     int i;
01408 
01409     memset(buf, 0, buf_len);
01410     memset(&my_tm, 0, sizeof(struct tm));
01411     my_tm.tm_wday = dow;
01412     i = qof_strftime(buf, buf_len, "%a", &my_tm);
01413     buf[i] = 0;
01414 }

Timespec gnc_iso8601_to_timespec_gmt ( const gchar *   ) 

The gnc_iso8601_to_timespec_gmt() routine converts an ISO-8601 style date/time string to Timespec. Please note that ISO-8601 strings are a representation of Universal Time (UTC), and as such, they 'store' UTC. To make them human readable, they show timezone information along with a local-time string. But fundamentally, they *are* UTC. Thus, thir routine takes a UTC input, and returns a UTC output.

For example: 1998-07-17 11:00:00.68-0500 is 680 milliseconds after 11 o'clock, central daylight time It is also 680 millisecs after 16:00:00 hours UTC.

Returns:
The universl time.
XXX Caution: this routine does not handle strings that specify times before January 1 1970.

const char* gnc_print_date ( Timespec  ts  ) 

Convenience; calls through to qof_print_date_dmy_buff(). Return: static global string.

Warning:
This routine is not thread-safe, because it uses a single global buffer to store the return value. Use qof_print_date_buff() or qof_print_date() instead.

Definition at line 502 of file gnc-date.c.

00503 {
00504   static char buff[MAX_DATE_LENGTH];
00505   time_t t;
00506 
00507   t = ts.tv_sec + (ts.tv_nsec / 1000000000.0);
00508 
00509   qof_print_date_buff (buff, MAX_DATE_LENGTH, t);
00510 
00511   return buff;
00512 }

void gnc_timespec2dmy ( Timespec  ts,
gint *  day,
gint *  month,
gint *  year 
)

DOCUMENT ME! FIXME: Probably similar to xaccDMYToSec() this date routine might return incorrect values for dates before 1970.

int gnc_timespec_last_mday ( Timespec  ts  ) 

DOCUMENT ME! Probably the same as date_get_last_mday()

Definition at line 1205 of file gnc-date.c.

01206 {
01207   struct tm result;
01208   time_t t_secs = t.tv_sec + (t.tv_nsec / NANOS_PER_SECOND);
01209   localtime_r(&t_secs, &result);
01210   return date_get_last_mday (&result);
01211 }

gchar* gnc_timespec_to_iso8601_buff ( Timespec  ts,
gchar *  buff 
)

The gnc_timespec_to_iso8601_buff() routine takes the input UTC Timespec value and prints it as an ISO-8601 style string. The buffer must be long enough to contain the NULL-terminated string (32 characters + NUL). This routine returns a pointer to the null terminator (and can thus be used in the 'stpcpy' metaphor of string concatenation).

Please note that ISO-8601 strings are a representation of Universal Time (UTC), and as such, they 'store' UTC. To make them human readable, they show timezone information along with a local-time string. But fundamentally, they *are* UTC. Thus, this routine takes a UTC input, and returns a UTC output.

The string generated by this routine uses the local timezone on the machine on which it is executing to create the timestring.

time_t gnc_timet_get_day_end ( time_t  time_val  ) 

The gnc_timet_get_day_end() routine will take the given time in seconds and adjust it to the last second of that day.

Definition at line 1362 of file gnc-date.c.

01363 {
01364   struct tm tm;
01365 
01366   gnc_tm_get_day_end(&tm, time_val);
01367   return mktime(&tm);
01368 }

time_t gnc_timet_get_day_start ( time_t  time_val  ) 

The gnc_timet_get_day_start() routine will take the given time in seconds and adjust it to the last second of that day.

Definition at line 1353 of file gnc-date.c.

01354 {
01355   struct tm tm;
01356 
01357   gnc_tm_get_day_start(&tm, time_val);
01358   return mktime(&tm);
01359 }

time_t gnc_timet_get_today_end ( void   ) 

The gnc_timet_get_today_end() routine returns a time_t value corresponding to the last second of today.

Definition at line 1395 of file gnc-date.c.

01396 {
01397   struct tm tm;
01398 
01399   gnc_tm_get_day_end(&tm, time(NULL));
01400   return mktime(&tm);
01401 }

time_t gnc_timet_get_today_start ( void   ) 

The gnc_timet_get_today_start() routine returns a time_t value corresponding to the first second of today.

Definition at line 1386 of file gnc-date.c.

01387 {
01388   struct tm tm;
01389 
01390   gnc_tm_get_day_start(&tm, time(NULL));
01391   return mktime(&tm);
01392 }

glong gnc_timezone ( const struct tm *  tm  ) 

The gnc_timezone function returns the number of seconds *west* of UTC represented by the tm argument, adjusted for daylight savings time.

This function requires a tm argument returned by localtime or set by mktime. This is a strange function! It requires that localtime or mktime be called before use. Subsequent calls to localtime or mktime *may* invalidate the result! The actual contents of tm *may* be used for both timezone offset and daylight savings time, or only daylight savings time! Timezone stuff under unix is not standardized and is a big mess.

Definition at line 1306 of file gnc-date.c.

01307 {
01308   g_return_val_if_fail (tm != NULL, 0);
01309 
01310 #ifdef HAVE_STRUCT_TM_GMTOFF
01311   /* tm_gmtoff is seconds *east* of UTC and is
01312    * already adjusted for daylight savings time. */
01313   return -(tm->tm_gmtoff);
01314 #else
01315   /* timezone is seconds *west* of UTC and is
01316    * not adjusted for daylight savings time.
01317    * In Spring, we spring forward, wheee! */
01318   return (long int)(timezone - (tm->tm_isdst > 0 ? 3600 : 0));
01319 #endif
01320 }

void gnc_tm_get_day_end ( struct tm *  tm,
time_t  time_val 
)

The gnc_tm_get_day_end() routine will convert the given time in seconds to the struct tm format, and then adjust it to the last second of that day.

Definition at line 1345 of file gnc-date.c.

01346 {
01347   /* Get the equivalent time structure */
01348   tm = localtime_r(&time_val, tm);
01349   gnc_tm_set_day_end(tm);
01350 }

void gnc_tm_get_day_start ( struct tm *  tm,
time_t  time_val 
)

The gnc_tm_get_day_start() routine will convert the given time in seconds to the struct tm format, and then adjust it to the first second of that day.

Definition at line 1337 of file gnc-date.c.

01338 {
01339   /* Get the equivalent time structure */
01340   tm = localtime_r(&time_val, tm);
01341   gnc_tm_set_day_start(tm);
01342 }

void gnc_tm_get_today_end ( struct tm *  tm  ) 

The gnc_tm_get_today_end() routine takes a pointer to a struct tm and fills it in with the last second of the today.

Definition at line 1380 of file gnc-date.c.

01381 {
01382   gnc_tm_get_day_end(tm, time(NULL));
01383 }

void gnc_tm_get_today_start ( struct tm *  tm  ) 

The gnc_tm_get_today_start() routine takes a pointer to a struct tm and fills it in with the first second of the today.

Definition at line 1374 of file gnc-date.c.

01375 {
01376   gnc_tm_get_day_start(tm, time(NULL));
01377 }

QofDateFormat qof_date_format_get ( void   ) 

The qof_date_format_get routine returns the date format that the date printing will use when printing a date, and the scaning routines will assume when parsing a date.

Returns:
: the one of the enumerated date formats.

Definition at line 315 of file gnc-date.c.

00316 {
00317   return dateFormat;
00318 }

const gchar* qof_date_format_get_string ( QofDateFormat  df  ) 

This function returns a strftime formatting string for printing an all numeric date (e.g. 2005-09-14). The string returned is based upon the location specified.

Parameters:
df The date style (us, uk, iso, etc) that should be provided.
Returns:
A formatting string that will print a date in the requested style

Definition at line 355 of file gnc-date.c.

00356 {
00357   switch(df) {
00358    case QOF_DATE_FORMAT_US:
00359     return "%m/%d/%y";
00360    case QOF_DATE_FORMAT_UK:
00361     return "%d/%m/%y";
00362    case QOF_DATE_FORMAT_CE:
00363     return "%d.%m.%y";
00364    case QOF_DATE_FORMAT_UTC:
00365     return "%Y-%m-%dT%H:%M:%SZ";
00366    case QOF_DATE_FORMAT_ISO:
00367     return "%Y-%m-%d";
00368    case QOF_DATE_FORMAT_LOCALE:
00369    default:
00370     return GNC_D_FMT;
00371   };
00372 }

void qof_date_format_set ( QofDateFormat  df  ) 

The qof_date_format_set() routine sets date format to one of US, UK, CE, OR ISO. Checks to make sure it's a legal value. Args: QofDateFormat: enumeration indicating preferred format

Definition at line 331 of file gnc-date.c.

00332 {
00333   if(df >= DATE_FORMAT_FIRST && df <= DATE_FORMAT_LAST)
00334   {
00335     prevQofDateFormat = dateFormat;
00336     dateFormat = df;
00337   }
00338   else
00339   {    /* hack alert - Use a neutral default. */
00340     PERR("non-existent date format set attempted. Setting ISO default");
00341     prevQofDateFormat = dateFormat;
00342     dateFormat = QOF_DATE_FORMAT_ISO;
00343   }
00344 
00345   return;
00346 }

const gchar* qof_date_text_format_get_string ( QofDateFormat  df  ) 

This function returns a strftime formatting string for printing a date using words and numbers (e.g. 2005-September-14). The string returned is based upon the location specified.

Parameters:
df The date style (us, uk, iso, etc) that should be provided.
Returns:
A formatting string that will print a date in the requested style

Definition at line 383 of file gnc-date.c.

00384 {
00385   switch(df) {
00386    case QOF_DATE_FORMAT_US:
00387     return "%b %d, %y";
00388    case QOF_DATE_FORMAT_UK:
00389    case QOF_DATE_FORMAT_CE:
00390     return "%d %b, %y";
00391    case QOF_DATE_FORMAT_UTC:
00392     return "%Y-%m-%dT%H:%M:%SZ";
00393    case QOF_DATE_FORMAT_ISO:
00394     return "%Y-%b-%d";
00395    case QOF_DATE_FORMAT_LOCALE:
00396    default:
00397     return GNC_D_FMT;
00398   };
00399 }

gchar* qof_format_time ( const gchar *  format,
const struct tm *  tm 
)

qof_format_time takes a format specification in UTF-8 and a broken-down time, tries to call strftime with a sufficiently large buffer and, if successful, return a newly allocated string in UTF-8 for the printing result.

Parameters:
format A format specification in UTF-8.
tm A broken-down time.
Returns:
A newly allocated string on success, or NULL otherwise.

Definition at line 878 of file gnc-date.c.

00879 {
00880     gchar *locale_format, *tmpbuf, *retval;
00881     gsize tmplen, tmpbufsize;
00882 
00883     g_return_val_if_fail(format, 0);
00884     g_return_val_if_fail(tm, 0);
00885 
00886     locale_format = qof_time_format_from_utf8(format);
00887     if (!locale_format)
00888         return NULL;
00889 
00890     tmpbufsize = MAX(128, strlen(locale_format) * 2);
00891     while (TRUE) {
00892         tmpbuf = g_malloc(tmpbufsize);
00893 
00894         /* Set the first byte to something other than '\0', to be able to
00895          * recognize whether strftime actually failed or just returned "".
00896          */
00897         tmpbuf[0] = '\1';
00898         tmplen = strftime(tmpbuf, tmpbufsize, locale_format, tm);
00899 
00900         if (tmplen == 0 && tmpbuf[0] != '\0') {
00901             g_free(tmpbuf);
00902             tmpbufsize *= 2;
00903 
00904             if (tmpbufsize > 65536) {
00905                 g_warning("Maximum buffer size for qof_format_time "
00906                           "exceeded: giving up");
00907                 g_free(locale_format);
00908 
00909                 return NULL;
00910             }
00911         } else {
00912             break;
00913         }
00914     }
00915     g_free(locale_format);
00916 
00917     retval = qof_formatted_time_to_utf8(tmpbuf);
00918     g_free(tmpbuf);
00919 
00920     return retval;
00921 }

char* qof_print_date ( time_t  secs  ) 

Convenience; calls through to qof_print_date_dmy_buff(). Return: string, which should be freed when no longer needed.

Definition at line 494 of file gnc-date.c.

00495 {
00496    char buff[MAX_DATE_LENGTH];
00497    qof_print_date_buff (buff, MAX_DATE_LENGTH, t);
00498    return g_strdup (buff);
00499 }

size_t qof_print_date_buff ( char *  buff,
size_t  buflen,
time_t  secs 
)

Convenience: calls through to qof_print_date_dmy_buff().

Definition at line 470 of file gnc-date.c.

00471 {
00472   struct tm theTime;
00473 
00474   if (!buff) return 0 ;
00475 
00476   localtime_r(&t, &theTime);
00477 
00478   return qof_print_date_dmy_buff (buff, len,
00479                    theTime.tm_mday, 
00480                    theTime.tm_mon + 1,
00481                    theTime.tm_year + 1900);
00482 }

size_t qof_print_date_dmy_buff ( gchar *  buff,
size_t  buflen,
int  day,
int  month,
int  year 
)

qof_print_date_dmy_buff Convert a date as day / month / year integers into a localized string representation

Args: buff - pointer to previously allocated character array; its size must be at lease MAX_DATE_LENTH bytes. len - length of the buffer, in bytes. day - day of the month as 1 ... 31 month - month of the year as 1 ... 12 year - year (4-digit)

Returns: number of characters printed

Globals: global dateFormat value

size_t qof_print_gdate ( char *  buf,
size_t  bufflen,
const GDate *  gd 
)

Convenience; calls through to qof_print_date_dmy_buff().

Definition at line 485 of file gnc-date.c.

00486 {
00487   return qof_print_date_dmy_buff( buf, len,
00488              g_date_get_day(gd),
00489              g_date_get_month(gd),
00490              g_date_get_year(gd) );
00491 }

size_t qof_print_time_buff ( char *  buff,
size_t  len,
time_t  secs 
)

The qof_print_time_buff() routine prints only the hour-part of the date. Thus, if secs is ... Returns the number of bytes printed.

Definition at line 573 of file gnc-date.c.

00574 {
00575         int flen;
00576         struct tm ltm, gtm;
00577         
00578         if (!buff) return 0;
00579         if(dateFormat == QOF_DATE_FORMAT_UTC)
00580         {
00581                 gtm = *gmtime (&secs);
00582                 flen = qof_strftime(buff, len, QOF_UTC_DATE_FORMAT, &gtm);
00583                 return flen;
00584         }
00585         ltm = *localtime (&secs);
00586         flen = qof_strftime (buff, len, GNC_T_FMT, &ltm);
00587         
00588         return flen;
00589 }

gboolean qof_scan_date ( const char *  buff,
int *  day,
int *  month,
int *  year 
)

qof_scan_date Convert a string into day / month / year integers according to the current dateFormat value.

Args: buff - pointer to date string day - will store day of the month as 1 ... 31 month - will store month of the year as 1 ... 12 year - will store the year (4-digit)

Return: TRUE if the string seemed to be a valid date; else FALSE.

Globals: uses global dateFormat value to assist in parsing.

Definition at line 784 of file gnc-date.c.

00785 {
00786   return qof_scan_date_internal(buff, day, month, year, dateFormat);
00787 }

gboolean qof_scan_date_secs ( const char *  buff,
time_t *  secs 
)

as above, but returns seconds

Definition at line 790 of file gnc-date.c.

00791 {
00792   gboolean rc;
00793   int day, month, year;
00794   
00795   rc = qof_scan_date_internal(buff, &day, &month, &year, dateFormat);
00796   if (secs) *secs = xaccDMYToSec (day, month, year);
00797 
00798   return rc;
00799 }

gsize qof_strftime ( gchar *  buf,
gsize  max,
const gchar *  format,
const struct tm *  tm 
)

qof_strftime calls qof_format_time to print a given time and afterwards tries to put the result into a buffer of fixed size.

Parameters:
buf A buffer.
max The size of buf in bytes.
format A format specification in UTF-8.
tm A broken-down time.
Returns:
The number of characters written, not include the null byte, if the complete string, including the null byte, fits into the buffer. Otherwise 0.

Definition at line 924 of file gnc-date.c.

00925 {
00926     gsize convlen, retval;
00927     gchar *convbuf;
00928 
00929     g_return_val_if_fail(buf, 0);
00930     g_return_val_if_fail(max > 0, 0);
00931     g_return_val_if_fail(format, 0);
00932     g_return_val_if_fail(tm, 0);
00933 
00934     convbuf = qof_format_time(format, tm);
00935     if (!convbuf) {
00936         buf[0] = '\0';
00937         return 0;
00938     }
00939 
00940     convlen = strlen(convbuf);
00941 
00942     if (max <= convlen) {
00943         /* Ensure only whole characters are copied into the buffer. */
00944         gchar *end = g_utf8_find_prev_char(convbuf, convbuf + max);
00945         g_assert(end != NULL);
00946         convlen = end - convbuf;
00947 
00948         /* Return 0 because the buffer isn't large enough. */
00949         retval = 0;
00950     } else {
00951         retval = convlen;
00952     }
00953 
00954     memcpy(buf, convbuf, convlen);
00955     buf[convlen] = '\0';
00956     g_free(convbuf);
00957 
00958     return retval;
00959 }

Timespec timespec_abs ( const Timespec t  ) 

absolute value, also normalised

Definition at line 238 of file gnc-date.c.

00239 {
00240   Timespec retval = *t;
00241 
00242   timespec_normalize(&retval);
00243   if (retval.tv_sec < 0)
00244   {
00245     retval.tv_sec = - retval.tv_sec;
00246     retval.tv_nsec = - retval.tv_nsec;
00247   }
00248   
00249   return retval;
00250 }

gint timespec_cmp ( const Timespec ta,
const Timespec tb 
)

comparison: if (ta < tb) -1; else if (ta > tb) 1; else 0;

Definition at line 217 of file gnc-date.c.

00218 {
00219   if(ta == tb) return 0;
00220   if(ta->tv_sec < tb->tv_sec) return -1;
00221   if(ta->tv_sec > tb->tv_sec) return 1;
00222   if(ta->tv_nsec < tb->tv_nsec) return -1;
00223   if(ta->tv_nsec > tb->tv_nsec) return 1;
00224   return 0;
00225 }

Timespec timespec_diff ( const Timespec ta,
const Timespec tb 
)

difference between ta and tb, results are normalised ie tv_sec and tv_nsec of the result have the same size abs(result.tv_nsec) <= 1000000000

Definition at line 228 of file gnc-date.c.

00229 {
00230   Timespec retval;
00231   retval.tv_sec = ta->tv_sec - tb->tv_sec;
00232   retval.tv_nsec = ta->tv_nsec - tb->tv_nsec;
00233   timespec_normalize(&retval);
00234   return retval;
00235 }

gboolean timespec_equal ( const Timespec ta,
const Timespec tb 
)

strict equality

Definition at line 208 of file gnc-date.c.

00209 {
00210   if(ta == tb) return TRUE;
00211   if(ta->tv_sec != tb->tv_sec) return FALSE;
00212   if(ta->tv_nsec != tb->tv_nsec) return FALSE;
00213   return TRUE;
00214 }

Timespec timespecCanonicalDayTime ( Timespec  t  ) 

convert a timepair on a certain day (localtime) to the timepair representing midday on that day

Definition at line 258 of file gnc-date.c.

00259 {
00260   struct tm tm;
00261   Timespec retval;
00262   time_t t_secs = t.tv_sec + (t.tv_nsec / NANOS_PER_SECOND);
00263   localtime_r(&t_secs, &tm);
00264   gnc_tm_set_day_middle(&tm);
00265   retval.tv_sec = mktime(&tm);
00266   retval.tv_nsec = 0;
00267   return retval;
00268 }

void timespecFromTime_t ( Timespec ts,
time_t  t 
)

Turns a time_t into a Timespec

Definition at line 1324 of file gnc-date.c.

01325 {
01326     ts->tv_sec = t;
01327     ts->tv_nsec = 0;
01328 }

time_t timespecToTime_t ( Timespec  ts  ) 

Turns a Timespec into a time_t

Definition at line 1331 of file gnc-date.c.

01332 {
01333     return ts.tv_sec;
01334 }

char* xaccDateUtilGetStamp ( time_t  thyme  ) 

The xaccDateUtilGetStamp() routine will take the given time in seconds and return a buffer containing a textual for the date.

Parameters:
thyme The time in seconds to convert.
Returns:
A pointer to the generated string.
Note:
The caller owns this buffer and must free it when done.

Definition at line 975 of file gnc-date.c.

00976 {
00977    struct tm *stm;
00978                                                                                 
00979    stm = localtime (&thyme);
00980                                                                                 
00981    return g_strdup_printf("%04d%02d%02d%02d%02d%02d",
00982       (stm->tm_year + 1900),
00983       (stm->tm_mon +1),
00984       stm->tm_mday,
00985       stm->tm_hour,
00986       stm->tm_min,
00987       stm->tm_sec
00988    );
00989 }

char* xaccDateUtilGetStampNow ( void   ) 

The xaccDateUtilGetStampNow() routine returns the current time in seconds in textual format.

Returns:
A pointer to the generated string.
Note:
The caller owns this buffer and must free it when done.

Definition at line 1002 of file gnc-date.c.

01003 {
01004    time_t now;
01005    time (&now);
01006    return xaccDateUtilGetStamp (now);
01007 }

time_t xaccDMYToSec ( gint  day,
gint  month,
gint  year 
)

Warning:
hack alert XXX FIXME -- these date routines return incorrect values for dates before 1970. Most of them are good only up till 2038. This needs fixing ...
XXX This routine should be modified to assume that the the user wanted the time at noon, localtime. The returned time_t should be seconds (at GMT) of the local noon-time.


Variable Documentation

const char* gnc_default_strftime_date_format

The default date format for use with strftime.

Definition at line 73 of file gnc-date.c.


Generated on Sun Oct 12 05:08:00 2008 for GnuCash by  doxygen 1.5.2