mirror of
https://github.com/Gnucash/gnucash.git
synced 2025-02-25 18:55:30 -06:00
standardize some of the names of the date handling routines;
add routines to print time. git-svn-id: svn+ssh://svn.gnucash.org/repo/gnucash/trunk@8801 57a11ea4-9604-0410-9ed3-97b8803252fd
This commit is contained in:
parent
439e305550
commit
7838125667
@ -281,7 +281,7 @@ xaccTransGetDateStr (Transaction *trans)
|
||||
|
||||
date = localtime (&secs);
|
||||
|
||||
printDate(buf, date->tm_mday, date->tm_mon+1, date->tm_year +1900);
|
||||
qof_print_date_buff(buf, date->tm_mday, date->tm_mon+1, date->tm_year +1900);
|
||||
|
||||
return g_strdup (buf);
|
||||
}
|
||||
|
@ -57,13 +57,18 @@
|
||||
|
||||
#ifdef HAVE_LANGINFO_D_FMT
|
||||
# define GNC_D_FMT (nl_langinfo (D_FMT))
|
||||
# define GNC_D_T_FMT (nl_langinfo (D_T_FMT))
|
||||
# define GNC_T_FMT (nl_langinfo (T_FMT))
|
||||
#else
|
||||
# define GNC_D_FMT "%Y-%m-%d"
|
||||
# define GNC_D_T_FMT "%Y-%m-%d %r"
|
||||
# define GNC_T_FMT "%r"
|
||||
#endif
|
||||
|
||||
|
||||
/* This is now user configured through the gnome options system() */
|
||||
static DateFormat dateFormat = DATE_FORMAT_LOCALE;
|
||||
static DateFormat prevDateFormat = DATE_FORMAT_LOCALE;
|
||||
static QofDateFormat dateFormat = QOF_DATE_FORMAT_LOCALE;
|
||||
static QofDateFormat prevQofDateFormat = QOF_DATE_FORMAT_LOCALE;
|
||||
|
||||
/* This static indicates the debugging module that this .o belongs to. */
|
||||
static short module = MOD_ENGINE;
|
||||
@ -72,20 +77,20 @@ static short module = MOD_ENGINE;
|
||||
\********************************************************************/
|
||||
|
||||
const char*
|
||||
gnc_date_dateformat_to_string(DateFormat format)
|
||||
gnc_date_dateformat_to_string(QofDateFormat format)
|
||||
{
|
||||
switch (format) {
|
||||
case DATE_FORMAT_US:
|
||||
case QOF_DATE_FORMAT_US:
|
||||
return "us";
|
||||
case DATE_FORMAT_UK:
|
||||
case QOF_DATE_FORMAT_UK:
|
||||
return "uk";
|
||||
case DATE_FORMAT_CE:
|
||||
case QOF_DATE_FORMAT_CE:
|
||||
return "ce";
|
||||
case DATE_FORMAT_ISO:
|
||||
case QOF_DATE_FORMAT_ISO:
|
||||
return "iso";
|
||||
case DATE_FORMAT_LOCALE:
|
||||
case QOF_DATE_FORMAT_LOCALE:
|
||||
return "locale";
|
||||
case DATE_FORMAT_CUSTOM:
|
||||
case QOF_DATE_FORMAT_CUSTOM:
|
||||
return "custom";
|
||||
default:
|
||||
return NULL;
|
||||
@ -93,23 +98,23 @@ gnc_date_dateformat_to_string(DateFormat format)
|
||||
}
|
||||
|
||||
gboolean
|
||||
gnc_date_string_to_dateformat(const char* fmt_str, DateFormat *format)
|
||||
gnc_date_string_to_dateformat(const char* fmt_str, QofDateFormat *format)
|
||||
{
|
||||
if (!fmt_str)
|
||||
return TRUE;
|
||||
|
||||
if (!strcmp(fmt_str, "us"))
|
||||
*format = DATE_FORMAT_US;
|
||||
*format = QOF_DATE_FORMAT_US;
|
||||
else if (!strcmp(fmt_str, "uk"))
|
||||
*format = DATE_FORMAT_UK;
|
||||
*format = QOF_DATE_FORMAT_UK;
|
||||
else if (!strcmp(fmt_str, "ce"))
|
||||
*format = DATE_FORMAT_CE;
|
||||
*format = QOF_DATE_FORMAT_CE;
|
||||
else if (!strcmp(fmt_str, "iso"))
|
||||
*format = DATE_FORMAT_ISO;
|
||||
*format = QOF_DATE_FORMAT_ISO;
|
||||
else if (!strcmp(fmt_str, "locale"))
|
||||
*format = DATE_FORMAT_LOCALE;
|
||||
*format = QOF_DATE_FORMAT_LOCALE;
|
||||
else if (!strcmp(fmt_str, "custom"))
|
||||
*format = DATE_FORMAT_CUSTOM;
|
||||
*format = QOF_DATE_FORMAT_CUSTOM;
|
||||
else
|
||||
return TRUE;
|
||||
|
||||
@ -324,31 +329,31 @@ void date_add_months (struct tm *tm, int months, gboolean track_last_day)
|
||||
}
|
||||
|
||||
/**
|
||||
* getDateFormat
|
||||
* qof_date_format_get
|
||||
* Args: nothing
|
||||
* returns: DateFormat: enumeration indicating preferred format
|
||||
* returns: QofDateFormat: enumeration indicating preferred format
|
||||
*
|
||||
* Globals: dateFormat
|
||||
**/
|
||||
DateFormat getDateFormat (void)
|
||||
QofDateFormat qof_date_format_get (void)
|
||||
{
|
||||
return dateFormat;
|
||||
}
|
||||
|
||||
/**
|
||||
* setDateFormat
|
||||
* qof_date_format_set
|
||||
* set date format to one of US, UK, CE, OR ISO
|
||||
* checks to make sure it's a legal value
|
||||
* Args: DateFormat: enumeration indicating preferred format
|
||||
* Args: QofDateFormat: enumeration indicating preferred format
|
||||
* returns: nothing
|
||||
*
|
||||
* Globals: dateFormat
|
||||
**/
|
||||
void setDateFormat(DateFormat df)
|
||||
void qof_date_format_set(QofDateFormat df)
|
||||
{
|
||||
if(df >= DATE_FORMAT_FIRST && df <= DATE_FORMAT_LAST)
|
||||
{
|
||||
prevDateFormat = dateFormat;
|
||||
prevQofDateFormat = dateFormat;
|
||||
dateFormat = df;
|
||||
}
|
||||
else
|
||||
@ -360,54 +365,54 @@ void setDateFormat(DateFormat df)
|
||||
}
|
||||
|
||||
/**
|
||||
* getDateFormatString
|
||||
* qof_date_format_get_string
|
||||
* get the date format string for the current format
|
||||
* returns: string
|
||||
*
|
||||
* Globals: dateFormat
|
||||
**/
|
||||
const gchar *getDateFormatString(DateFormat df)
|
||||
const gchar *qof_date_format_get_string(QofDateFormat df)
|
||||
{
|
||||
switch(df) {
|
||||
case DATE_FORMAT_US:
|
||||
case QOF_DATE_FORMAT_US:
|
||||
return "%m/%d/%y";
|
||||
case DATE_FORMAT_UK:
|
||||
case QOF_DATE_FORMAT_UK:
|
||||
return "%d/%m/%y";
|
||||
case DATE_FORMAT_CE:
|
||||
case QOF_DATE_FORMAT_CE:
|
||||
return "%d.%m.%y";
|
||||
case DATE_FORMAT_ISO:
|
||||
case QOF_DATE_FORMAT_ISO:
|
||||
return "%y-%m-%d";
|
||||
case DATE_FORMAT_LOCALE:
|
||||
case QOF_DATE_FORMAT_LOCALE:
|
||||
default:
|
||||
return GNC_D_FMT;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* getDateTextFormatString
|
||||
* qof_date_format_get_format
|
||||
* get the date format string for the current format
|
||||
* returns: string
|
||||
*
|
||||
* Globals: dateFormat
|
||||
**/
|
||||
const gchar *getDateTextFormatString(DateFormat df)
|
||||
const gchar *qof_date_format_get_format(QofDateFormat df)
|
||||
{
|
||||
switch(df) {
|
||||
case DATE_FORMAT_US:
|
||||
case QOF_DATE_FORMAT_US:
|
||||
return "%b %d, %y";
|
||||
case DATE_FORMAT_UK:
|
||||
case DATE_FORMAT_CE:
|
||||
case QOF_DATE_FORMAT_UK:
|
||||
case QOF_DATE_FORMAT_CE:
|
||||
return "%d %b, %y";
|
||||
case DATE_FORMAT_ISO:
|
||||
case QOF_DATE_FORMAT_ISO:
|
||||
return "%y-%b-%d";
|
||||
case DATE_FORMAT_LOCALE:
|
||||
case QOF_DATE_FORMAT_LOCALE:
|
||||
default:
|
||||
return GNC_D_FMT;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* printDate
|
||||
* qof_print_date_buff
|
||||
* Convert a date as day / month / year integers into a localized string
|
||||
* representation
|
||||
*
|
||||
@ -421,10 +426,11 @@ const gchar *getDateTextFormatString(DateFormat df)
|
||||
*
|
||||
* Globals: global dateFormat value
|
||||
*/
|
||||
void
|
||||
printDate (char * buff, int day, int month, int year)
|
||||
size_t
|
||||
qof_print_date_buff (char * buff, size_t len, int day, int month, int year)
|
||||
{
|
||||
if (!buff) return;
|
||||
int flen;
|
||||
if (!buff) return 0;
|
||||
|
||||
/* Note that when printing year, we use %-4d in format string;
|
||||
* this causes a one, two or three-digit year to be left-adjusted
|
||||
@ -436,16 +442,16 @@ printDate (char * buff, int day, int month, int year)
|
||||
*/
|
||||
switch(dateFormat)
|
||||
{
|
||||
case DATE_FORMAT_UK:
|
||||
sprintf (buff, "%2d/%2d/%-4d", day, month, year);
|
||||
case QOF_DATE_FORMAT_UK:
|
||||
flen = g_snprintf (buff, len, "%2d/%2d/%-4d", day, month, year);
|
||||
break;
|
||||
case DATE_FORMAT_CE:
|
||||
sprintf (buff, "%2d.%2d.%-4d", day, month, year);
|
||||
case QOF_DATE_FORMAT_CE:
|
||||
flen = g_snprintf (buff, len, "%2d.%2d.%-4d", day, month, year);
|
||||
break;
|
||||
case DATE_FORMAT_ISO:
|
||||
sprintf (buff, "%04d-%02d-%02d", year, month, day);
|
||||
case QOF_DATE_FORMAT_ISO:
|
||||
flen = g_snprintf (buff, len, "%04d-%02d-%02d", year, month, day);
|
||||
break;
|
||||
case DATE_FORMAT_LOCALE:
|
||||
case QOF_DATE_FORMAT_LOCALE:
|
||||
{
|
||||
struct tm tm_str;
|
||||
|
||||
@ -454,20 +460,22 @@ printDate (char * buff, int day, int month, int year)
|
||||
tm_str.tm_year = year - 1900; /* this is what the standard
|
||||
* says, it's not a Y2K thing */
|
||||
|
||||
gnc_tm_set_day_start (&tm_str);
|
||||
strftime (buff, MAX_DATE_LENGTH, GNC_D_FMT, &tm_str);
|
||||
gnc_tm_set_day_start (&tm_str);
|
||||
flen = strftime (buff, len, GNC_D_FMT, &tm_str);
|
||||
}
|
||||
break;
|
||||
|
||||
case DATE_FORMAT_US:
|
||||
case QOF_DATE_FORMAT_US:
|
||||
default:
|
||||
sprintf (buff, "%2d/%2d/%-4d", month, day, year);
|
||||
flen = g_snprintf (buff, len, "%2d/%2d/%-4d", month, day, year);
|
||||
break;
|
||||
}
|
||||
|
||||
return flen;
|
||||
}
|
||||
|
||||
void
|
||||
printDateSecs (char * buff, time_t t)
|
||||
qof_print_date_secs_buff (char * buff, time_t t)
|
||||
{
|
||||
struct tm *theTime;
|
||||
|
||||
@ -475,25 +483,26 @@ printDateSecs (char * buff, time_t t)
|
||||
|
||||
theTime = localtime (&t);
|
||||
|
||||
printDate (buff, theTime->tm_mday,
|
||||
qof_print_date_buff (buff, MAX_DATE_LENGTH,
|
||||
theTime->tm_mday,
|
||||
theTime->tm_mon + 1,
|
||||
theTime->tm_year + 1900);
|
||||
}
|
||||
|
||||
void
|
||||
printGDate( char *buf, GDate *gd )
|
||||
qof_print_gdate( char *buf, GDate *gd )
|
||||
{
|
||||
printDate( buf,
|
||||
qof_print_date_buff( buf, MAX_DATE_LENGTH,
|
||||
g_date_day(gd),
|
||||
g_date_month(gd),
|
||||
g_date_year(gd) );
|
||||
}
|
||||
|
||||
char *
|
||||
xaccPrintDateSecs (time_t t)
|
||||
qof_print_date_secs (time_t t)
|
||||
{
|
||||
char buff[100];
|
||||
printDateSecs (buff, t);
|
||||
char buff[MAX_DATE_LENGTH];
|
||||
qof_print_date_secs_buff (buff, t);
|
||||
return g_strdup (buff);
|
||||
}
|
||||
|
||||
@ -505,13 +514,170 @@ gnc_print_date (Timespec ts)
|
||||
|
||||
t = ts.tv_sec + (ts.tv_nsec / 1000000000.0);
|
||||
|
||||
printDateSecs (buff, t);
|
||||
qof_print_date_secs_buff (buff, t);
|
||||
|
||||
return buff;
|
||||
}
|
||||
|
||||
/* ============================================================== */
|
||||
|
||||
size_t
|
||||
qof_print_hours_elapsed_buff (char * buff, size_t len, int secs, gboolean show_secs)
|
||||
{
|
||||
size_t flen;
|
||||
if (0 <= secs)
|
||||
{
|
||||
if (show_secs)
|
||||
{
|
||||
flen = g_snprintf(buff, len,
|
||||
"%02d:%02d:%02d", (int)(secs / 3600),
|
||||
(int)((secs % 3600) / 60), (int)(secs % 60));
|
||||
}
|
||||
else
|
||||
{
|
||||
flen = g_snprintf(buff, len,
|
||||
"%02d:%02d", (int)(secs / 3600),
|
||||
(int)((secs % 3600) / 60));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (show_secs)
|
||||
{
|
||||
flen = g_snprintf(buff, len,
|
||||
"-%02d:%02d:%02d", (int)(-secs / 3600),
|
||||
(int)((-secs % 3600) / 60), (int)(-secs % 60));
|
||||
}
|
||||
else
|
||||
{
|
||||
flen = g_snprintf(buff, len,
|
||||
"-%02d:%02d", (int)(-secs / 3600),
|
||||
(int)((-secs % 3600) / 60));
|
||||
}
|
||||
}
|
||||
return flen;
|
||||
}
|
||||
|
||||
/* ============================================================== */
|
||||
|
||||
size_t
|
||||
qof_print_minutes_elapsed_buff (char * buff, size_tlen, int secs, gboolean show_secs)
|
||||
{
|
||||
size_t flen;
|
||||
if (0 <= secs)
|
||||
{
|
||||
if (show_secs)
|
||||
{
|
||||
flen = g_snprintf(buff, len,
|
||||
"%02d:%02d",
|
||||
(int)(secs / 60), (int)(secs % 60));
|
||||
}
|
||||
else
|
||||
{
|
||||
flen = g_snprintf(buff, len,
|
||||
"%02d", (int)(secs / 60));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (show_secs)
|
||||
{
|
||||
flen = g_snprintf(buff, len,
|
||||
"-%02d:%02d", (int)(-secs / 60), (int)(-secs % 60));
|
||||
}
|
||||
else
|
||||
{
|
||||
flen = g_snprintf(buff, len,
|
||||
"-%02d", (int)(-secs / 60));
|
||||
}
|
||||
}
|
||||
return flen;
|
||||
}
|
||||
|
||||
/* ============================================================== */
|
||||
|
||||
size_t
|
||||
qof_print_date_time_buff (char * buff, size_t len, time_t secs)
|
||||
{
|
||||
int flen;
|
||||
int day, month, year, hour, min, sec;
|
||||
struct tm ltm;
|
||||
|
||||
if (!buff) return buff;
|
||||
|
||||
/* Note that when printing year, we use %-4d in format string;
|
||||
* this causes a one, two or three-digit year to be left-adjusted
|
||||
* when printed (i.e. padded with blanks on the right). This is
|
||||
* important while the user is editing the year, since erasing a
|
||||
* digit can temporarily cause a three-digit year, and having the
|
||||
* blank on the left is a real pain for the user. So pad on the
|
||||
* right.
|
||||
*/
|
||||
ltm = *localtime (&secs);
|
||||
day = ltm.tm_mday;
|
||||
month = ltm.tm_mon +1;
|
||||
year = ltm.tm_year +1900;
|
||||
hour = ltm.tm_hour;
|
||||
min = ltm.tm_min;
|
||||
sec = ltm.tm_sec;
|
||||
|
||||
switch(dateFormat)
|
||||
{
|
||||
case DATE_FORMAT_UK:
|
||||
flen = g_snprintf (buff, len, "%2d/%2d/%-4d %2d:%02d", day, month, year, hour, min);
|
||||
break;
|
||||
case DATE_FORMAT_CE:
|
||||
flen = g_snprintf (buff, len, "%2d.%2d.%-4d %2d:%02d", day, month, year, hour, min);
|
||||
break;
|
||||
case DATE_FORMAT_ISO:
|
||||
flen = g_snprintf (buff, len, "%04d-%02d-%02d %02d:%02d", year, month, day, hour, min);
|
||||
break;
|
||||
case DATE_FORMAT_LOCALE:
|
||||
{
|
||||
flen = strftime (buff, len, GNC_D_T_FMT, <m);
|
||||
}
|
||||
break;
|
||||
|
||||
case DATE_FORMAT_US:
|
||||
default:
|
||||
flen = g_snprintf (buff, len, "%2d/%2d/%-4d %2d:%02d", month, day, year, hour, min);
|
||||
break;
|
||||
}
|
||||
return flen;
|
||||
}
|
||||
|
||||
size_t
|
||||
qof_print_time_buf (char * buff, size_t len, time_t secs)
|
||||
{
|
||||
int flen;
|
||||
struct tm ltm;
|
||||
|
||||
if (!buff) return buff;
|
||||
ltm = *localtime (&secs);
|
||||
flen = strftime (buff, len, GNC_T_FMT, <m);
|
||||
|
||||
return flen;
|
||||
}
|
||||
|
||||
/* ============================================================== */
|
||||
|
||||
int
|
||||
qof_is_same_day (time_t ta, time_t tb)
|
||||
{
|
||||
struct tm lta, ltb;
|
||||
lta = *localtime (&ta);
|
||||
ltb = *localtime (&tb);
|
||||
if (lta.tm_year == ltb.tm_year)
|
||||
{
|
||||
return (ltb.tm_yday - lta.tm_yday);
|
||||
}
|
||||
return (ltb.tm_year - lta.tm_year)*365; /* very approximate */
|
||||
}
|
||||
|
||||
/* ============================================================== */
|
||||
|
||||
/**
|
||||
* scanDate
|
||||
* qof_scan_date
|
||||
* Convert a string into day / month / year integers according to
|
||||
* the current dateFormat value.
|
||||
*
|
||||
@ -531,8 +697,8 @@ gnc_print_date (Timespec ts)
|
||||
* Globals: global dateFormat value
|
||||
*/
|
||||
static gboolean
|
||||
scanDateInternal (const char *buff, int *day, int *month, int *year,
|
||||
DateFormat which_format)
|
||||
qof_scan_date_internal (const char *buff, int *day, int *month, int *year,
|
||||
QofDateFormat which_format)
|
||||
{
|
||||
char *dupe, *tmp, *first_field, *second_field, *third_field;
|
||||
int iday, imonth, iyear;
|
||||
@ -571,44 +737,44 @@ scanDateInternal (const char *buff, int *day, int *month, int *year,
|
||||
/* get numeric values */
|
||||
switch (which_format)
|
||||
{
|
||||
case DATE_FORMAT_LOCALE:
|
||||
case QOF_DATE_FORMAT_LOCALE:
|
||||
if (buff[0] != '\0')
|
||||
{
|
||||
struct tm thetime;
|
||||
|
||||
/* Parse time string. */
|
||||
memset(&thetime, -1, sizeof(struct tm));
|
||||
/* Parse time string. */
|
||||
memset(&thetime, -1, sizeof(struct tm));
|
||||
strptime (buff, GNC_D_FMT, &thetime);
|
||||
|
||||
if (third_field) {
|
||||
/* Easy. All three values were parsed. */
|
||||
iyear = thetime.tm_year + 1900;
|
||||
iday = thetime.tm_mday;
|
||||
imonth = thetime.tm_mon + 1;
|
||||
} else if (second_field) {
|
||||
/* Hard. Two values parsed. Figure out the ordering. */
|
||||
if (thetime.tm_year == -1) {
|
||||
/* %m-%d or %d-%m. Don't care. Already parsed correctly. */
|
||||
iday = thetime.tm_mday;
|
||||
imonth = thetime.tm_mon + 1;
|
||||
} else if (thetime.tm_mon != -1) {
|
||||
/* Must be %Y-%m-%d. Reparse as %m-%d.*/
|
||||
imonth = atoi(first_field);
|
||||
iday = atoi(second_field);
|
||||
} else {
|
||||
/* Must be %Y-%d-%m. Reparse as %d-%m. */
|
||||
iday = atoi(first_field);
|
||||
imonth = atoi(second_field);
|
||||
}
|
||||
} else if (first_field) {
|
||||
iday = atoi(first_field);
|
||||
}
|
||||
if (third_field) {
|
||||
/* Easy. All three values were parsed. */
|
||||
iyear = thetime.tm_year + 1900;
|
||||
iday = thetime.tm_mday;
|
||||
imonth = thetime.tm_mon + 1;
|
||||
} else if (second_field) {
|
||||
/* Hard. Two values parsed. Figure out the ordering. */
|
||||
if (thetime.tm_year == -1) {
|
||||
/* %m-%d or %d-%m. Don't care. Already parsed correctly. */
|
||||
iday = thetime.tm_mday;
|
||||
imonth = thetime.tm_mon + 1;
|
||||
} else if (thetime.tm_mon != -1) {
|
||||
/* Must be %Y-%m-%d. Reparse as %m-%d.*/
|
||||
imonth = atoi(first_field);
|
||||
iday = atoi(second_field);
|
||||
} else {
|
||||
/* Must be %Y-%d-%m. Reparse as %d-%m. */
|
||||
iday = atoi(first_field);
|
||||
imonth = atoi(second_field);
|
||||
}
|
||||
} else if (first_field) {
|
||||
iday = atoi(first_field);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case DATE_FORMAT_UK:
|
||||
case DATE_FORMAT_CE:
|
||||
case QOF_DATE_FORMAT_UK:
|
||||
case QOF_DATE_FORMAT_CE:
|
||||
if (third_field) {
|
||||
iday = atoi(first_field);
|
||||
iday = atoi(first_field);
|
||||
imonth = atoi(second_field);
|
||||
iyear = atoi(third_field);
|
||||
} else if (second_field) {
|
||||
@ -618,9 +784,9 @@ scanDateInternal (const char *buff, int *day, int *month, int *year,
|
||||
iday = atoi(first_field);
|
||||
}
|
||||
break;
|
||||
case DATE_FORMAT_ISO:
|
||||
case QOF_DATE_FORMAT_ISO:
|
||||
if (third_field) {
|
||||
iyear = atoi(first_field);
|
||||
iyear = atoi(first_field);
|
||||
imonth = atoi(second_field);
|
||||
iday = atoi(third_field);
|
||||
} else if (second_field) {
|
||||
@ -630,7 +796,7 @@ scanDateInternal (const char *buff, int *day, int *month, int *year,
|
||||
iday = atoi(first_field);
|
||||
}
|
||||
break;
|
||||
case DATE_FORMAT_US:
|
||||
case QOF_DATE_FORMAT_US:
|
||||
default:
|
||||
if (third_field) {
|
||||
imonth = atoi(first_field);
|
||||
@ -647,22 +813,30 @@ scanDateInternal (const char *buff, int *day, int *month, int *year,
|
||||
|
||||
g_free (dupe);
|
||||
|
||||
if (imonth > 12 || iday > 31) {
|
||||
if ((12 < imonth) || (31 < iday))
|
||||
{
|
||||
/*
|
||||
* Ack! Thppfft! Someone just fed this routine a string in the
|
||||
* wrong date format. This is known to happen if a register
|
||||
* window is open when changing the date format. Try the
|
||||
* previous date format. If that doesn't work, bail and give the
|
||||
* caller what they asked for (garbage) parsed in the new format.
|
||||
* previous date format. If that doesn't work, se if we can
|
||||
* exchange month and day. If that still doesn't work,
|
||||
* bail and give the caller what they asked for (garbage)
|
||||
* parsed in the new format.
|
||||
*
|
||||
* Note: This test cannot detect any format change that only
|
||||
* swaps month and day field, if the day is 12 or less. This is
|
||||
* deemed acceptable given the obscurity of this bug.
|
||||
*/
|
||||
if (which_format == prevDateFormat)
|
||||
return(FALSE);
|
||||
if (scanDateInternal(buff, day, month, year, prevDateFormat))
|
||||
if ((which_format != prevQofDateFormat) &&
|
||||
qof_scan_date_internal(buff, day, month, year, prevQofDateFormat))
|
||||
{
|
||||
return(TRUE);
|
||||
}
|
||||
if ((12 < imonth) && (12 >= iday))
|
||||
{
|
||||
int tmp = imonth; imonth = iday; iday = tmp;
|
||||
}
|
||||
}
|
||||
|
||||
/* if the year entered is smaller than 100, assume we mean the current
|
||||
@ -677,9 +851,9 @@ scanDateInternal (const char *buff, int *day, int *month, int *year,
|
||||
}
|
||||
|
||||
void
|
||||
scanDate (const char *buff, int *day, int *month, int *year)
|
||||
qof_scan_date (const char *buff, int *day, int *month, int *year)
|
||||
{
|
||||
scanDateInternal(buff, day, month, year, dateFormat);
|
||||
qof_scan_date_internal(buff, day, month, year, dateFormat);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -692,21 +866,21 @@ scanDate (const char *buff, int *day, int *month, int *year)
|
||||
*
|
||||
* Globals: global dateFormat value
|
||||
*/
|
||||
char dateSeparator ()
|
||||
char dateSeparator (void)
|
||||
{
|
||||
static char locale_separator = '\0';
|
||||
|
||||
switch (dateFormat)
|
||||
{
|
||||
case DATE_FORMAT_CE:
|
||||
case QOF_DATE_FORMAT_CE:
|
||||
return '.';
|
||||
case DATE_FORMAT_ISO:
|
||||
case QOF_DATE_FORMAT_ISO:
|
||||
return '-';
|
||||
case DATE_FORMAT_US:
|
||||
case DATE_FORMAT_UK:
|
||||
case QOF_DATE_FORMAT_US:
|
||||
case QOF_DATE_FORMAT_UK:
|
||||
default:
|
||||
return '/';
|
||||
case DATE_FORMAT_LOCALE:
|
||||
case QOF_DATE_FORMAT_LOCALE:
|
||||
if (locale_separator != '\0')
|
||||
return locale_separator;
|
||||
else
|
||||
@ -729,6 +903,44 @@ char dateSeparator ()
|
||||
return '\0';
|
||||
}
|
||||
|
||||
/********************************************************************\
|
||||
\********************************************************************/
|
||||
|
||||
/** The xaccDateUtilGetStamp() routine will take the given time in
|
||||
* seconds and return a buffer containing a textual for the date.
|
||||
* @param thyme The time in seconds to convert.
|
||||
* @return A pointer to the generated string.
|
||||
* @note The caller owns this buffer and must free it when done. */
|
||||
char *
|
||||
xaccDateUtilGetStamp (time_t thyme)
|
||||
{
|
||||
struct tm *stm;
|
||||
|
||||
stm = localtime (&thyme);
|
||||
|
||||
return g_strdup_printf("%04d%02d%02d%02d%02d%02d",
|
||||
(stm->tm_year + 1900),
|
||||
(stm->tm_mon +1),
|
||||
stm->tm_mday,
|
||||
stm->tm_hour,
|
||||
stm->tm_min,
|
||||
stm->tm_sec
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/** The xaccDateUtilGetStampNow() routine returns the current time in
|
||||
* seconds in textual format.
|
||||
* @return A pointer to the generated string.
|
||||
* @note The caller owns this buffer and must free it when done. */
|
||||
char *
|
||||
xaccDateUtilGetStampNow (void)
|
||||
{
|
||||
time_t now;
|
||||
time (&now);
|
||||
return xaccDateUtilGetStamp (now);
|
||||
}
|
||||
|
||||
/********************************************************************\
|
||||
* iso 8601 datetimes should look like 1998-07-02 11:00:00.68-05
|
||||
\********************************************************************/
|
||||
@ -1055,25 +1267,6 @@ gnc_timet_get_day_end (time_t time_val)
|
||||
return mktime(&tm);
|
||||
}
|
||||
|
||||
/* The xaccDateUtilGetStamp() routine will take the given time in
|
||||
* seconds and return a buffer containing a textual for the date. */
|
||||
char *
|
||||
xaccDateUtilGetStamp (time_t thyme)
|
||||
{
|
||||
struct tm *stm;
|
||||
|
||||
stm = localtime (&thyme);
|
||||
|
||||
return g_strdup_printf("%04d%02d%02d%02d%02d%02d",
|
||||
(stm->tm_year + 1900),
|
||||
(stm->tm_mon +1),
|
||||
stm->tm_mday,
|
||||
stm->tm_hour,
|
||||
stm->tm_min,
|
||||
stm->tm_sec
|
||||
);
|
||||
}
|
||||
|
||||
/* ======================================================== */
|
||||
|
||||
void
|
||||
@ -1106,16 +1299,5 @@ gnc_timet_get_today_end (void)
|
||||
return mktime(&tm);
|
||||
}
|
||||
|
||||
/* The xaccDateUtilGetStampNow() routine returns the current time in
|
||||
* seconds in textual format. */
|
||||
char *
|
||||
xaccDateUtilGetStampNow (void)
|
||||
{
|
||||
time_t now;
|
||||
time (&now);
|
||||
return xaccDateUtilGetStamp (now);
|
||||
}
|
||||
|
||||
|
||||
/********************** END OF FILE *********************************\
|
||||
\********************************************************************/
|
||||
|
@ -18,7 +18,7 @@
|
||||
\********************************************************************/
|
||||
/** @addtogroup Date
|
||||
@{ */
|
||||
/** @file date.h
|
||||
/** @file gnc-date.h
|
||||
@brief Date handling routines
|
||||
*
|
||||
Utility functions to handle the date (adjusting, get
|
||||
@ -34,31 +34,49 @@
|
||||
@author Copyright (C) 1998,1999,2000 Linas Vepstas <linas@linas.org>
|
||||
*/
|
||||
|
||||
#ifndef XACC_DATE_H
|
||||
#define XACC_DATE_H
|
||||
#ifndef GNC_DATE_H
|
||||
#define GNC_DATE_H
|
||||
|
||||
#include <glib.h>
|
||||
#include <time.h>
|
||||
|
||||
/** The maximum length of a string created by the date printers */
|
||||
#define MAX_DATE_LENGTH 11
|
||||
|
||||
/* Deprecated, backwards-compat defines; remove after gnome2 port */
|
||||
#define getDateFormatString qof_date_format_get_string
|
||||
#define getDateTextFormatString qof_date_format_get_format
|
||||
#define getDateFormat qof_date_format_get
|
||||
#define setDateFormat qof_date_format_set
|
||||
#define DateFormat QofDateFormat
|
||||
#define printDateSecs qof_print_date_secs_buff
|
||||
#define printDate(S,D,M,Y) qof_print_date_buff(S,MAX_DATE_LENTH,D,M,Y)
|
||||
#define printGDate qof_print_gdate
|
||||
#define xaccPrintDateSecs qof_print_date_secs
|
||||
#define scanDate qof_scan_date
|
||||
|
||||
#define DATE_FORMAT_US QOF_DATE_FORMAT_US
|
||||
#define DATE_FORMAT_UK QOF_DATE_FORMAT_UK
|
||||
#define DATE_FORMAT_CE QOF_DATE_FORMAT_CE
|
||||
#define DATE_FORMAT_ISO QOF_DATE_FORMAT_ISO
|
||||
#define DATE_FORMAT_LOCALE QOF_DATE_FORMAT_LOCALE
|
||||
#define DATE_FORMAT_CUSTOM QOF_DATE_FORMAT_CUSTOM
|
||||
|
||||
/** Constants *******************************************************/
|
||||
|
||||
/** Enum for determining a date format */
|
||||
typedef enum
|
||||
{
|
||||
DATE_FORMAT_US, /**< United states: mm/dd/yyyy */
|
||||
DATE_FORMAT_UK, /**< Britain: dd/mm/yyyy */
|
||||
DATE_FORMAT_CE, /**< Continental Europe: dd.mm.yyyy */
|
||||
DATE_FORMAT_ISO, /**< ISO: yyyy-mm-dd */
|
||||
DATE_FORMAT_LOCALE, /**< Take from locale information */
|
||||
DATE_FORMAT_CUSTOM /**< Used by the check printing code */
|
||||
} DateFormat;
|
||||
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_LOCALE, /**< Take from locale information */
|
||||
QOF_DATE_FORMAT_CUSTOM /**< Used by the check printing code */
|
||||
} QofDateFormat;
|
||||
|
||||
#define DATE_FORMAT_FIRST DATE_FORMAT_US
|
||||
#define DATE_FORMAT_LAST DATE_FORMAT_LOCALE
|
||||
|
||||
/** The maximum length of a string created by the date printers */
|
||||
#define MAX_DATE_LENGTH 11
|
||||
#define DATE_FORMAT_FIRST QOF_DATE_FORMAT_US
|
||||
#define DATE_FORMAT_LAST QOF_DATE_FORMAT_LOCALE
|
||||
|
||||
|
||||
/**
|
||||
@ -73,9 +91,9 @@ typedef enum {
|
||||
|
||||
|
||||
/* The string->value versions return 0 on success and 1 on failure */
|
||||
const char* gnc_date_dateformat_to_string(DateFormat format);
|
||||
const char* gnc_date_dateformat_to_string(QofDateFormat format);
|
||||
gboolean gnc_date_string_to_dateformat(const char* format_string,
|
||||
DateFormat *format);
|
||||
QofDateFormat *format);
|
||||
|
||||
|
||||
const char* gnc_date_monthformat_to_string(GNCDateMonthFormat format);
|
||||
@ -202,16 +220,26 @@ long int gnc_timezone (struct tm *tm);
|
||||
|
||||
|
||||
/* ------------------------------------------------------------------------ */
|
||||
/** @name DateFormat functions */
|
||||
/** @name QofDateFormat functions */
|
||||
/*@{*/
|
||||
/** 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.
|
||||
*/
|
||||
QofDateFormat qof_date_format_get(void);
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
void qof_date_format_set(QofDateFormat df);
|
||||
|
||||
/** DOCUMENT ME! */
|
||||
DateFormat getDateFormat(void);
|
||||
const gchar *qof_date_format_get_string(QofDateFormat df);
|
||||
/** DOCUMENT ME! */
|
||||
void setDateFormat(DateFormat df);
|
||||
/** DOCUMENT ME! */
|
||||
const gchar *getDateFormatString(DateFormat df);
|
||||
/** DOCUMENT ME! */
|
||||
const gchar *getDateTextFormatString(DateFormat df);
|
||||
const gchar *qof_date_format_get_format(QofDateFormat df);
|
||||
/*@}*/
|
||||
|
||||
/** dateSeparator
|
||||
@ -234,49 +262,76 @@ char dateSeparator(void);
|
||||
* itself, instead of depending on the routines here.
|
||||
*/
|
||||
/*@{*/
|
||||
/** printDate
|
||||
/** qof_print_date_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)
|
||||
*
|
||||
* Return: nothing
|
||||
* Returns: number of characters printed
|
||||
*
|
||||
* Globals: global dateFormat value
|
||||
**/
|
||||
void printDate (char * buff, int day, int month, int year);
|
||||
size_t qof_print_date_buff (char * buff, size_t buflen, int day, int month, int year);
|
||||
|
||||
/** convenience: calls through to printDate(). **/
|
||||
void printDateSecs (char * buff, time_t secs);
|
||||
/** Convenience: calls through to qof_print_date_buff(). **/
|
||||
void qof_print_date_secs_buff (char * buff, time_t secs);
|
||||
|
||||
/** Convenience; calls through to printDate(). **/
|
||||
void printGDate( char *buf, GDate *gd );
|
||||
/** Convenience; calls through to qof_print_date_buff(). **/
|
||||
void qof_print_gdate( char *buf, GDate *gd );
|
||||
|
||||
/** Convenience; calls through to printDate().
|
||||
/** Convenience; calls through to qof_print_date_buff().
|
||||
* Return: string, which should be freed when no longer needed.
|
||||
* **/
|
||||
char * xaccPrintDateSecs (time_t secs);
|
||||
char * qof_print_date_secs (time_t secs);
|
||||
|
||||
/** Convenience; calls through to printDate().
|
||||
/** Convenience; calls through to qof_print_date_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 printDateSecs()
|
||||
* or xaccPrintDateSecs instead.
|
||||
* global buffer to store the return value. Use qof_print_date_secs_buff()
|
||||
* or qof_print_date_secs instead.
|
||||
* **/
|
||||
const char * gnc_print_date(Timespec ts);
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* time printing utilities */
|
||||
|
||||
/** The qof_print_hours_elapsed_buff() routine will print the 'secs' argument
|
||||
* as HH:MM, and will print the seconds if show_secs is true.
|
||||
* Thus, for example, secs=3599 will print as 0:59
|
||||
* Returns the number of bytes copied.
|
||||
*/
|
||||
size_t qof_print_hours_elapsed_buff (char * buff, size_t len, int secs, gboolean show_secs);
|
||||
size_t qof_print_minutes_elapsed_buff (char * buff, size_t len, int secs, gboolean show_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.
|
||||
*/
|
||||
|
||||
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);
|
||||
|
||||
/** The qof_is_same_day() routine returns 0 if both times are in the
|
||||
* same day.
|
||||
*/
|
||||
|
||||
gboolean qof_is_same_day (time_t, time_t);
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/** The xaccDateUtilGetStamp() routine will take the given time in
|
||||
* seconds and return a buffer containing a textual for the date.
|
||||
* @param thyme The time in seconds to convert.
|
||||
* @return A pointer to the generated string.
|
||||
* @note The caller owns this buffer and must free it when done. */
|
||||
char *xaccDateUtilGetStamp (time_t thyme);
|
||||
|
||||
/** scanDate
|
||||
char * xaccDateUtilGetStamp (time_t thyme);
|
||||
|
||||
/** qof_scan_date
|
||||
* Convert a string into day / month / year integers according to
|
||||
* the current dateFormat value.
|
||||
*
|
||||
@ -289,7 +344,7 @@ char *xaccDateUtilGetStamp (time_t thyme);
|
||||
*
|
||||
* Globals: global dateFormat value
|
||||
*/
|
||||
void scanDate (const char *buff, int *day, int *month, int *year);
|
||||
void qof_scan_date (const char *buff, int *day, int *month, int *year);
|
||||
|
||||
|
||||
/** @name Date Start/End Adjustment routines
|
||||
@ -393,8 +448,9 @@ time_t gnc_timet_get_today_end(void);
|
||||
* seconds in textual format.
|
||||
* @return A pointer to the generated string.
|
||||
* @note The caller owns this buffer and must free it when done. */
|
||||
char *xaccDateUtilGetStampNow (void);
|
||||
char * xaccDateUtilGetStampNow (void);
|
||||
|
||||
/*@}*/
|
||||
|
||||
#endif /* XACC_DATE_H */
|
||||
#endif /* GNC_DATE_H */
|
||||
/** @} */
|
||||
|
@ -164,6 +164,8 @@ foreach (@files)
|
||||
s/gncQuerySortGetIncreasing/qof_query_sort_get_increasing/g;
|
||||
|
||||
s/QuerySort/QofSortFunc/g;
|
||||
s/QueryTerm_t/QofQueryTerm/g;
|
||||
s/SortFunc_t/QofQuerySort/g;
|
||||
|
||||
s/querynew_s/_QofQuery/g;
|
||||
s/QueryNew/QofQuery/g;
|
||||
@ -233,13 +235,28 @@ foreach (@files)
|
||||
s/kvp_value* /KvpValue* /g;
|
||||
s/kvp_value_t/KvpValueType/g;
|
||||
|
||||
}
|
||||
s/QofQueryTerm_t/QofQueryTerm/g;
|
||||
s/QofSortFunc_t/QofQuerySort/g;
|
||||
|
||||
s/QofQof/Qof/g;
|
||||
s/QOF_QOF/QOF/g;
|
||||
|
||||
s/getDateFormatString/qof_date_format_get_string/g;
|
||||
s/getDateTextFormatString/qof_date_format_get_format/g;
|
||||
s/getDateFormat/qof_date_format_get/g;
|
||||
s/setDateFormat/qof_date_format_set/g;
|
||||
s/DateFormat/QofDateFormat/g;
|
||||
s/printDateSecs/qof_print_date_secs_buff/g;
|
||||
s/printDate/qof_print_date_buff/g;
|
||||
s/printGDate/qof_print_gdate/g;
|
||||
s/xaccPrintDateSecs/qof_print_date_secs/g;
|
||||
s/scanDate/qof_scan_date/g;
|
||||
s/DATE_FORMAT_US/QOF_DATE_FORMAT_US/g;
|
||||
s/DATE_FORMAT_UK/QOF_DATE_FORMAT_UK/g;
|
||||
s/DATE_FORMAT_CE/QOF_DATE_FORMAT_CE/g;
|
||||
s/DATE_FORMAT_ISO/QOF_DATE_FORMAT_ISO/g;
|
||||
s/DATE_FORMAT_LOCALE/QOF_DATE_FORMAT_LOCALE/g;
|
||||
s/DATE_FORMAT_CUSTOM/QOF_DATE_FORMAT_CUSTOM/g;
|
||||
|
||||
}
|
||||
|
||||
print OF $_;
|
||||
}
|
||||
close OF;
|
||||
|
Loading…
Reference in New Issue
Block a user